重写规则以删除扩展名".php"并添加尾部斜杠?



可能的重复项:
如何删除.php扩展名并在网址上添加斜杠?

我需要在我的 .htacces(现在是空白的(中放入一些东西来从我的所有 url 中删除.php并强制使用尾部斜杠。


我知道这种问题(通常更具体(已经被问了无数次,但我发誓我找不到适合我的答案。

奇怪的是,我曾经有一个 .htaccess 代码来做我想做的事,我发誓我是从这里得到的代码......但我失去了它。

尝试将以下内容添加到域根目录中的 .htaccess 文件中

RewriteEngine On
RewriteBase /
#skip existing files and directories
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteCond %{REQUEST_FILENAME} !-f 
#capture every url path request ending with a slash 
RewriteCond %{REQUEST_URI} /(.+)/$ [NC]
#and rewritten to .php with that name. Assumes all .php files are in the root
RewriteRule . %1.php [NC,L]    

#add a trailing slash to all urls without one
RewriteCond %{REQUEST_URI} ^(/.+[^/]) [NC]
RewriteRule .  %1/  [L,R=301]
#remove .php from all urls and also add a trailing slash
RewriteRule (.*).php  /$1/  [L,R=301,NC]

编辑:上面进行了修改,以重写路径后缀为斜杠的请求,以.php同名。 请注意,这假定所有.php文件都在根目录中

最新更新