htaccess: Remove and disable file ending

为君一笑 提交于 2021-02-08 05:11:46

问题


I'd plastered all over the Internet that this code in your .htaccess file will remove the file ending:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

However, although enables the URL without file ending, it does not disable the URL with file ending.

Essentially, what I'd like to force any attempt for an request with file ending (domain.com/sample.php) to end up without file ending (domain.com/sample)

I'm sure someone will have covered this somewhere, but I cannot find anything.

Many thanks.


回答1:


Add this extra rule before your existing rule:

RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]


来源:https://stackoverflow.com/questions/21601051/htaccess-remove-and-disable-file-ending

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!