仅允许域使用htaccess进行某些扩展

时间:2012-10-27 12:27:11

标签: .htaccess

我有一个域名abc.com

带子域名

abc.com
www.abc.com
img.abc.com

所有指向我服务器上的同一文件夹。

由于我打算将img.abc.com用作cdn,因此需要阻止img.abc.com/pqr.html与www.abc.com/pqr.html重复。

abc.com/* should redirect to www.abc.com/*
img.abc.com/*.jpg/gif/png should be allowed
[img.abc.com/*.other extension] should be redirected to [www.abc.com/*.other extension]

可能包含哪些可能的htaccess规则?

1 个答案:

答案 0 :(得分:0)

将这些规则添加到文档根目录中的htaccess文件中:

RewriteEngine On

# abc.com/* should redirect to www.abc.com/*
RewriteCond %{HTTP_HOST} ^abc\.com$ [NC]
RewriteRule ^(.*)$ http://www.abc.com/$1 [L,R]

# img.abc.com/*.jpg/gif/png should be allowed
RewriteCond %{HTTP_HOST} ^img\.abc\.com$ [NC]
RewriteRule \.(jpe?g|png|gif)$ - [L,NC]

# [img.abc.com/*.other extension] should be redirected to [www.abc.com/*.other extension]
RewriteCond %{HTTP_HOST} ^img\.abc\.com$ [NC]
RewriteRule ^(.*)$ http://www.abc.com/$1 [L,R]