Welcome to WebmasterWorld Guest from 54.167.5.15

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirecting duplicate urls with ?menu id= to homepage

301, Rewrite, mod_rewrite

     

blurped

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

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

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



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

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

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



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

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

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



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

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

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



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

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

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



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

blurped

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



got it, thank you!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month