문제

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