Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

301 Redirect many pages to single new page, except one

301 redirect many old pages using mod_rewrite to newpage, except one



8:46 pm on Jun 5, 2008 (gmt 0)

10+ Year Member

Hey there,

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.


9:16 pm on Jun 5, 2008 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

You didn't say what *does* happen with your code in place. So it's not clear what the problem might be. As long as "olddir/oldpage.php" is not actually the same URL as dir/dir2/page2.php, then your rules should in fact work.

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'm being "strict" in the second rule with the extra RewriteCond. These RewriteConds together require both that the query string start with "code=" AND that the query string NOT match "code=FOOBAR".

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.



10:01 pm on Jun 5, 2008 (gmt 0)

10+ Year Member

Hey jd...sorry about that...i guess knowing what the end result was would help...

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)



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...


1:30 am on Jun 6, 2008 (gmt 0)

10+ Year Member

i finally figured this out...
learned quite a bit more on mod_rewrite now too...

placing a ? at the end of the substitution string in the rewriterule erases the existing query string. so, i appended a ? to each, cleared my cache, tested, and it worked.

thanks for your help on this jd!


1:44 am on Jun 6, 2008 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Yeah, it wasn't clear if you wanted to keep the query or not. And I always forget that trailing "?" even when it is clear that the query should be discarded... :)



Featured Threads

Hot Threads This Week

Hot Threads This Month