2013-10-23 52 views
1

我有一個重寫規則,從我的網頁中刪除.php擴展名。它和我在技術上看到的每一個樣本都差不多,而且我嘗試過使用相同結果的變體。奇怪的行爲在mod_rewrite

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

它的工作,但我有這個奇怪的異常,我只是不明白。

我所有的php文件都存儲在根文件夾中,我沒有子文件夾。如果我用子文件夾嘗試一個url,我會按預期得到一個404頁面。

但是,如果我使用php腳本名稱作爲文件夾,我會得到500內部服務器錯誤。

例如,我曾稱爲腳本 「logout.php」

http://example.com/eifiefj/ppp給404頁

http://example.com/logout/ppp給500錯誤????????????????? ??

Apache的錯誤日誌說...

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. 
r->uri = /logout/ppp.php.php.php.php.php.php.php.php.php.php 
redirected from r->uri = /logout/ppp.php.php.php.php.php.php.php.php.php 
redirected from r->uri = /logout/ppp.php.php.php.php.php.php.php.php 
redirected from r->uri = /logout/ppp.php.php.php.php.php.php.php 
redirected from r->uri = /logout/ppp.php.php.php.php.php.php 
redirected from r->uri = /logout/ppp.php.php.php.php.php 
redirected from r->uri = /logout/ppp.php.php.php.php 
redirected from r->uri = /logout/ppp.php.php.php 
redirected from r->uri = /logout/ppp.php.php 
redirected from r->uri = /logout/ppp.php 
redirected from r->uri = /logout/ppp 
headers: ap_headers_error_filter() 

這更多的是一種「我想知道爲什麼它的發生」的問題。我只是不明白爲什麼它要繼續添加.php,肯定它應該是失敗的最後一個RewriteCond?

我很想知道這種行爲是否發生在使用刪除擴展技術的其他人身上。

至於建議的RewriteLog ...

add path info postfix: /var/www/public/logout -> /var/www/public/logout/ppp 
strip per-dir prefix: /var/www/public/logout/ppp -> logout/ppp 
applying pattern '^(.+)$' to uri 'logout/ppp' 
RewriteCond: input='/var/www/public/logout' pattern='!-f' => matched 
RewriteCond: input='/var/www/public/logout' pattern='!-d' => matched 
RewriteCond: input='/var/www/public/logout.php' pattern='-f' => matched 
rewrite 'logout/pod' -> 'logout/ppp.php' 
add per-dir prefix: logout/ppp.php -> /var/www/public/logout/ppp.php 
strip document_root prefix: /var/www/public/logout/ppp.php -> /logout/ppp.php 
internal redirect with /logout/ppp.php [INTERNAL REDIRECT] 

nb The log then basically repeats itself adding .php to the end of the filename 

看來,REQUEST_FILENAME包含「/無功/網絡/公/註銷」,我可以看到,添加PHP擴展將匹配最終規則。 REQUEST_FILENAME當然應該是「/ var/www/public/logout/ppp」?爲什麼我失去了/ ppp?

+0

啓用RewriteLog,然後您可以看到在重寫過程中實際發生了什麼。 – CBroe

+0

我編輯過這個問題以包含RewriteLog。看來我的參數正在拾取不正確的信息,或者參數通常會被誤解! (代碼來源於網上的很多例子)。 – MortimerCat

+0

REQUEST_FILENAME在文檔中定義爲_「與文件或腳本匹配請求的完整本地文件系統路徑」_ - 很可能您有[Options MultiViews](http://httpd.apache.org/docs/2.0/en/ mod/core.html#options)啓用,這將使得Apache服務於文件'logout.php',即使只有'logout'被請求。 – CBroe

回答

1

雖然沒有真正回答爲什麼RewriteCond失敗的問題,但我發現了一組實際克服文件夾/腳本問題的規則。我在這裏重現它來幫助任何尋求功能腳本的人。

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

這重寫規則將只適用於文件,文件的名稱不包含句號,即文件不帶擴展名。