Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess redirect all but one page from html to php

My site is set up with htaccess directing all pages from html to php as below:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^website.com
RewriteRule (.*) http://www.website.com/$1 [R=301,L]

RewriteRule ^(.+)\.html$ http://www.website.com/$1.php [R=301,L]

I am trying to set up Yahoo Site Explorer and to verify my account I need to either add a meta tag or add a html file to my website root folder. Yahoo is refusing to recognize my meta tag (I am sure I have added it correctly)! So my only option is to add the html file.

My html file keeps getting redirected to php and it seems yahoo cannot find it.

Is there something I can add to my htaccess file so that all files are redirected to php apart from the one file yahoo needs to see?

like image 886
Alex Mason Avatar asked Dec 22 '22 09:12

Alex Mason


2 Answers

Try adding this condition:

RewriteCond %{REQUEST_FILENAME} !-f

This should allow any files that actually exist (there are other options for other types of files). This assumes it wouldn't mess up anything else for other files that already exist.

like image 146
ldg Avatar answered Dec 24 '22 00:12

ldg


I see 4 main approaches (you choose which one suits you more - it depends on your website rewrite rules logic):

1. Add global exclusion rule that will prevent ANY further rewrite operations on that file:

RewriteCond %{HTTP_HOST} ^website.com
RewriteRule (.*) http://www.website.com/$1 [R=301,L]

# do not do any rewriting to this file
RewriteRule somefile\.html$ - [L]

RewriteRule ^(.+)\.html$ http://www.website.com/$1.php [R=301,L]

If you wish you can specify full path to the file to be more specific (useful to exclude only 1 specific file if there are more than 1 file with such name but in different folders -- such URL should start with NO leading slash):

# do not do any rewriting to this file
RewriteRule ^full/path/to/somefile\.html$ - [L]

2. Add global exclusion rule that will prevent ANY further rewrite operations on ANY existing file or folder:

RewriteCond %{HTTP_HOST} ^website.com
RewriteRule (.*) http://www.website.com/$1 [R=301,L]

# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]

RewriteRule ^(.+)\.html$ http://www.website.com/$1.php [R=301,L]

3. Add exclusion condition that will deny rewriting this particular .html file to .php:

RewriteCond %{REQUEST_URI} !somefile\.html$
RewriteRule ^(.+)\.html$ http://www.website.com/$1.php [R=301,L]

If you wish you can specify full path to the file to be more specific (useful to exclude only 1 specific file if there are more than 1 file with such name but in different folders -- such URL should start with leading slash):

RewriteCond %{REQUEST_URI} !^/full/url/path/to/somefile\.html$

4. Add exclusion condition that will only allow rewriting .html to .php if such .html file does not exist:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.html$ http://www.website.com/$1.php [R=301,L]

ALL RULES ABOVE intended to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.

like image 21
LazyOne Avatar answered Dec 24 '22 02:12

LazyOne