8

The problem: Some html pages of php equivalents (apple.html, apple.php; orange.html, orange.php), but not all do (grapes.html).

The goal: If the php version exists, rewrite, otherwise keep it the html version.

I have:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)\.html$ /$1.php [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.php$ /$1.html [R]

Interesting issues:

If I don't put / in front of $1.php then I end up with: site.com/document/root/path (ie: site.com/home/user/www/file.php)

When calling the second RewriteRule, I get http://site.com/http:/site.com/page.html and it tells me there were too many redirects. Notice how there is only one / in the second http.

I've made some progress, I added RewriteBase / and removed the / before the $1, but I still get the too many redirects error (the web page at site.com/page.html has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer).

It seems like if I just rewrite html -> php -> html I get the same error. So it looks like the logic is working, but that sort of logic isn't allowed. I can't think of any other way I could see if a "php version" of a file exists? The only way I can think of is do something similar to:

RewriteCond ^(.*)\.html$ $1.php -f
RewriteRule ^(.*)\.html$ $1.php [R]

Unfortunately that doesn't quite work (I'm guessing because it has three segments on the condition line). I'm trying to get the filename without the extension, then say if filename.php is a file, rewrite page.html to page.php

user677285
  • 81
  • 1
  • 3

3 Answers3

13

you should be able to achieve that by using two conditions:

RewriteCond %{REQUEST_FILENAME} (.*)\.html$
RewriteCond %1\.php -f
RewriteRule ^(.*)\.html$ $1.php [R,L]

The first condition checks if the filename ended with .html and the second uses the back reference %1 from the first condition to check if .php version exists.

Hope it helps. :)

szemian
  • 2,381
  • 3
  • 18
  • 22
  • When I first read your email I was like ah ha! Then I tried it, and no avail :( It still goes to page.html even if the php exists. Did you by chance mean $1 instead of %1 on the second RewriteCond? – user677285 Mar 30 '11 at 14:25
  • I've just tested on my apache and it works. Are you using httpd.conf or .htaccess? Would you post all your rewrite rules if there are others? % symbol is supposed to be used in RewriteCond, $ is used in RewriteRule, at least this is what I remember. :) – szemian Mar 30 '11 at 14:35
1

I'm sorry to answer sooooo late but you will need to add the RewriteBase directive to make it works.

I had the same problem (with your http:/stufff) and fixed it this way :

RewriteBase   /your/application_path
RewriteCond %{REQUEST_FILENAME} (.*)\.html$
RewriteCond %1\.php -f
RewriteRule ^(.*)\.html$ $1.php [L]

Hope it will help !

1

I also wanted to Rewrite all .html request to .php files, but only if the .php file exist. But my variation was that this should only happen if the actual .html file does not exist.

So only calls to .html files that does not exist is Rewritten to .php file, if they do exists by these rules: (I also have tested this on a XAMP local server and also a Apache online server with success!)

# Rewrite rules to .html file to .php if the .php version
# does actually exist.

RewriteCond %{REQUEST_FILENAME} (.*)\.html [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %1\.php -f [NC]
RewriteRule ^(.*).html$ $1.php [NC]
  1. It first check if the requested file is a .html file (we don't want .jpg, .css, .pdf, etc. to be rewritten).
  2. The it checks if that .html file does not exist. (we don't want to rewrite to .php if it actually does exist.)
  3. Then it checks if a .php version does exist (we don't want to rewrite to a non existing .php file).
  4. Then it rewrites the .html to .php
Wasted_Coder
  • 1,878
  • 19
  • 13