Jump to content

Recommended Posts

im having a bit of trouble with an expression to reroute my url

 

for instance i want to change my url:

 

news/search/?seach=value

and changed to

news/search/value

 

here is what i have but its not the correct expression if anyone can help please

$routing = array('/news\/search\?(.*?)=(.*?)/' => 'news/search/\2');

 

thank you

[/code]

Link to comment
https://forums.phpfreaks.com/topic/226365-expression-help/
Share on other sites

$routing = array('~news/search/[^=]+=(.*)~' => 'news/search/$1');

 

That's just an educated guess, based on what you provided. If it doesn't work, then you need to provide more info.  Explain where/how you are getting the URL, the context it coming from, example context, etc...

 

 

But it looks like (and based on your mention of making a CMS) what you want to do is if user goes to .../news/search/?search=blah you want to redirect to ../news/search/blah  well if that is the case, then mod_rewrite is what you should be doing.

Link to comment
https://forums.phpfreaks.com/topic/226365-expression-help/#findComment-1169675
Share on other sites

well mod_rewrite wont work for what i want...basically when a user submits a form the url will action to news/search/?seach=whatever then i want to re route it by regex then redirect the page to change the url...i dont think i can do that with mod_rewrite making it show the new url

 

and thank you the regex works great thank you...although i changed it slightly and can't get the first $1 to work if you could kindly help again please...than you

 

<?php
$pattern = "~news/search/?(.*)+=(.*)~";
$result  = "news/search/$1/$2";
$url     = "/news/search/?query=test123";

if (preg_match($pattern, $url, $matches)) 
{
print_r($matches);
echo preg_replace($pattern, $result, $url);
}
else 
{
echo "no";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/226365-expression-help/#findComment-1170040
Share on other sites

well mod_rewrite wont work for what i want...basically when a user submits a form the url will action to news/search/?seach=whatever then i want to re route it by regex then redirect the page to change the url...i dont think i can do that with mod_rewrite making it show the new url

 

That is exactly what mod_rewrite does.  Whenever a request is made to your server, regardless of whether a user clicks on a link, submits a form (action="..."), enters url in address bar, clicks a bookmark, etc... it will redirect to specified url.

 

and thank you the regex works great thank you...although i changed it slightly and can't get the first $1 to work if you could kindly help again please...than you

 

You said this was your

 

before: news/search/?seach=value

after: news/search/value

 

...and that's what my regex does.... 

 

But now it looks like you are trying to do:

 

before: /news/search/?query=test123

after: /news/search/query/test123

 

is that what you are trying to do? If so, then it should be:

 

$pattern = "~news/search/\?([^=]+)=(.*)~";

 

...but again, I really think you should be using mod_rewrite.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/226365-expression-help/#findComment-1170055
Share on other sites

Okay, you have a link like

 

http://www.yoursite.com/news/search?query=something

 

And you want it to be redirected to

 

http://www.yoursite.com/news/search/query/something

 

Put this in your .htaccess file:

 

.htaccess

RewriteEngine on
RewriteCond %{QUERY_STRING} ([^=]+)=([^&]*) 
RewriteRule ^news/search news/search/$1/%2 [R=301]

 

 

Link to comment
https://forums.phpfreaks.com/topic/226365-expression-help/#findComment-1170241
Share on other sites

could get it to work sorry

 

url

news/search?field=value

 

to

news/search/field/value

 

here is my .htaccess which might help...

 

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?url=$1 [PT,L]

RewriteCond %{QUERY_STRING} ([^=]+)=([^&]*)
RewriteRule ^news/search news/search/$1/%2 [R=301]

</IfModule>

<Files .htaccess>
order allow,deny
deny from all
</Files>

 

sorry to be a pain

 

Link to comment
https://forums.phpfreaks.com/topic/226365-expression-help/#findComment-1170272
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.