我对重写规则的结果感到非常困惑。我使用 WAMP 在我的本地机器上托管。
RewriteEngine on
RewriteBase /niklasrosenstein/
RewriteCond %{REQUEST_URI} !res/(.+)$
RewriteCond %{REQUEST_URI} !index.php$
RewriteRule ^(.*)$ index.php?uri=$1 [QSA]
去
扩展为
在浏览器的地址栏中。我已经在 Firefox 14、Opera 11.62 和 Internet Explorer 8 下进行了测试。
在 URL 的末尾添加一个斜杠,即
没问题。
有谁知道为什么在浏览器的地址栏中调整 URL?我想使用 mod_rewrite 来摆脱模糊的 URL 格式,但这个问题实际上把它分解了..
最佳答案
Does anyone know why the URL is adjusted in the browsers' address-bar?
这看起来像是 mod_dir/mod_rewrite 冲突。默认情况下,加载 mod_dir,目录模块的默认值是:
DirectoryIndex index.html
DirectorySlash On
第二个默认值使得任何时候出现访问目录的请求,并且缺少尾部斜杠,301 重定向到与斜杠相同的 URI。这发生在 URI 文件映射管道的某处,并且干扰了 mod_rewrite 通过您的规则应用的内部重写
由于您正在通过 index.php
路由所有内容,因此它可能不是 detrimental to turn off DirectorySlash ,因此在您的 /niklasrosenstein/
目录中的 htaccess 文件中,尝试将其关闭:
DirectorySlash Off
否则,您可以尝试使用 mod_rewrite 处理它:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond !.+[^/]$
RewriteRule ^(.+)$ $1/ [L]
关于php - 奇怪的重写(Apache mod_rewrite),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12326972/