Forum Moderators: phranque

Message Too Old, No Replies

301 redirect of a dynamic URL using mod rewrite problem with passed on

         

finkedelic

11:37 am on Aug 31, 2011 (gmt 0)

10+ Year Member


Hi there,

I have a bit of challenge with redirecting a dynamic URL.
I am trying to redirect a URL to another URL but the resulting URL has the query string of the original URL added to it and I don't want that. Can't seem to find a solution anywhere...

Original URL: http://www.domain.com/CMS/index.php?Itemid=1
Target URL: http://www.domain.com

Code used in .htaccess:

RewriteEngine On
RewriteCond %{QUERY_STRING} Itemid=1
RewriteRule ^.*$ http://www.domain.com [R=301,L]

Resulting URL: http://www.domain.com/Itemid=1

Desired resulting URL: http://www.domain.com

Anyone any ideas?
Thanks!

g1smd

11:41 am on Aug 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can't seem to find a solution anywhere...

The answer to this question is posted almost every day in this forum.

Indeed there have been five threads in the last week with the answer.

You need a question mark to clear the query string.


Note. As you have used the .* pattern in your code you are not restricting it to redirect only requests containing
CMS/index.php
. Is that what you want? I would replace the .* with something like ^CMS/ with no end anchor.

finkedelic

12:06 pm on Aug 31, 2011 (gmt 0)

10+ Year Member



Hi g1smd,

thanks. I read about the question mark in other posts but it's not clear to me where to place the question mark. Hence, I thought this was a different situation...

Sorry, new to Apache...

So where should I put the question mark?

g1smd

12:39 pm on Aug 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



On the end of the target URL, a question mark clears the query string data and stops it being appended to that new URL.


Use example.com in here to stop forum auto-linking.

[edited by: g1smd at 12:48 pm (utc) on Aug 31, 2011]

finkedelic

12:43 pm on Aug 31, 2011 (gmt 0)

10+ Year Member



Thank!

I now wrote it like this:

RewriteCond %{QUERY_STRING} Itemid=1
RewriteRule ^index.php [domain.com?...] [R=301,L]

Am I right if I explain the core of the above code like this (just checking if I am starting to get it):

If the original URL contains 'Itemid=1'
Then rewrite the URL to [domain.com,...] at least if it the original URL starts with 'index.php'

Thanks for helping out!

g1smd

12:53 pm on Aug 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It reads:

If the requested path "begins" indexXphp (where X is "any" character - you should escape the period if you want to match "index DOT php") and the query string "contains" Itemid=1 (with no anchoring for ^ & $, it also matches SomeItemid=1 and Itemid=10002) then redirect to root URL of www subdomain stripping any parameters that were present.

The RewriteRule RegEx pattern is processed before the RewriteCond is looked at.

Always specify a trailing slash for the root URL of any domain or subdomain. Use example.com/ not example.com here.

finkedelic

1:15 pm on Aug 31, 2011 (gmt 0)

10+ Year Member



That clears up a lot. Thanks!