我正在尝试将 example.com/news.php
重写为 example.com/news
或 example.com/news/
。使用我的代码 example.com/news
工作得很好,但是 example.com/news/
给了我一个 404 Not Found。
我的代码:
<Directory /var/www/>
Options -Indexes SymLinksIfOwnerMatch -MultiViews
AllowOverride None
Order allow,deny
allow from all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^games/csgo/([0-9]+)$ /games/csgo/view.php?id=$1 [L]
</IfModule>
</Directory>
如何将 example.com/news.php
重定向到 example.com/news
和 example.com/news/
?
最佳答案
您最好坚持使用一种网址类型! (带或不带尾部斜杠)并将错误类型重定向到其他类型,
此代码用于删除网址末尾的斜杠:
RewriteRule ^(.*)/$ /$1 [L,R=301]
如果您在此之前添加 RewriteCond %{REQUEST_FILENAME} !-d
和 RewriteCond %{REQUEST_FILENAME}.php -f
,它也适用于子目录。
关于php - 删除扩展 mod_rewrite 太宽容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41477064/