开发者

.htaccess RewriteRule adds drive path to URL

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 开发者_JAVA技巧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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜