在我的.htaccess中有一个redirect规则,使用以下模式将代理从A.html转发到B.html :
Redirect 301 /A.html http://mysite.com/B.html
由于Redirect指令需要设置目标主机,是否可以让这个规则只在特定主机上占上风? 我有一个testing和部署域,只需要在部署域。 我可以为Rewrite规则设置HTTP条件,但是我怎么能为HTTP Redirect ?
我来到了解决scheme,它不能用纯粹的Redirect指令。 如果需要在某些HTTP条件下redirect访问者,则必须使用Rewrite规则。
你实际上可以做到这一点,但你确实失去了301redirect代码。
SetEnvIfNoCase Host "myotherdomain.com" redirectthisdomain <Files "A.html"> Order Deny,Allow Deny from env=redirectthisdomain ErrorDocument 403 http://mysite.com/B.html </Files>
只需将redirect规则放置在部署域的虚拟主机configuration中即可。 这样它只会对这个主机有效。
我的情况是:两个域(domain.org和domain.info)指向同一个网站,我想要.info是使用,而不是.org
该网站是用PHP编写的,在每个页面的开头都有这样的东西
if (stristr($_SERVER['SERVER_NAME'], 'domain.org') === FALSE) { // do standard initial operations } else { // insert here the code to rebuild the correct path and put it in $newpath header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.domain.info/$newpath"); }
通过这种方式,您可以向用户和search引擎search器通知您的configuration。
一个简单的例子,如果访问者去dev.example.com/A.html然后他们被redirect到B.html。 如果他们访问www.example.com/A.html,那么他们会被redirect到C.html; 与301redirect。
RewriteEngine on RewriteCond %(HTTP_HOST) ^dev.example.com$ [NC] RewriteCond %(REQUEST_URI) ^/A.html RewriteRule ^(.*)$ http://dev.exmaple.com/B.html [R=301] RewriteCond %(HTTP_HOST) !^dev.example.com$ [NC] RewriteCond %(REQUEST_URI) ^/A.html RewriteRule ^(.*)$ http://dev.exmaple.com/C.html [R=301]
您可以在Apache网站上阅读更多关于Mod_Rewrite的信息。