从与评论沿着你的问题,似乎要重定向从有和没有www
到page.php?code=anything after
example.co任何要求,如果这是正确的,那么你可以尝试:
# 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,它将无法工作。
同样适用于子域到主域或其他域或其他子域。
简而言之,您只能将从问题域内部重定向到本身。
域名更改意味着重定向,因为浏览器必须使用不同的HTTP主机发出新的请求。 –
那么这是否意味着无法重写浏览器地址? –
我在下面删除了我的答案 - 你能更详细地描述你的情况吗?你有'.co'域和'.com'域。除非它们的文件驻留在同一个虚拟主机文档根目录中,否则不能在它们之间静默地重写。 –