2013-08-19 100 views
1

我有两个域 - example.com和example.co。这两个域在同一个虚拟主机包,这两个文件根“的public_html”Htaccess重定向不改写

我想重写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 
+1

域名更改意味着重定向,因为浏览器必须使用不同的HTTP主机发出新的请求。 –

+0

那么这是否意味着无法重写浏览器地址? –

+0

我在下面删除了我的答案 - 你能更详细地描述你的情况吗?你有'.co'域和'.com'域。除非它们的文件驻留在同一个虚拟主机文档根目录中,否则不能在它们之间静默地重写。 –

回答

1

从与评论沿着你的问题,似乎要重定向从有和没有wwwpage.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,它将无法工作。

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

简而言之,您只能将从问题域内部重定向到本身

+0

辉煌 - 解决了这个问题。非常感谢你。 –

+0

@JasonLipo不客气。 – Prix

+0

@JasonLipo:很高兴您的问题得到解决,但根据您希望在重定向的URL中输入https:// www.example.com的问题。只记得内部转发不会强制https。 – anubhava