htaccess重写单个+多个URL

时间:2017-12-23 01:17:32

标签: .htaccess redirect mod-rewrite

我需要从一个域重定向到另一个域:一些特定的重定向+一般规则:

特定重定向:

subdomain.example.com/ => example.com/subdomain/
subdomain.example.com/page1 => example.com/subdomain/page1

一般规则 - 应适用于未使用上述规则捕获的所有网址:

subdomain.example.com/* => example.com/*

以下是我现有的规则 - 问题是它们似乎互相排斥:

RewriteCond %{HTTP_HOST}  ^subdomain\.example\.com [NC]
RewriteRule /?   http://example.com/subdomain/ [R=301,L]

RewriteCond %{HTTP_HOST}  ^subdomain\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$   http://example.com/$1 [R=301,L]

我做错了什么

1 个答案:

答案 0 :(得分:1)

您的RewriteRule模式不正确,您应将/?^/?$括起来,否则它会与所有网址匹配。

^:行首

$:行尾

# first specific rule
RewriteCond %{HTTP_HOST}  ^subdomain\.example\.com [NC]
RewriteRule ^/?$   http://example.com/subdomain/ [R=301,L]

# second specific rule
RewriteCond %{HTTP_HOST}  ^subdomain\.example\.com [NC]
RewriteRule ^page1$   http://example.com/subdomain/page1 [R=301,L]