2014-10-10 74 views
1

我需要重写规则的多个参数如下帮助:多个参数友好的URL重写规则

sitename.com/project.php?t=value1&a=value2 

成为

sitename.com/project/value2/value1 

但不知何故,我无法解决的问题,并表示500 Internal Server Error

页面

我的htaccess文件:

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> 

请帮忙。

回答

1

你的规则大部分看起来不错,但你有2个问题。第一个也是最明显的问题是,你有2个条件仅适用于第一个规则:

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 ^([^/\.]+)/?$ agent.php?name=$1 [L] 

条件只适用于紧随其后的规则,所以你需要复制它们。

另一个问题是不那么明显,但是这可能是什么导致500错误,就是这个条件:

RewriteCond %{REQUEST_FILENAME}\.php -f 

的问题是,你有这样的要求:/project/123/abcd,你有文件/project.php 。变量%{REQUEST_FILENAME}也考虑了PATH INFO,所以如果你只是坚持.php到最后,它实际上会检查:/project.php/123/abcd和PASS -f检查。在规则本身中,您将其追加到最后,因此:project/123/abcd.php。然后下一次,同样的条件再次通过,然后.php再次被追加到末尾:project/123/abcd.php.php,从而无限循环。

所以,你需要改变你的规则是这样的:

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>