問題描述
我試圖從我的網站中刪除 PHP 擴展.當用戶請求一個 PHP 文件時,PHP 將被刪除并且用戶將被重定向,當用戶輸入一個沒有 PHP 的 URL 時,將提供實際的 PHP 文件.這很有效,除非 URL 中有 GET 參數.我的規則如下:
I was trying to remove the PHP extensions from my website. When user requests a PHP file, the PHP will be removed and the user will be redirected, and when the user types in an URL without PHP, the actual PHP file will be served. This worked well except when there is GET parameter in the URL. My rules are as below:
# remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (.phpsHTTP/1)
RewriteRule ^(.+).php$ /$1 [R=301,L,QSA]
# remove trailing slash ONLY if it is not an existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# rewrite to FILENAME.php if such file does exist and is not a folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]
我認為即使有任何 GET 參數,這也應該已經能夠刪除 php,但它失敗了.我也試過這樣的:
I thought this should already be able to remove php even when there is any GET parameter, but it failed. I also tried something like this:
RewriteCond %{THE_REQUEST} (.phpsHTTP/1)
RewriteRule ^(.).php(.)$ $1$2 [R=301,L,QSA]
也沒用,php還在.但如果我嘗試:
It also didn't work, the php is still there. But if I try:
RewriteRule ^(.).php(.)$ $1$2 [R=301,L,QSA]
即,移除 RewriteCond,php 擴展被移除,參數被保留,但頁面不會被提供,因為瀏覽器說有太多的重定向.
ie, removing the RewriteCond, the php extension gets removed and the parameters were preserved, but the page won't be served as the browser says there were too many redirects.
有人有什么想法嗎?
推薦答案
感謝 SteAp 的回答.剛好我現在能想出一個辦法來處理它,想在這里分享一下,以防其他人遇到類似的問題.
Thank you SteAp for your answer. I happened to be able to figure out a way to deal with it just now, and would like to share here in case others run into similar problems.
在我的規則中,我有
# remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (.phpsHTTP/1)
RewriteRule ^(.+).php$ /$1 [R=301,L,QSA]
當用戶請求一個 PHP 文件時執行外部重定向.這里的 RewriteCond 是為了防止重定向循環——即由于內部重寫和外部重定向不當而導致的無限重定向(刪除 php,然后添加 php,然后再次刪除,...)
To execute external redirect when user requests a PHP file. The RewriteCond here is to prevent redirect loops - ie, endless redirect due to improper internal rewrite and external redirect (removing php, then adding php, then remove again, ...)
當有參數時,實際的標題為 http://domain.com/file.php?.... HTTP/1.1像這樣的東西,所以 RewriteCond 中的模式將不起作用,因為它沒有考慮參數.
When there are parameters, the actual header comes as http://domain.com/file.php?.... HTTP/1.1 Something like this, so the pattern in the RewriteCond won't work since it didn't take parameters into account.
要解決它,只需將上面的代碼替換為:
To solve it, simply replace the above code with:
# remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (.php(.*)sHTTP/1)
RewriteRule ^(.+).php$ /$1 [R=301,L,QSA]
通過這樣做,參數可以被模式匹配,現在一切正常.
By doing so, the parameters can be matched by the pattern, and now everything works.
希望這能幫助有類似問題的人(或者它只是像我這樣的菜鳥?lol)
Hope this will help someone having similar problem (or is it just a noob like me? lol)
這篇關于如何使用 Apache Mod_rewrite 刪除 php 擴展,同時保留 GET 參數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!