Rewrite rules showing in location bar of browser

别说谁变了你拦得住时间么 提交于 2019-12-13 00:15:15

问题


I have the following rewrite rules:

<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>

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!

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.

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>

The new .htaccess does the same as before as well as:

  • Disables mod_dir.
  • Adds trailing slashes to the URI through the use of a RewriteRule.

回答1:


You could explicitly block those directory urls with other rewrite rules.

Or... You can use DirectorySlash to turn off the redirect.

http://httpd.apache.org/docs/2.0/mod/mod_dir.html



来源:https://stackoverflow.com/questions/2012083/rewrite-rules-showing-in-location-bar-of-browser

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