用斜杠重写规则问题

时间:2019-03-15 08:17:27

标签: php .htaccess

我有一个网址为

的网站

http://mywebsite.com/abc.php

我添加了用于从网址中删除.php扩展名的代码,如下所示

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

如果有人使用网址进行搜索

http://mywebsite.com/abc正常工作。

如果有人用http://mywebsite.com/abc/搜索,我需要使用htaccess强制将其重定向到http://mywebsite.com/abc

2 个答案:

答案 0 :(得分:0)

  

#删除斜杠

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/$ /$1 [L,R=301]

答案 1 :(得分:0)

您可以使用此:

RewriteEngine on

#redirect and remove trailing slash
#from all URIs except directories "!-d"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ /$1 [L,R=301]
#remove .php extension
#this allows you to access php files without extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/?$ /$1.php [QSA,L]
相关问题