Forum Moderators: phranque

Message Too Old, No Replies

301 redirects without querystring

301 redirects

         

jimmybeige2

11:02 am on Jan 21, 2010 (gmt 0)

10+ Year Member



Hi,

I am did a htaccess rewrite for some pages recently. They are working fine with the rewrite below:

RewriteCond %{HTTP_HOST} ^subdomain.mysite.com$ [NC]
RewriteRule ^(.*)-(page).htm$ [mysite.com...] [L]

This works for something like the URLs below, which is the format I would like my URLs to be in.
[subdomain.mysite.com...]
[subdomain.mysite.com...]

The problem is there are some old pages, that I would like to eventually remove and am doing a 301 redirect for currently. For example

redirect 301 /testpage.htm [westernwebstest.com...]
redirect 301 /test2page.htm [westernwebstest.com...]

The only issue I'm having is for the 301 redirects. It seems to attach the querystring.

So

/testpage.htm becomes [westernwebstest.com...]

and

/testpage.htm becomes [westernwebstest.com...]

Is there any way of preventing the query string being attached at the end?

jimmybeige2

11:50 am on Jan 21, 2010 (gmt 0)

10+ Year Member



Update:
I made a chamge and used:

RewriteRule ^testpage.htm$ /test-page.htm [L,R=301]

This seems to work, is this the same as using the below statement?

redirect 301 /testpage.htm [westernwebstest.com...]

g1smd

8:42 pm on Jan 21, 2010 (gmt 0)

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



Never mix Redirect and RewriteRule in the same .htaccess file, use RewriteRule for all of the rules. If you mix these two types of directives you cannot be sure which order your redirects and rewrites will be evaluated in because they are from two different Apache modules and are evaluated in per-module order. The redirects must go first, so all rewrites and redirects must use directives from the same module.

Your RewriteRule in the post immediately above, needs the domain name adding to the rule target. The rule needs to fix the correct domain name, otherwise your rule redirects non-www to non-www and www to www.

If you need to clear a query string, append a question mark to the end of the target URL in the rule.

I did a htaccess rewrite for some pages recently. They are working fine with the rewrite below:

RewriteCond %{HTTP_HOST} ^subdomain.example.com$ [NC]
RewriteRule ^(.*)-(page).htm$ http://www.example.com/folder/page.php?var1=1&var2=$1 [L]

The above rule is not a rewrite. As it contains a domain name it is a redirect. As there is no [R=301] flag, it sends a 302 redirect. It redirects to a dynamic URL. Remove the domain name and add the [L] flag and turn it back into a rewrite, if that's what you need.

jdMorgan

9:07 pm on Jan 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A few more code and pattern tweaks:

RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule ^(.+)-page\.htm$ /folder/page.php?var1=1&var2=$1 [L]

Jim