多个参数友好的URL重写规则 [英] multiple parameters friendly URL rewriteRule
问题描述
我需要重写规则的多个参数如下帮助:
I need help on rewriteRule for multiple parameters as follow:
sitename.com/project.php?t=value1&a=value2
成为
sitename.com/project/value2/value1
但不知何故,我无法解决问题,并显示在页面 500内部服务器错误
我的htaccess文件:
my htaccess file:
DirectoryIndex index.php
Options -Indexes
<Files *htaccess>
Deny from all
</Files>
<files page>
ForceType application/x-httpd-php
</files>
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^cp/edit-agent/([^/\.]+)/?$ cp/edit-agent.php?name=$1 [L]
RewriteRule ^([^/\.]+)/?$ agent.php?name=$1 [L]
#rule to handle example.com/123/sys
RewriteRule ^project/([0-9]+)/([^/\.]+)/?$ project.php?a=$1&t=$2 [L,QSA]
</IfModule>
请帮忙。
推荐答案
你的规则看行的大部分,但你有2个问题。第一个也是最明显的问题是,你有2个条件只适用于第一个规则:
You're rules look ok for the most part, but you have 2 problems. The first and most obvious problem is that you have 2 conditions that are only applied to the first rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
只适用于这条规则:
is only applied to this rule:
#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
当它也需要被应用到这个规则:
When it also needs to be applied to this rule:
RewriteRule ^([^/\.]+)/?$ agent.php?name=$1 [L]
条件只能得到适用于紧随其后的规则,所以你需要复制它们。
Conditions only get applied to the immediately following rule, so you need to duplicate them.
另一个问题是不那么明显,但是这可能是什么导致500错误,是这样的情况:
The other problem isn't so obvious, but this is probably what's causing the 500 error, is this condition:
RewriteCond %{REQUEST_FILENAME}\.php -f
问题是,你有这样的要求: /工程/ 123 / ABCD
,你有文件 /project.php
。该%{REQUEST_FILENAME}
变量也考虑到路径信息,因此,如果您只是坚持的.php
到最后,它实际上将检查: /project.php/123/abcd
并通过 -f
检查。但在规则本身,你要追加到最后,是这样的:项目/ 123 / abcd.php
。然后下一次,在相同的条件下再次通过,则的.php
被追加到最后一次:项目/ 123 / abcd.php.php
,这样无限循环。
THe problem is that you have requests like: /project/123/abcd
and you have the file /project.php
. The %{REQUEST_FILENAME}
variable also takes into account PATH INFO, so if you just stick .php
to the end, it will actually check: /project.php/123/abcd
and PASS the -f
check. BUt in the rule itself, you're appending it to the end, thus: project/123/abcd.php
. Then the next time around, the same condition passes again, then .php
gets appended to the end again: project/123/abcd.php.php
, thus infinite loop.
所以,你需要改变你的规则是这样的:
So you need to change your rules to look like:
DirectoryIndex index.php
Options -Indexes -Mutiviews
<Files *htaccess>
Deny from all
</Files>
<files page>
ForceType application/x-httpd-php
</files>
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#resolve .php file for extensionless php urls
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^cp/edit-agent/([^/\.]+)/?$ cp/edit-agent.php?name=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/\.]+)/?$ agent.php?name=$1 [L]
#rule to handle example.com/123/sys
RewriteRule ^project/([0-9]+)/([^/\.]+)/?$ project.php?a=$1&t=$2 [L,QSA]
</IfModule>
这篇关于多个参数友好的URL重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!