簡體   English   中英

當url字符串是現有文件的名稱時,mod_rewrite不起作用

[英]mod_rewrite not working when url string is the name of an existing file

我想要做的是刪除網址所需的php文件擴展名,

即myipaddress / abc-> myipaddress / abc.php

但是我似乎無法使用此鏈接中的解決方案使mod_rewrite正常工作。

我的.htaccess文件包含以下幾行:

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

因此,如果我在服務器上有一個名為abc.php的文件,並且瀏覽器中的URL是“ myipaddress / ab”,則將正確顯示abc.php。 找不到任何文件都將返回內部服務器錯誤(因為第三個條件已被注釋掉)。

在這種情況下,我可以使用它:

myipaddress / ab-> myipaddress / abc.php

現在,如果我像這樣更改最后一行(提供的答案):

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

現在,如果我有一個名為abc.php的文件,並將其放在myipaddress / abc中,則會顯示未找到的404信息,

Not Found

The requested URL /abc was not found on this server.

如果我查找服務器上沒有的其他鏈接(如abcd.php),則仍然會收到500個內部服務器錯誤。 但是,當我嘗試使用myipaddress / name之類的鏈接並且如果name.php在我的服務器上時,我得到404。如果URL包含名稱,mod_rewrite不會重寫URL並花費了數小時,這一事實讓我感到困惑在上面。 有人可以幫忙嗎?

聽起來您的系統上啟用了自動內容協商。 您要禁用它。

查看文檔。 它寫得很好,並帶有很多很好的例子:

http://httpd.apache.org/docs/current/content-negotiation.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM