Forum Moderators: phranque

Message Too Old, No Replies

Htaccess Conditional Redirect

How to make a conditional redirect with htaccess?

         

Symbiont

12:13 pm on Nov 26, 2008 (gmt 0)

10+ Year Member



On a single domain I'm using both php and html file extensions. Some files are converted from html to php.
While some of my clients still have bookmarked the html extension. Instead of redirecting them to a 404 error page I would like to do a conditional redirect;
check if there is a different existing extension to the index file.

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.

Samizdata

1:01 pm on Nov 26, 2008 (gmt 0)

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



Unless you have a very large number of files affected then specific redirects might be easier:

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.

...

g1smd

2:16 pm on Nov 26, 2008 (gmt 0)

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



You can also set up your server such that filenames ending in .html are treated as having PHP scripts inside them.

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.

Symbiont

2:49 pm on Nov 26, 2008 (gmt 0)

10+ Year Member



Thanks for your comments.

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.

Samizdata

4:14 pm on Nov 26, 2008 (gmt 0)

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



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]

jdMorgan

3:09 am on Dec 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's a little tricky, but not so bad once you know how to 'construct' the .php filepath and test it:

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]

Checking the filesystem for file or directory exists is inefficient, but the inefficiency here is limited by the fact that we only check if .html extensions are requested. Since most requests on an average site are for images, at least the filesystem checks are limited in scope here, because RewriteConds are not processed unless the RewriteRule pattern matches (see Apache mod_rewrite docs for details).

Jim