I have the following mod_rewrite rule:
RewriteRule ^([^/.]+)/?$ search.php?action=procedure&procedureName=$1
This works fine in redirecting things like /blabla to /search.php?action=procedure&procedureName=blabla
The problem is that sometimes I want to pass a 'start' value (for pagination). For example, /blabla/?start=20.
Currently, it just ignores it. Printing out the $_REQUEST array doesn't show 'start'. I tried modifying the rule to:
RewriteRule ^([^/.]+)/\?start=([0-9]+)$ search.php?action=procedure&procedureName=$1&start=$2
RewriteRule ^([^/.]+)/?$ search.php?action=procedure&procedureName=$1
But it didn't do anything.
Any idea?
Thanks
RewriteRule ^([^/.]+)/?$ search.php?action=procedure&procedureName=$1 [L,NC,QSA]
The QSA means query string append, and it'll append $_GET vars you pass. Otherwise, they are normally not added.
RewriteRule applies to the path. You need to use RewriteCond to match the query string.
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule
Or to just pass the query string through, use %{QUERY_STRING}
RewriteRule ^([^/.]+)/?$ search.php?action=procedure&procedureName=$1&%{QUERY_STRING}
Actually I think I found my answer:
RewriteRule ^([^/.]+)/?$ search.php?action=procedure&procedureName=$1 [QSA]
The QSA allows it to pass query strings.
Right?
Related
In fact I am working on a small php script, and now I am struggling with doing redirect using mod-rewrite.
What i want is to redirect
www.xx.com/motivational/api.php?latest=1
to
www.xx.com/api.php?latest=1&app=motivational
I tried this but it doesn't work:
RewriteRule ^/motivational/api\.php$ /api.php?latest=&%{QUERY_STRING}
%{QUERY_STRING} represents the entire query string, in your case latest=1. So when you append it to ...?latest= in your substitution string the result is ...?latest=latest=1 which is not what you want.
Change your rule to
RewriteRule ^/motivational/api\.php$ /api.php?%{QUERY_STRING}&app=motivational
and you should be fine.
Or you could do:
RewriteRule ^/motivational/api\.php$ /api.php?app=motivational [QSA]
The QSA flag means to append the new query string to the old, rather than to replace it, so your latest variable will not be lost.
You can not match against query strings in a RewriteRule, You will need a RewriteCond to match query strings in url :
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^/]+)/api\.php\?latest=1 [NC]
RewriteRule ^ /api.php?latest=1&app=%1 [NC,L,R]
%1 is part of the regex "([^/]+)" in RewriteCond, it contains dynmically captured path in the request line.
I have a simple question, but I cant find the way to do it. I need to redirect or rewrite this:
http://mydomain.com/libro.php?isbn=9788493652470
To this:
http://mydomain.com/busqueda.php?busqueda=medimecum
I have already tried this but it doesn’t work:
Redirect 301 /libro.php?isbn=9788493652470 http://mydomain.com/busqueda.php?busqueda=medimecum
A straight Redirect would not work well. But using mod_rewrite with a RewriteCond for a query string would work well. This should work for your example:
RewriteEngine On
RewriteCond %{QUERY_STRING} isbn=9788493652470
RewriteRule ^libro.php http://mydomain.com/busqueda.php?busqueda=medimecum [L,R=301]
Now looking at your question again it’s not clear if you want to pass along the original query string, correct? So this URL:
http://mydomain.com/libro.php?isbn=9788493652470
Would become this URL:
http://mydomain.com/busqueda.php?busqueda=medimecum&isbn=9788493652470
But if somehow you wanted to do that, you could add the QSA (Query String Append) flag to that RewriteRule like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} isbn=9788493652470
RewriteRule ^libro\.php http://mydomain.com/busqueda.php?busqueda=medimecum [L,R=301,QSA]
I am using codeigniter for my website, and before theres always that index.php? on my every url or links, for example
mysite.com/index.php?/about
Google has indexed all of my urls with that index.php? and I want to remove it and redirect it without that. I am having a problem rewriting the URL and redirect it to mysite.com/about and this what i have tried so far
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?(/[^\s\?]+)? [NC]
RewriteRule ^ %1/ [QSA,L,R=301]
what happened is, it only removed the index.php,
for example mysite.com/index.php?/about will turn to mysite.com/?/about I don't know how to remove that question mark,
I'm not good on mod_rewrite thanks in advance for the help.
I think you can improve the rules slightly.
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} index\.php?\?.+
RewriteRule .*$ %{QUERY_STRING}? [R=301,L]
Essentially, you don't have to worry about the entire request line in %{THE_REQUEST}, which removes all the complicated regex. Also, the rule redirects to whatever is listed in %{QUERY_STRING}, and removes the query string.
I am not sure why you used QSA in the first place. I think that was part of the problem earlier. Just for an exercise, you can try removing QSA and see what happens.
You should try this one.
RewriteCond %{QUERY_STRING} ^/[a-z]+$ [NC]
RewriteRule ^/index.php$ %{QUERY_STRING} [NC,L,R=301]
I currently have a simple rewrite that redirects
/photos/2
to
/photoviewer.php?photo=2
However I need to be able to allow the user to add a photo to their cart so I was thinking I need to retain the query string so that the following works.
/photos/2?action=purchase
redirects to:
/photoviewer.php?photo=2&action=purcahse
My current htaccess rule is:
RewriteRule ^photos/([a-zA-Z0-9_-]+)$ photoviewer.php?photo=$1 [L]
Append the [QSA] flag (query string append).
RewriteRule ^photos/([a-zA-Z0-9_-]+)$ photoviewer.php?photo=$1 [L,QSA]
In your rule, replace [L] with [L,QSA].
That will then retain the query information.
Hope this helps!
[QSA,L] instead of just [L] should do the trick. Hope this helps!
How about just extending the idea to pass "any" name/value pairs thru the URL to the photoviewer.php script? This is an elegant approach I've used in the past.
RewriteRule ^photos\/([^/\.\-]+)\/([^/\.\-]+)$ /photoviewer\.php\?$1=$2 [L]
RewriteRule ^photos\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)$ /photoviewer\.php\?$1=$2&$3=$4 [L]
RewriteRule ^photos\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)$ /photoviewer\.php\?$1=$2&$3=$4&$5=$6 [L]
So, something like:
photos/photo/2/action/purchase
rewrites to:
photoviewer.php?photo=2&action=purchase
If I am mod_rewriting a URL from:
http://www.mysite.com/blog/this-is-my-title/1/
to
http://www.mysite.com/blog.php?title=this-is-my-title&id=1
...is it possible then to arbitrarily attach a get value on to the URL later, or does the mod_rewrite throw it off?
MY REWRITE RULE:
RewriteRule ^blog/([A-Za-z]+)/(0-9]+)/? blog?title=$1&id=$2 [L]
EXAMPLE:
can i go
http://www.mysite.com/blog/this-is-my-title/1/?first=Johnnie&last=Wiggles
which would essentially mean
http://www.mysite.com/blog.php?title=this-is-my-title&id=1&first=Johnnie&last=Wiggles
I would think that should work, but for some reason it's not for me at the moment.
You can add QSA to the RewriteRule flags:
RewriteRule page_([0-9]+)\.html page.php?id=$1 [QSA]
Will redirect page_1.html?a=2 to page.php?id=1&a=2
However, be careful because requesting page_1.html?id=2 will redirect to page.php?id=1&id=2, and (in PHP), $_GET['id'] will be 2.
It's possible to append it, with the QSA (query string append) flag.
RewriteEngine on
RewriteRule {from-url} {to-url} [L,NC,QSA]