2014-10-10 77 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>