【发布时间】:2016-12-19 22:59:24
【问题描述】:
我目前在为具有mod_rewrite 的 Apache 2.2 服务器配置 .htaccess 文件中的重写规则时遇到了一些麻烦。以下是我想要做的总体思路:
假设服务器域是 example.com/,所有对 example.com/abc/ 的请求和该目录下的路径都将被重写。有两种情况:
-
直接向 example.com/abc/ 或 example.com/abc/index.php 的请求应成为对 example.com/index.php 的请求,并带有一个附加参数来指示请求的原始目录。举几个例子:
-
example.com/abc/==>example.com/abc/index.php?directory=abc -
example.com/abc/?foo=1&bar=2==>example.com/abc/index.php?directory=abc&foo=1&bar=2 -
example.com/abc/?a=b&c=d==>example.com/abc/index.php?directory=abc&a=b&c=d - ...等等
-
-
对 example.com/abc/ 中文件的请求应成为对 example.com/ 中文件的请求,如果这些文件存在于那里。参数表示当前目录。举几个例子:
-
example.com/abc/image.png==>example.com/image.png(如果后面的文件存在) -
example.com/abc/sub/file.css==>example.com/sub/file.css(如果后面的文件存在) -
example.com/abc/foo/bar/baz/name.js==>example.com/foo/bar/baz/name.js(如果后面的文件存在) - ...等等。
-
我的 .htaccess 的内容目前看起来类似于:
RewriteEngine on
Options FollowSymLinks
RewriteBase /
# rule for files in abc/: map to files in root directory
RewriteCond $1 -f
RewriteRule ^abc/(.*?)$ $1
# rule for abc/index.php: map to index.php?directory=abc&...
RewriteCond $1 !-f
RewriteRule ^abc/(.*?)$ index.php?directory=abc&$1 [QSA]
后面的规则似乎有效,对 example.com/abc/index.php 的请求按预期重写。但是,这不适用于 abc/ 目录中的文件。 任何关于我在这里做错了什么以及如何解决该问题的提示都值得赞赏。必须对 .htaccess 应用哪些更改才能使事情按描述工作?
【问题讨论】:
标签: apache .htaccess mod-rewrite lamp apache2.2