homepage Welcome to WebmasterWorld Guest from 54.226.161.112
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
301 Redirect many pages to single new page, except one
301 redirect many old pages using mod_rewrite to newpage, except one
dareRock




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

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

 

jdMorgan




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

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.

Jim

dareRock




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

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

dareRock




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

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!

jdMorgan




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

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

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved