dongqun9403 2017-08-13 08:03
浏览 27
已采纳

当url string是现有文件的名称时,mod_rewrite不起作用

What I want to do is to remove the php file extension needed for the url,

i.e. myipaddress/abc -> myipaddress/abc.php

However I can't seem to get mod_rewrite to work using the solutions in this link.

My .htaccess file contains these lines:

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

So if i have a file named abc.php on my server and my url in the browser is "myipaddress/ab", abc.php will be properly shown. Any file that cannot be found will return me a internal server error (since the third cond is commented out).

In this case i get this to work:

myipaddress/ab -> myipaddress/abc.php

Now if i change the last line like this (which is the answer provided):

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

Now if I have a file named abc.php and i put in myipaddress/abc, i get a 404 not found which says

Not Found

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

If i look up for other links like abcd.php which is not on my server, i still get a 500 internal server error. However when i try a link like myipaddress/name and if name.php is on my server, i get a 404. I'm very confused by the fact that if the url contains name, mod_rewrite will not rewrite the url and have spent hours on it. Can anyone help?

  • 写回答

1条回答 默认 最新

  • doupo2157 2017-08-13 09:26
    关注

    Sounds like you have automatic content negotiation enabled on your system. You want to disable it.

    Take a look into the documentation. It is well written and comes with lots of great examples:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?