Forum Moderators: phranque
How do I do a simple 301 redirect from one page to another using the rewrite engine? I have done tons of wildcard rewrites like this:
#RewriteEngine On
#RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
#RewriteRule ^(.*)$ http://www.example.com/$1 [R,L
But if I try to do this it does not work:
RewriteEngine On
RewriteCond %{THE_REQUEST} !/test.html
RewriteRule /default.asp [R=301,L]
Note: I have to use the rewrite syntax, and not the simple redirect, because this is a mod_rewrite engine for Windows/IIS, and the simple syntax does not work
redirect 301 /oldpage.html /newpage.html
RewriteRule ^thispage http://www.example.com/thatpage [R=301,L] The 'thispage' part can be a single named page or a pattern that will match multiple pages.
If you need to 're-use' something on the right you can use $1 etc as usual.
RewriteRule ^pagenumber([0-9]+) http://www.example.com/$1 [R=301,L]
You will need to add an exclusion so that requests for "/default.asp" are not redirected. This may be in addition to, or a replacement of, the exclusion already present for "/test.html" -- I can't tell because you did not explain the intent of your new rule.
Jim
RewriteRule ^ncoa.htm$ /product/ncoa [R=301,L] Now the only issue I have, is that some of the old url's contain spaces...so I'm trying to figure out how to get this to work:
RewriteRule ^contact us.htm$ /contactus [R=301,L] I've tried these combinations with no luck:
RewriteRule "contact us.htm" /contactus [R=301,L] RewriteRule ^contact\ us\.htm$ /contactus [R=301,L]
Note that in order to avoid problems caused by the RewriteRule's failure to detect double-encoded or multiply-encoded spaces, you may have to use a RewriteCond, examine %{THE_REQUEST}, and use a pattern like "\%(25)*20". This shouldn't really be necessary, though, since the RewriteRule should be seeing the fully-decoded space character, and your last rule should have worked.
Jim
One thing which may be an issue, is that this is not true mod_rewite on Apache - this is a commercial IIS mod_rewrite module, so there may be some things which it is either not capable of, or perhaps handles differently form the "real" mod_rewrite...
If this is an older version or if you're using something other than ISAPI Rewrite, then the syntax will be very different and I'll have to refer you to the documentation, because I'm not sufficiently experienced at using it to offer much more than sympathy here...
Jim