Forum Moderators: phranque

Message Too Old, No Replies

rewrite foo.htm to foo.html

         

big_k105

7:01 pm on Apr 5, 2010 (gmt 0)

10+ Year Member



Hello, I have done a search and couldn't find anything that would help. I am curious if with apache mod_rewrite if I can redirect foo.htm to foo.html The way our website is written is that htm files are static pages and html dynamic pages. So we switched a static page to a dynamic version so that the page doesn't need to be manually edited when things change. The rule I was using is as follows:

RewriteRule ^/foo.htm http://www.example.com/foo.html [R=301,L]


But that won't work as foo.html still matches the foo.htm rule so it ends up in a loop and never stops. Any help would be greatly appreciated, Thanks

g1smd

7:18 pm on Apr 5, 2010 (gmt 0)

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



Add the
$
end anchor to the end of the pattern to make it an exact match.


There's an inefficiency here, in that you'll need to manually edit the
.htaccess
file every time a change like this is made to the site. If that's once in a very long time, I'd live with it. If it happens often enough to be annoying there's one other thing you could do to 'automate' this.

You could change all the links on every page to point to
.html
URLs. Redirect all
.htm
URL requests to
.html
URLs.

Rewrite
.html
URL requests to serve content from
.htm
files when a
.htm
file exists to serve the content; or when a
.html
file does NOT exist to serve the content.

This way, every page of the site uses
.html
URLs and you can change a file from static
.htm
filename to dynamic
.html
file without having to re-edit the
.htaccess
file or change any links on the site.

This is because URLs and files are not the same thing, the magic of
Mod_Rewrite
can associate any URL request with any internal file inside the server.

In fact for the daring, there's one more step that could be taken: go extensionless, so that URLs for pages have neither
.html
nor
.htm
suffix.

I'd want a scheme where the URLs used out on the web remain the same even when the server technology changes for generating that page.

There's a downside. Inappropriate and badly coded rules could slow the server down enough to make the whole thing a 'very bad idea'.

[edited by: g1smd at 7:43 pm (utc) on Apr 5, 2010]

big_k105

7:25 pm on Apr 5, 2010 (gmt 0)

10+ Year Member



How I didn't figure that out I don't know, lol. I should have known that was how to make it an exact match. Thanks for the help.