Forum Moderators: phranque
So check if there is an existing php file followed by an html and htm extension. If none of the files exist it will result in an 404 error page.
I've searched for an solution with .htaccess but i haven't found one.
So far I tried out the RewriteRule:
RewriteEngine On
RewriteRule (.*).html$ $1.php [NC,L]
The command above one doesn't have the desired effect that I'm looking for. It changes every html into php, while some pages on my domain still uses the html extension, which result in an 404 error page with this command.
Have any of you come across to this problem and got a solution or references to it?
Your advice will be greatly appreciated.
Redirect 301 /somefile.html http://www.example.com/somefile.php
Redirect 301 /someotherfile.html http://www.example.com/someotherfile.php
Redirect 301 /yetanotherfile.html http://www.example.com/yetanotherfile.php
I appreciate that this does not address your question exactly, but it may do what you need.
...
In that case there is no need for any name changes, no need for any rewrites, and no need for any redirects. You continue to use .html URLs for all of your pages.
***
RewriteRule (.*)\.html$ $1.php [NC,L] *** Your code above takes every .html URL request and connects it to the equivalent .php filename on the server.
That's a rewrite, not a redirect.
However it does involve a large amount of files.
Also the .html does not need to be treated as PHP scripts.
I have converted multiple subsites from html to php on one domain and basically only the extension has changed to the filename.
The solution I'm searching for is to handle old bookmarked URL's and inbound/anchor-links that still use the wrong extension, so instead of getting a page-error it needs to process a check first if there is an alternative extension to the filename.
I have converted multiple subsites from html to php on one domain and basically only the extension has changed to the filename.
Unfortunately that makes it a "wouldn't start from here" question - simply setting the server to parse HTML as PHP (as g1smd suggested) would have been the ideal solution ("cool URLs don't change"), but presumably it's too late for that now.
Judicious searching of previous posts in this forum will probably turn up the necessary information. I do recall, though, that checking if files exist is processor intensive and may slow your server down.
RewriteCond %{REQUEST_FILENAME} and RewriteCond %{DOCUMENT_ROOT} with a "-f" flag are the likely candidates, but the logic may get complicated.
Hope this helps.
...
[edited by: Samizdata at 4:18 pm (utc) on Nov. 26, 2008]
RewriteEngine on
#
# If the requested URL exists as a file with a .php extension
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# internally rewrite requested .html URL to .php file
RewriteRule ^(([^/]+/)*[^.]+)\.html$ /$1.php [NC,L]
Jim