.htaccess RewriteRule adds drive path to URL

烂漫一生 提交于 2019-12-31 05:12:34

问题


I am using Zend Server CE (v.5.1.0) installed on C: on a Win7 machine. I have added one project to httpd.conf by adding:

Alias /project "D:\Homepages\project"
<Directory "D:\Homepages\project">
    Options Indexes FollowSymLinks
    AllowOverride all
    Order allow,deny
    Allow from all
</Directory>

My .htaccess file in the project directory contains the following:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/\w*\.(css|js) [NC]
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Now to the problem; if I go to

http://localhost/project/index.php

everything seems to be working fine. I reach the index.php file and get my contents. However, if I go to any other page that would trigger the RewriteRule, it seems to be adding the directory path. FireFox outputs the following Not Found message:

The requested URL /Homepages/project/index.php was not found on this server.

I tried to find a similar question/answer here, but failed. Any idea? Ps. Me accepting of an answer might be delayed as I will be out for a while on an errand.


回答1:


Have your last line like this:

RewriteRule ^.*$ /index.php [NC,L]

However I think this is infinite loop so I would suggest this rule instead:

RewriteCond %{THE_REQUEST} !\s/index.php [NC]
RewriteCond %{REQUEST_URI} !^/index.php [NC]
RewriteRule . /index.php [L]

which prevents going to index.php if it is already /index.php.




回答2:


You need to set the RewriteBase directive; otherwise, mod_rewrite automatically, and by default, prepends the file path to the resulting rewrite rule.

From: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

When a substitution occurs for a new URL, this module has to re-inject the URL into the server processing. To be able to do this it needs to know what the corresponding URL-prefix or URL-base is. By default this prefix is the corresponding filepath itself. However, for most websites, URLs are NOT directly related to physical filename paths, so this assumption will often be wrong! Therefore, you can use the RewriteBase directive to specify the correct URL-prefix. If your webserver's URLs are not directly related to physical file paths, you will need to use RewriteBase in every .htaccess file where you want to use RewriteRule directives.



来源:https://stackoverflow.com/questions/5778681/htaccess-rewriterule-adds-drive-path-to-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!