.htaccess удалить расширение типа файла, когда специально введено - PullRequest
0 голосов
/ 04 марта 2012

В настоящий момент мой файл .htaccess будет принимать ссылки в формате: website.com/file (автоматически добавляющего .php) или даже с неверным расширением, например: website.com/file.foo (удаляя расширение и заменяя его на .php)

Однако, если пользователь вводит URL-адрес с расширением правильное , например: website.com/file.php, расширение не будет скрыто.Это не очень эстетично, поэтому я хотел бы удалить его.Однако все методы, которые я пробую, приводят к бесконечному циклу перенаправления.

Как бы я это сделал?Мой текущий код:

#If the URL does not exist, try removing the URL's file type
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([A-Za-z0-9_/]+)\.([A-Za-z0-9_/]+)$ /$1 [NC,R]

#If the URL does not exist, but the URL+.php exists, then append the .php extension to it
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^([A-Za-z0-9_/-]+)$ /$1.php [NC,L]

#If the URL does not exist, but the URL+.html exists, then append the .html extension to it
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule ^([A-Za-z0-9_/-]+)$ /$1.html [NC,L]

#If the URL does not exist and it's got the .php extension, get rid of the .php extension
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.+)\.php$ /$1 [NC,L]

1 Ответ

0 голосов
/ 05 марта 2012

Попробуйте изменить первый раздел правил, как показано ниже

#If the URL does not exist, try removing the URL's file type
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([A-Za-z0-9_/]+)\.([A-Za-z0-9_/])(\?|\ ) [NC]  
RewriteRule ^ /%1 [NC,R=301]
...