繁体   English   中英

使用htaccess覆盖Apache重写规则

[英]override apache rewrite rules with htaccess

在承载多个域的服务器上工作。 在apache config中的某处是为所有域设置的重写规则。

发生的情况是,如果用户转到example.com/foo,则应该将他们重定向到example.com/foo_bar

但是,在一个域中,我想覆盖此行为,以便url保持在example.com/foo且不重定向。 我一直在搜索并尝试各种规则和条件都无济于事。 我无权访问apache配置,但是我在此域上使用.htaccess进行一些重写规则。

这是我在.htaccess中的重写规则:

<IfModule mod_rewrite.c>
    RewriteEngine on

    # this isn't working
    # RewriteRule ^(foo)($|/) - [L]

    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?/$1

    # this didn't work either
    # RewriteRule ^/foo index.php?/$1 [L]
</IfModule>

尝试将/foo_bar重定向回/foo

当Apache处理重写规则时,它会多次运行它们,您可以在打开debug时看到这些规则。 我认为您遇到的情况是您试图在第一遍中匹配URI,但是Apache已将其修改为/foo_bar

另外,作为调试的问题,您应该尝试在您控制的环境中重新创建问题。 向您的系统管理员询问全局配置的副本,并镜像您要约束的设置。

您可以为一个域创建例外:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} !^(?:www\.)?domain\.com$ [NC]
RewriteRule ^foo(/.*)?$ /foo_bar$1 [L,R=302]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM