homepage Welcome to WebmasterWorld Guest from 54.226.0.225
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Redirecting duplicate urls with ?menu id= to homepage
301, Rewrite, mod_rewrite
blurped




msg:4606671
 5:04 am on Sep 2, 2013 (gmt 0)

I need to redirect some duplicate homepage urls to the homepage. They have a similar expression, followed by a 3 digit number, like this:

http://example.com/content.cfm?menu_id=012
http://example.com/content.cfm?menu_id=345
http://example.com/content.cfm?menu_id=678
http://example.com/content.cfm?menu_id=901
http://example.com/content.cfm?content_id=012
http://example.com/content.cfm?content_id=345
http://example.com/content.cfm?content_id=678
http://example.com/content.cfm?content_id=901

How can I modify this rule so that it works in .htaccess?

RewriteRule ^content.cfm\?(menu|content)_id=[0-7]+$ http://example.com [R=301,L]

 

lucy24




msg:4606692
 6:10 am on Sep 2, 2013 (gmt 0)

The body of a RewriteRule can't see the query string (the part beginning with the question mark). For that you need a preceding RewriteCond looking at %{QUERY_STRING}.

phranque




msg:4606697
 6:48 am on Sep 2, 2013 (gmt 0)

also note this since you will probably want to drop the query string from the redirected url.

http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

Modifying the Query String

By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.

g1smd




msg:4606780
 4:05 pm on Sep 2, 2013 (gmt 0)

The RegEx pattern
(^|&)(menu|content)_id=[0-7]+(&|$)
will enable the redirect even if this isn't the first parameter.

Don't forget to escape any literal periods in RegEx patterns.

lucy24




msg:4606896
 11:02 pm on Sep 2, 2013 (gmt 0)

Oops, I thought that was g1's typo but it's in the OP
id=[0-7]+

Typo for [0-9] ? The examples include all possible numbers. If your server is OK with \d that's a savings of three bytes each time ;)

If you need to exclude patterns with more or less than 3 digits, say
=\d\d\d(&|$)
or
=\d{3}(&|$)
or
=[0-9][0-9][0-9](&|$)
or
=[0-9]{3}(&|$)
but otherwise it doesn't matter.

g1smd




msg:4607311
 6:13 pm on Sep 4, 2013 (gmt 0)

Yes, check whether you need [0-9] here, or [0-7].

blurped




msg:4611765
 10:00 am on Sep 22, 2013 (gmt 0)

got it, thank you!

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