2013-09-27 79 views
1

我有一個.htaccess文件下列規則結合的.htaccess規則:刪除擴展和重寫URL變量

# replace 'RewriteBase' with current working folder 
# put '<base href="/">' in <head> if you are in the root folder 
# put '<base href="/foldername/">' in <head> if you are in a subfolder of the root 
Options +FollowSymLinks 
RewriteEngine On 
RewriteBase /pretty-urls 

# Remove slash if file 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^/]+)/$ $1 [R=301,L] 

# Redirect to extensionless url 
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/ 
RewriteRule ^(.+)\.php$ $1 [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php [L] 

RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule .* - [L] 
RewriteRule ^(.*)$ user.php?u=$1 [NC] 

這基本上轉換和重定向以下任一網址,www.mysite.com/about.phpwww.mysite.com/aboutwww.mysite.com/about/www.mysite.com/about,也應該允許我寫www.mysite.com/user/john,但相反拋出一個內部服務器錯誤加500.

我找不出什麼是錯的,代碼是基於此(How-to? Vanity URL & ignore .PHP extension with .htaccess

我的PHP看起來像這樣

foreach ($users as $user) { 
    echo '<p><a href="user/'.$user->username.'">'.$user->username.'</a></p>'; 
} 

任何幫助表示讚賞,謝謝。

編輯:Apache的錯誤日誌說

[Fri Sep 27 14:26:26.539589 2013] [core:error] [pid 2276:tid 1680] [client ::1:60023] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://localhost/pretty-urls/users 

至於建議,我刪除了[NC]標誌,並增加了[L]標誌,但同樣的錯誤仍然存​​在。

+0

嘗試調試使用Apache日誌你重寫劇本,像這樣: http://serverfault.com/questions/135694/mod-rewrite-settings-causes-server-to-throw-http-500-errors-404- –

+0

謝謝,我檢查了答案並更新了我的帖子,但還是500錯誤。 –

+0

你在''extensionless url''規則中缺少']'。 – anubhava

回答

2

錯誤Request exceeded the limit of 10 internal redirects通常是由重寫規則引起的,它們都與原始url以及重寫的url相匹配。 L標誌只會停止當前週期的重寫,但由於apache無論如何都會通過.htaccess推送請求,因此這不會阻止這些錯誤的內部重定向。

當我在自己的本地主機上重新創建這個時,我注意到問題是將.php添加到您的url的規則。

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php [L] 

%{REQUEST_FILENAME}包含在我的情況的文件路徑來document_root/user。您測試document_root/user.php是否是一個文件(它是),但是然後將.php添加到該url,這將導致/user/john/.php。我認爲這個問題可以通過將你的文件移動到一個子目錄來解決,這樣虛擬url將永遠不會匹配文件夾中的文件或目錄。

你可以改變規則的東西像下面,但我不知道這是否會完全解決問題:

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^([^/]+)/?(.*)$ $1.php?q=$2 [L] 

這將改寫link.url/test/qwertest.php?q=qwerlink.url/testtest.php?q=

您的規則作出user.php內部重寫應該在「一般」規則上方移動,並應改爲:

RewriteRule ^user/(.*)/?$ user.php?u=$1 [NC,L] 

這將改寫link.url/user/john/user.php?u=john

一如既往,請參閱the documentation。要正確調試.htaccess,請將LogLevel alert rewrite:trace3放入您的httpd.conf並重新啓動服務。這將有呈線條狀;下面,給你一個線索發生了什麼:

[rewrite:trace1] [pid 4800:tid 1532] mod_rewrite.c(468): [client ::1:49451] ::1 - - [localhost/sid#3ecb48][rid#12b1a88/initial/redir#2] [perdir C:/wamp/www/] internal redirect with /user/john/.php.php.php [INTERNAL REDIRECT] 
+0

你實際上解決了它,不知道如何謝謝你。我倒過去的兩個街區,現在一切正常。再次感謝你 ! –