【问题标题】:Fixing mod_rewrite and mod_alias conflict修复 mod_rewrite 和 mod_alias 冲突
【发布时间】:2010-07-25 19:03:43
【问题描述】:

我有一个这样的 VirtualHost 设置:

Alias /somedir /some/other/dir

http://example.com/somedir 工作正常

但是,我需要为 /somedir(我想要干净 URL 的 CodeIgniter 应用程序)设置 mod_rewrite。这是来自 CodeIgniter wiki 的部分内容:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

通常,当mod_rewrite 用于子目录时,更改RewriteBase 以匹配目录名称就足以使其工作:

RewriteBase /somedir

...但它似乎不适用于mod_alias(得到404s)。这个问题有解决办法吗?

【问题讨论】:

  • 附带说明:当包含模拟网址时,使用 example.com 作为域名,这就是它的用途。我怀疑你是 site.com 的所有者 :)
  • 您在哪里定义了mod_rewrite 规则?
  • @Wrikken ups...我的错,感谢您纠正 :)。 @Tim,在别名目录根目录下的 .htaccess 文件中。我也尝试在 vhost 配置文件中设置它们,效果相同。

标签: apache mod-rewrite codeigniter clean-urls mod-alias


【解决方案1】:

以下在我的测试服务器上工作,所以尝试这可能会解决你的问题..

在您的虚拟主机中:

Alias /somedir /some/other/dir

RewriteEngine On

# I think these conditions should work correctly; the other ones shouldn't
# because the alias has not been applied at this stage of processing yet
RewriteCond /some/other/dir%{REQUEST_URI} !-d
RewriteCond /some/other/dir%{REQUEST_URI} !-f
# L is unnecessary in this context, but be sure to PT so mod_alias picks up
# on the rewrite
RewriteRule ^/somedir/(.*)$ /somedir/index.php/$1 [PT]

【讨论】:

  • 谢谢,但它仍然无法正常工作......我已经尝试将它放在 .htaccess 中,在别名目录的根目录中,在 vhost 配置中的 <Location> 块中,仍然没有去吧。
  • 您是否尝试将其放在任何 <Location>/<Directory> 块之外的 vhost 配置中?稍后我将使用.htaccess 进行测试,看看我是否能弄清楚为什么这也可能不起作用。
【解决方案2】:

虽然我没有测试过你的示例,但在我自己的配置中,我在 PT 标志方面没有取得多大成功,可能是因为与我的配置中的重写规则交互。

我发现将别名的使用限制为重写条件更容易,并且始终在重写输出中使用文件系统路径。

Define SOME_DIR = "/somedir"
Define SOME_OTHER_PATH = "/some/other/dir"

Alias "${SOME_DIR}" "${SOME_OTHER_PATH}"

RewriteEngine On
RewriteCond ${SOME_DIR}%{REQUEST_URI} !-d
RewriteCond ${SOME_DIR}%{REQUEST_URI} !-f

RewriteRule ^${SOME_DIR}/(.*)$ ${SOME_OTHER_PATH}/index.php/$1 [NC,QSA,L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 2016-10-15
    • 1970-01-01
    • 2011-03-08
    相关资源
    最近更新 更多