I've used htaccess to remove extensions or redirect, such as;

RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^(.*)/$ /$1.php [L]

I was wondering if anyone has used to it in this way:

If file extension can't be found, check for html or php file under the same name re-direct to file exith extension.

I can only find examples for redirection, file-extension removal and adding trailing slashes to the file, not to do something like my example, any ideas?

有帮助吗?

解决方案

Try this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

# see if .php file can be found with same name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

# now see if .html file can be found with same name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top