问题描述
我有以下重写规则:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# Route all URLs to dispatch.php.
RewriteCond %{REQUEST_URI} !media/
RewriteRule ^(.*)$ dispatch.php [L]
#Route requests to /media/* to /project/media/*
RewriteRule ^media/(.*)?$ project/media/$1 [L]
</IfModule>
一切都被改写成 dispatch.php
除非URI开始媒体/
在这种情况下,将改写URI来项目/媒体/ *
。一切工作正常,如果我浏览到 example.com/media/css/style.css
样式表它服务。如果我浏览到 example.com/media/css /
然后一个403 Forbidden错误发送。完美!
Everything is rewritten to dispatch.php
unless the URI starts with media/
in which case it will rewrite the URI to project/media/*
. Everything works fine and if I navigate to example.com/media/css/style.css
the stylesheet it served. If I navigate to example.com/media/css/
then a 403 Forbidden error is sent. Perfect!
不过,如果我浏览到 example.com/media/css
(缺少尾随'/'),然后在地址栏中的URL改写为 example.com/project/media/css
,并请求由 dispatch.php
。如何阻止发生这种行为?我想它通过处理 dispatch.php
,但没有项目/
添加到URL。
However, If I navigate to example.com/media/css
(missing the trailing '/') then the URL in the location bar is rewritten to example.com/project/media/css
and the request is handled by dispatch.php
. How do I stop this behaviour from happening? I would like it to be handled by dispatch.php
but without projects/
being added to the URL.
解决:的问题是由于mod_dir重定向是没有结尾的斜线的网址。这导致了位置,因为它被用于重定向这意味着该子文件夹(项目)附加到该URI出现在磁盘上。我用最后的.htaccess如下:
Solved: The problem was due to mod_dir redirecting the URLs that had no trailing slash. This resulted in the location as it appears on disk being used for the redirect which means that the subfolder (project) was appended to the URI. The final .htaccess that I used is as follows:
Options +FollowSymLinks +MultiViews -Indexes
Options -Indexes
<IfModule mod_dir.c>
DirectorySlash Off
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
#Add trailing slashes
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1/ [R=301,L]
#Route requests to /media/* to /project/media/*
RewriteRule ^media(/.*)$ project/media$1 [L]
# Route all URLs to dispatch.php.
RewriteCond %{REQUEST_URI} !^media/(.*)
RewriteRule ^(.*)$ dispatch.php [L]
</IfModule>
新的.htaccess不会和以前一样还有:
The new .htaccess does the same as before as well as:
- 禁用mod_dir。
- 添加尾随斜杠的URI通过使用重写规则的。
- Disables mod_dir.
- Adds trailing slashes to the URIthrough the use of a RewriteRule.
推荐答案
您可以明确地阻止这些目录下的网址与其他重写规则。
You could explicitly block those directory urls with other rewrite rules.
或...您可以使用DirectorySlash关闭重定向。
Or... You can use DirectorySlash to turn off the redirect.
http://httpd.apache.org/docs/2.0/mod/ mod_dir.html
这篇关于显示在浏览器的地址栏中重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!