Welcome to WebmasterWorld Guest from 54.147.189.54

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirecting duplicate urls with ?menu id= to homepage

301, Rewrite, mod_rewrite

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

New User

joined:Aug 31, 2013
posts: 9
votes: 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]
6:10 am on Sept 2, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12702
votes: 244


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}.
6:48 am on Sept 2, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10542
votes: 8


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.
4:05 pm on Sept 2, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 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.
11:02 pm on Sept 2, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12702
votes: 244


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.
6:13 pm on Sept 4, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


Yes, check whether you need [0-9] here, or [0-7].
10:00 am on Sept 22, 2013 (gmt 0)

New User

joined:Aug 31, 2013
posts: 9
votes: 0


got it, thank you!