1

我有两个域 - example.com 和 example.co。两个域都在同一个虚拟主机包上,并且两个文档根都是“public_html”

我想将 example.co(例如 example.co/abc123)之后的任何字符串重写为https://www.example.com/page.php?url=TEXT-HERE

这是我的 htaccess,但它似乎是重定向而不是重写。

RewriteCond %{HTTP_HOST} ^(www\.)?example\.co$ [NC]
RewriteRule ^(.*)$ https://www.example.com/page.php?code=$1
4

1 回答 1

1

从您的问题以及评论来看,您似乎想将来自 example.co 的任何请求重定向wwwpage.php?code=anything after,如果是正确的,那么您可以尝试:

# match example.co with and without www
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co$ [NC]
# make sure we don't redirect page.php
RewriteCond %{REQUEST_FILENAME} !page\.php
# internally redirect anything received to page.php as query string to code
RewriteRule ^(.*)$ /page.php?code=$1 [L]

这将在内部重定向,因此用户仍将看到域example.co

鉴于您仍然想使用 HTTPS,如果它还没有,您可以进一步使用它,而不是上面的规则:

# if it does not start with WWW we redirect to www.domain
# make sure the domain.co is enclosed by parenthesis like below
RewriteCond %{HTTP_HOST} !^www\.(example\.co)$ [NC]
# we use this to make sure we are redirecting the right domain
# in case of multiple domains
RewriteCond %{HTTP_HOST} example\.co$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]

# if HTTPS is not being used we force it to HTTPS
RewriteCond %{HTTPS} !=on
# because we want to force it for this domain only
RewriteCond %{HTTP_HOST} ^example\.co$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# and now we finally do the internal redirect
# match example.co, we don't need to match the www anymore
RewriteCond %{HTTP_HOST} ^example\.co$ [NC]
# here we check if the file on the URL is page.php
# we don't want it redirect or we may fall into a loop
RewriteCond %{REQUEST_FILENAME} !page\.php
# internally redirect anything received to page.php as query string to code
RewriteRule ^(.*)$ /page.php?code=$1 [L]

否则,您拥有的规则应该可以正常工作:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.co$ [NC]
RewriteRule ^(.*)$ https://www.example.com/page.php?code=$1 [R=302,L]

当域不同或协议不同时,它会产生类似的重定向,或者不是它的工作原理。

因此,如果您尝试在内部从域 A 重定向到域 B,它将无法正常工作。

同样适用于子域到主域或其他域或其他子域。

简单地说,您只能在内部从相关域重定向到自身

于 2013-08-19T20:37:38.377 回答