homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

URL Rewrite for File Extention Vs. Mime Type Addition
Rewrite the .php to .html than use AddHandler

 5:29 pm on Mar 1, 2009 (gmt 0)

Direct to the point question:

I am using AddHandler to make .html render as PHP files. I wish to change that into a URL rewrite but I am not good at regex and mod_rewrite stuff. I wish to remove the add handler and do some kind of rewrite that makes all *.html really display *.php

Optional to read long story why I want to do this:

I use cPanel servers with several websites and many with custom file extension, or using .html to be rendered as .php by using AddHandler in my .htaccess file. In older Apache versions I used to use AddType.

Now that I have moved over to several VPS servers instead of a dedicated, I have less control on the server, but I put all my eggs in several baskets and minimizes problem websites during downtime issues.

One thing I noticed though with cPanel is whenever they upgrade it, or recompile Apache, for some reason it comments out all # AddHandler lines in all .htaccess files in all accounts. And since these hosting companies don't always tell us when this happens, I just suddenly get complains from everyone.

This seems to be the only line cPanel comments out during upgrades. So I was thinking of doing a rewrite solution instead so I touch the sites less often when these not anticipated cPanel upgrade happens.



 6:11 pm on Mar 1, 2009 (gmt 0)

The code for the rewrite will be something like:

RewriteRule [i]somepattern[/i]\.html $1.php [L]

The trick is in selecting the exact 'pattern' you need.


 6:30 pm on Mar 1, 2009 (gmt 0)

Thanks g1smd, is there any way we can do some kind of wildcard thing to do it all, all patterns? I really suck at regex.

[edited by: BenjArriola at 6:31 pm (utc) on Mar. 1, 2009]


 6:36 pm on Mar 1, 2009 (gmt 0)

Yes. In this case, perhaps
(.*)\.html might do, but that is usually very inefficient.

Maybe ^([^.]+)\.html might be it. It depends on the exact format of the URLs you want.

However, instead of using .html URLs you could also consider going extensionless.

Don't forget to change the links on your pages to use the new format of URL. It is links that 'define' URLs.


You will also need a redirect placed before the rewrite such that if anything requests a .php URL, they are redirected to the correct new URL. Failure to add the redirect will lead to both URLs being available to be indexed by search engines. You do need to solve that Duplicate Content issue. Do make sure that you also force the www at the same time in the same redirect for those URLs.


 7:16 pm on Mar 1, 2009 (gmt 0)

Excellent, thanks

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