[英].htaccess file extension and $_GET rewrite
我想在我的.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工作正常。
我究竟做错了什么?
就像Micheal说的那样,你需要改变顺序,但迈克尔没有移动RewriteCond,这导致了意想不到的行为。
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]
我确实在我的测试服务器上验证了这段代码。
你几乎有这个正确的。 要对/snippets
采取特定操作,它需要在catch-all规则之前。 否则,第一个规则匹配并路由到不存在的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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.