Welcome to WebmasterWorld Guest from 54.196.233.239

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

URL Rewrite for File Extention Vs. Mime Type Addition

Rewrite the .php to .html than use AddHandler

     

BenjArriola

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

5+ Year Member



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.

g1smd

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.

BenjArriola

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

5+ Year Member



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]

g1smd

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.

BenjArriola

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

5+ Year Member



Excellent, thanks
 

Featured Threads

Hot Threads This Week

Hot Threads This Month