1

我有这个动态规则:

RewriteRule ^(.*)/(.*)$ /rewrite.php?sub=$1&second=$2 [NC]

我正在尝试添加一些像这样的静态规则:

RedirectMatch 302 /construction/pools.html http://www.{samedomain}.com/construction-services/pools

问题是当我在地址栏中键入以下内容时:

http://www.{samedomain}.com/construction/pools.html

然后apache重定向到:

http://www.{samedomain}.com/construction-services/pools?sub=construction&second=pools.html

我想要的是apache重定向到:

http://www.{samedomain}.com/construction-services/pools

有人知道为什么吗?

谢谢你。

4

2 回答 2

1

重定向按照它们出现的顺序进行处理,因此将静态重定向放在RewriteRule. 不要[L]在你的RewriteRule.

RewriteEngine On
# Match the static redirect first
RedirectMatch 302 /construction/pools.html http://www.{samedomain}.com/construction-services/pools

# Since your dynamic URLs don't end in .html, avoid those with RewriteCond
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule ^([^/]+)/(.*)$ /rewrite.php?sub=$1&second=$2 [NC,L]

或者,RewriteCond如果您的动态 url 都没有.

RewriteRule ^([^/]+)/([^.]+)$ /rewrite.php?sub=$1&second=$2 [NC,L]
于 2013-01-17T12:33:34.540 回答
1

根据优先级编写htaccess规则(把有共同行为的规则放在最后)

在你的情况下

RedirectMatch 302 /construction/pools.html http://www.{samedomain}.com/construction-services/pools
RewriteRule ^(.*)/(.*)$ /rewrite.php?sub=$1&second=$2 [NC]
于 2013-01-17T12:36:59.577 回答