我正在尝试将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/
?
答案 0 :(得分:2)
你应该更好地坚持一个url类型! (带或不带斜杠)并将错误的类型重定向到另一个,
此代码用于从url的末尾删除斜杠:
RewriteRule ^(.*)/$ /$1 [L,R=301]
如果您之前添加RewriteCond %{REQUEST_FILENAME} !-d
和RewriteCond %{REQUEST_FILENAME}.php -f
,则它也适用于子目录。