Frossy
Frossy

Reputation: 458

htaccess redirect for not existing files

I want to redirect on my website if a file does not exsist as .php file but it does as .html file. For exmaple:

If page.php does not exsist i want to redirect to page.html

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /default.php [L]

So far i managed to redirect to default.php if any file does not exsist. How can i redirect to the html file if one exsists and how do i limit this on redirection on php files ?

Upvotes: 0

Views: 38

Answers (1)

arkascha
arkascha

Reputation: 42885

I have always been a fan of explicit and transparent rules:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?(.*)/?$ /$1.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?(.*)/?$ /$1.html [L]

Upvotes: 1

Related Questions