Forum Moderators: phranque
I've been banging my head for bit on this as I'm more of a novice (if that ;) ) when it comes to regex. I've gotten some help with simple regex from WebmasterWorld in the past and, as always, greatly appreciate the support from the community...
i have a feeling i'm close, but missing some small detail i can't put my finger on.
I'm 301 redirecting a bunch of old pages that have a common query string to a single new page. The problem I'm having is 301 redirecting one specific page to a seperate new page
Curreently I have:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^code=FOOBAR?$
RewriteRule ^olddir/oldpage.php$ http://www.example.com/dir/dir2/page2.php [R=301,L]
RewriteCond %{QUERY_STRING} ^code=.*$
RewriteRule ^olddir/oldpage.php$ http://www.example.com/dir/dir2/page1.php [R=301,L]
I'm trying to say, when you get this unique case url olddir/oldpage.php?code=FOOBAR send it to http://www.example.com/dir/dir2/page2.php, BUT anything else with a code other than 'FOOBAR", send it to http://www.example.com/dir/dir2/page1.php.
I was assuming that the first would catch the unique case and send it along and the 'L' flag would stop the remaining condition/ruleset, but it doesn't seem to be working.
Any advice would be greatly appreciated.
D
The exception would be if you internally rewrite the dir/dir2/page2.php URL back to the olddir/oldpage.php file.
Bear in mind that once a redirect is invoked, the client comes back with an entirely new HTTP request, and the server will have no "memory" of that client's previous request. For this reason, if the two directories cited above are actually the same, then you could get unexpected results.
You also need to know that with your rules written as they are, query strings are passed through unchanged. And that could play into the scenario I describe above.
Another possibility, since you didn't report the actual problem, might be that you forgot to completely flush your browser cache before testing. In this case, your browser will display its cached copy of the URL-contents, and no request will be sent to your server. Under those conditions, server-side code can have no effect on what is displayed by the browser.
If the URL-paths cited above are actually the same, then I'd suggest:
Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{QUERY_STRING} ^code=FOOBAR$
RewriteRule ^olddir/oldpage\.php$ http://www.example.com/dir/dir2/page2.php [R=301,L]
#
RewriteCond %{QUERY_STRING} ^code=
RewriteCond %{QUERY_STRING} !^code=FOOBAR$
RewriteRule ^olddir/oldpage\.php$ http://www.example.com/dir/dir2/page1.php [R=301,L]
I removed "RewriteBase /" since that is the default behaviour, and no use wasting CPU time on it.
If you are doing an internal rewrite that might confuse these rules, then let us know.
Jim
Basically, every query string with 'code=anything' would go to http://www.example.com/dir/dir2/page1.php
Although, for not knowing this, you did quite well. your code works.
the only detail is it appends the 'code=anything' to the new urls...like:
(using your example)
http://www.example.com/dir/dir2/page2.php?code=FOOBAR
and
http://www.example.com/dir/dir2/page1.php?code=everyThingElse
i tried wrapping (code=FOOBAR), (code=), (code=FOOBAR) for each of the rewrite conditions respectfully, and the same appending result. I figured if I didn't set the $1, it wouldn't get appended...