尾随斜线导致错误code 500 [英] Trailing slashes causing error code 500
问题描述
我有这样的的htaccess
剧本
Options -Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /$1.php [L,QSA]
所以,我能得到访问一个页面,比如 example.com/test
但是当我尝试去 example.com/test /
,它抛出一个500错误。谁能告诉我什么需要改变它的工作?可能是一个愚蠢的错误。
So i can get access a page like example.com/test
but when i try to go to example.com/test/
it throws a 500 error. Can someone tell me what needs to change for it to work? Probably a stupid error.
推荐答案
更改从模式^(+)$
来一个,将处理单独斜线: ^(。* [^ /])/?$
Change pattern from ^(.+)$
to one that will handle trailing slash separately: ^(.*[^/])/?$
Options -Indexes +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])/?$ /$1.php [L,QSA]
这将同时适用于 /测试
和 /测试/
- 两者都将被改写成 /test.php
。
This will work for both /test
and /test/
-- both will be rewritten to /test.php
.
如果你想不同的行为,有 /测试
工作,但有404 Not Found错误的 /测试/
(而不是愚蠢的500服务器端的错误),你可以使用这个:
If you want different behaviour, to have /test
working but have "404 Not Found" error for /test/
(instead of stupid "500 Server-side" error) you can use this:
Options -Indexes +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]
上面的规则将检查目标文件是否存在前进行重写..所以,如果 /test.php
不存在,则不会进行重写。
The rule above will check if target file exists BEFORE making rewrite .. so if /test.php
does not exist, no rewrite will occur.
这篇关于尾随斜线导致错误code 500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!