Forum Moderators: phranque

Message Too Old, No Replies

Redirect all page requests to HTM?

         

mountainsoft

6:39 pm on Feb 6, 2012 (gmt 0)

10+ Year Member



My web site uses PHP pages internally, but I want all references to appear as HTM pages to the end user. I am currently using the following rewrite rule which works nicely for this:

RewriteRule ^(.*)\.htm$ $1.php [NC,L]


Unfortunately, if the user goes directly to a PHP page, it bypasses the rewrite rule above. The result is duplicate pages in Google listings and other sites:

sample.htm
sample.php

The code above works great for rewriting HTM pages, but I want to redirect other requests to the HTM version (which is then rewritten internally to serve a PHP page). For example:

sample.htm
sample.php
sample.asp
sample.html
sample

Should all redirect to:

sample.htm

Which should actually serve the internal sample.php page.

Any idea how I would accomplish this?

g1smd

11:38 pm on Feb 6, 2012 (gmt 0)

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



You have an internal rewrite .htm -> .php

Now you need an external redirect .php -> .htm

Both will use a RewriteRule.

The rewrite uses the [L] flag and the redirect uses the [R=301,L] flags.

The redirect will include a domain name in the rule target.

The RewriteRule for the redirect will be preceded with a RewriteCond testing THE_REQUEST for .php so that only external requests for .php will be redirected. This RewriteCond prevents an infinite redirect-rewrite loop.