您可以检查主机,然后使用mod_rewrite在文件中创建 301 重定向来处理此问题.htaccess
;尽管如果您有访问权限,最好在httpd.conf
配置文件或包含的配置文件中执行此操作。
最佳场景
由于看起来mod_cband需要为每个域使用不同的虚拟主机,因此您可以httpd.conf
像这样设置文件并在配置本身中包含重写规则。一些网络主机将执行此方法,其中主帐户站点是DocumentRoot
,其他站点都嵌套在它的目录下:
<VirtualHost *:80>
ServerName www.mywebsite.com
DocumentRoot /home/mywebsite/
RewriteEngine on
RewriteRule ^/files/videos/(.*)$ http://video.mywebsite.com/$1 [R=301,L]
RewriteRule ^/files/images1/(.*)$ http://image1.mywebsite.com/$1 [R=301,L]
RewriteRule ^/files/images2/(.*)$ http://image2.mywebsite.com/$1 [R=301,L]
</VirtualHost>
<VirtualHost *:80>
ServerName video.mywebsite.com
DocumentRoot /home/mywebsite/files/video/
</VirtualHost>
<VirtualHost *:80>
ServerName image1.mywebsite.com
DocumentRoot /home/mywebsite/files/images1/
</VirtualHost>
<VirtualHost *:80>
ServerName image2.mywebsite.com
DocumentRoot /home/mywebsite/files/images2/
</VirtualHost>
亚军
如果您使用的是托管服务提供商,您无权访问httpd.conf
文件,并且他们没有将域设置alias
为主域(每个域都有一个单独的文件夹),那么您将编写规则在这样的根.htaccess
中www.mywebsite.com
:
RewriteEngine On
RewriteRule ^(files/videos/.*)$ http://video.mywebsite.com/$1 [R=301,L]
RewriteRule ^(files/images1/.*)$ http://image1.mywebsite.com/$1 [R=301,L]
RewriteRule ^(files/images2/.*)$ http://image2.mywebsite.com/$1 [R=301,L]
大多数开销
如果他们使用别名(所有内容都具有完全相同的文档根目录),那么您需要使用.htaccess
所有人通常制定的文件检查请求的主机名:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^video.mywebsite.com$
RewriteRule ^(files/videos/.*)$ http://video.mywebsite.com/$1 [R=301,L]
#Check to make sure if they're on the video domain
#that they're in the video folder otherwise 301 to www
RewriteCond %{HTTP_HOST} ^video.mywebsite.com$
RewriteCond %{REQUEST_URI} !^/files/videos [NC]
RewriteRule ^.*$ http://www.mywebsite.com/ [R=301,L]
RewriteCond %{HTTP_HOST} !^image1.mywebsite.com$
RewriteRule ^(files/images1/.*)$ http://image1.mywebsite.com/$1 [R=301,L]
#Check to make sure if they're on the image1 domain
#that they're in the images1 folder
RewriteCond %{HTTP_HOST} ^image1.mywebsite.com$
RewriteCond %{REQUEST_URI} !^/files/images1 [NC]
RewriteRule ^.*$ http://www.mywebsite.com/ [R=301,L]
RewriteCond %{HTTP_HOST} !^image2.mywebsite.com$
RewriteRule ^(files/images2/.*)$ http://image2.mywebsite.com/$1 [R=301,L]
#Check to make sure if they're on the image1 domain
#that they're in the images2 folder
RewriteCond %{HTTP_HOST} ^image2.mywebsite.com$
RewriteCond %{REQUEST_URI} !^/files/images2 [NC]
RewriteRule ^.*$ http://www.mywebsite.com/ [R=301,L]