homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Simple 301 Redirect using mod rewrite

 7:46 pm on Feb 9, 2009 (gmt 0)

I think this question is so simple that it's probably never been asked.

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



 10:09 pm on Feb 9, 2009 (gmt 0)

In a redirect, the target URL needs to contain the domain name.

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]


 2:24 pm on Feb 10, 2009 (gmt 0)

The new rule creates an "infinite" redirection loop, because it will redirect if the requested URL-path is NOT "/test.html". Therefore, after the initial redirect to "/default.asp", the redirect rule will be invoked again, because "/default.asp" is NOT "/test/html".

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.



 5:51 pm on Feb 10, 2009 (gmt 0)

It looks like this is the format that works:

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]


 6:50 pm on Feb 10, 2009 (gmt 0)

Please define "no luck." How did you test? What were the results? How did those results differ from your expectations? Did you completely flush your browser cache before testing?

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.



 7:03 pm on Feb 10, 2009 (gmt 0)

Hi jd - by "no luck", I mean that when I requested the old url in my browser, no rewriting happened - it just rendered the old page and that was it. So, I was looking at http://www.example.com/contact us.htm, and not [http:...] as it should have been.

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...


 7:52 pm on Feb 10, 2009 (gmt 0)

There are several versions of ISAPI Rewrite, if that's what you are referring to. If so, then the latest version supports "fully-Apache-mod_rewrite-compliant" code.

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...


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved