繁体   English   中英

htaccess将静态子域重定向到域

[英]Htaccess redirect static sub-domain to domain

我为图像创建了一些静态子域:

www.static1.domain.com
www.static2.domain.com

现在,我想将不是图像的文件从静态域重定向到www.domain.com,以避免重复的内容。 我的htaccess中有以下规则(不存在的文件被无提示地重定向到index.php):

#Redirect static to main
RewriteCond %{HTTP_HOST} static([0-9]+)\.
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !\.(js|css|png|jpg|jpeg|gif)$ [NC]
RewriteRule (.*) http://www.domain.com%{REQUEST_URI} [R=301,L]

#Redirect non exisitng files to index.php (silent)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

重定向工作正常。 但是,如果我输入了一个不存在的图像,例如http://www.static1.domain.com/test.gif ,则会重定向到http://www.domain.com/index.php

test.gif的重定向应该是对索引php的无提示重定向 ……我在做什么错?

感谢您的提示。

我不确定我是否理解正确。 通过无声重定向到您的意思是“ index.php”不应出现在URL栏中,或者您是说URL栏仍应显示为“ test.gif”,但页面应呈现index.php?

您不能添加多个RewriteCond行。 仅最后一个适用于RewriteRule

因此,此行没有任何效果:

RewriteCond %{REQUEST_FILENAME} -f

这就是为什么不存在的图像也会被重定向的原因。

怎么样:

#Redirect non exisitng files to index.php (silent)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]

#Redirect static to main
RewriteCond %{REQUEST_FILENAME} !\.(js|css|png|jpg|jpeg|gif)$ [NC]
RewriteRule (.*) http://www.domain.com%{REQUEST_URI} [R=301,L]

用这种方式做到了。 找不到http://www.static1.domain.com/test.gif时,引发默认服务器404页面。 不是最好,但是很好。

RewriteCond %{HTTP_HOST} !static([0-9]+)\.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

RewriteCond %{HTTP_HOST} static([0-9]+)\.
RewriteCond %{REQUEST_FILENAME} !\.(js|css|png|jpg|jpeg|gif)$ [NC]
RewriteRule (.*) http://www.domain.com%{REQUEST_URI} [R=301,L]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM