我想在我的.htacces文件中重写这些内容:
/*.php -> /*(/)
(例如gallery.php到/ gallery或/ gallery /)
/snippets.php*?s= -> /snippets/*
(例如,snippets.php *?s = test to / snippets / test或/ snippets / test /)
到目前为止我的代码:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^snippets/([^/\.]+)/?$ snippets.php?s=$1 [L]
使用我的代码显示的错误:
/ snippets /和/ snippets / test(/)将发出500错误警报。 / snippets工作正常。
我做错了什么?
答案 0 :(得分:2)
RewriteEngine on
RewriteBase /
RewriteRule ^snippets/([^/.]+)/?$ snippets.php?s=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]
我在测试服务器上验证了这段代码,只是为了确定。
答案 1 :(得分:0)
你几乎有这个正确的。要对/snippets
采取具体行动,需要在全面规则之前采取行动。否则,第一条规则匹配并路由到snippets/test.php
,但不存在。
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Match snippets first...
RewriteRule ^snippets/([^/.]+)/?$ snippets.php?s=$1 [L]
# Then the catch-all for remaining matches
RewriteRule ^(.*)$ $1.php [L]