Forum Moderators: phranque

Message Too Old, No Replies

Redirect 301 with URL Rewriting in the same rule

Redirect 301 with URL Rewriting

         

dcuniah

7:42 am on Mar 1, 2011 (gmt 0)

10+ Year Member



Hi, can someone please help me on this :
I need to redirect 301 an old HTML page to a new HTML one, where i'm stuck is that the new page is in fact in .PHP (example.php) which i rewrite to .HTML (example.html), i've tried to apply these rules below but they don't seems to work together:
-------------------------------------------------------------------
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*old-page-example\.html\ HTTP/
RewriteRule ^(.*) http://www.example.com/new-page-example.html [R=301,L]

RewriteRule ^new-page-example.html new-page-example.php
-------------------------------------------------------------------

Is there a way to write only one rule that does both?
Thanking you in advance for your reply

g1smd

9:58 am on Mar 1, 2011 (gmt 0)

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



the new page is in fact in .PHP (example.php) which i rewrite to .HTML (example.html)

This description is exactly backwards.

Mod_rewrite accepts a request for a .html URL and rewrites it to fetch the content from a .php file inside the server.

In what way did it fail?

# Redirect old .html request to new .html URL (and strip parameters)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*old-page-example\.html(\?[^\ ]*)? HTTP/
RewriteRule ^([^/]*/)*old-page-example\.html http://www.example.com/new-page-example.html? [R=301,L]

# Redirect external .php request to new .html URL (and strip parameters)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*new-page-example\.php(\?[^\ ]*)? HTTP/
RewriteRule ^([^/]*/)*new-page-example\.php http://www.example.com/new-page-example.html? [R=301,L]

# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# Rewrite .html request to fetch the .php file.
RewriteRule ^new-page-example.html /new-page-example.php [L]


The "and strip parameters" part is optional, and is performed by the question mark on the end of the redirect target. If the site does not use parameters, you DO want to leave that code in place so that a request for /old-example-page.html?for=bar is properly redirected to the correct new URL and without parameters.