Reputation: 1680
I have been unable to get a redirect to work. The behavior I was hoping to have would redirect from:
www.mysite.com/?id=12345
to
www.mysite.com/?other_id=12345
The rule I wrote seems to grab any url matching the very end like:
www.mysite.com/directory/another/?id=12345
I just want this to work at the root level, not all levels.
The rule I currently have is as follows:
RewriteCond %{QUERY_STRING} ^(.*&)?id=([^&]+)$
RewriteRule ^(.*)$ http://%{SERVER_NAME}/?other_id=%2 [R=301,L]
For some reason I had to include the first regex as it was not accepting it without it. Without the first ^(.*&) It kept saying that the site was creating a redirect that would not work and bombing out, which may be a part of my problem.
IS there a better way to write this rule? or is a root level match alone not possible?
Thank you,
Silvertiger
Upvotes: 2
Views: 148
Reputation: 143886
Change your regex from ^(.*)$
to `^$``:
RewriteCond %{QUERY_STRING} ^(.*&)?id=([^&]+)$
RewriteRule ^$ http://%{SERVER_NAME}/?other_id=%2 [R=301,L]
The ^(.*)$
regex matches any request URI, so /
, and /foo
and /dir1/dir2/dir3/file.txt
will all match. But the ^$
only matches /
.
Upvotes: 2