What more could you want?
Remove the Query String from a RewriteRule
Apache’s mod_rewrite is a whole universe of complexity of and to itself.
Suppose I have a URL that I want to redirect elsewhere:
http://www.ozymo.com/~chuck/home/tester.php?name=chuck&date=today
This PHP script doesn’t even exist on the server. This one does:
http://www.ozymo.com/~chuck/projects/mod/redird.html
So, I configure Apache to allow me to issue mod_rewrite directives in a
.htaccess file for that directory:
AllowOverride FileInfo
I bounce Apache, and put the following rewrite rules into a .htaccess file:
$ cat .htaccess
RewriteEngine on
#RewriteCond %{REQUEST_URI} ^/tester\.php$ [NC]
RewriteCond %{QUERY_STRING} ^name=chuck&date=today$
RewriteRule ^(.*)$ http://www.ozymo.com/~chuck/projects/mod/redird.html
[L,R=301]
As such, I am redirected here:
http://www.ozymo.com/~chuck/projects/mod/redird.html?name=chuck&date=today
I do not want that. I want to remove the query string from the URL. How
am I to do that? Good question, and the question gives us our answer:
I append a question mark to the end of the Substitution:
RewriteRule ^(.*)$ http://www.ozymo.com/~chuck/projects/mod/redird.html?
[L,R=301]
And voila, I’ve been redirected to the html page without an appended query
string. What a pain, that little question mark is!
/cs
| Print article | This entry was posted by chuck on November 27, 2008 at 12:20 am, and is filed under Uncategorized. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 1 year ago
After about 1 weak searching for this, I finally found it here!
That question mark at the end is a must!!
Thank you very much!!
about 1 year ago
What is left of my hair thanks you for posting this Gem….