如何重定向不存在的子域? [英] How do I redirect subdomains that do not exist?
问题描述
我想在下面的方式使用的.htaccess重定向。我不是所有熟悉的.htaccess,所以我不知道这是可以做到。另外,我不知道我是如何打算这样做遵循最佳实践的搜索引擎优化。
I am trying to redirect using .htaccess in the following fashion. I am not all that familiar with .htaccess, so I'm not sure it can be done. Also, I don't know if how I am intending to do it follows best practices for SEO.
www.domain.com > domain.com 301
ks.domain.com > kansas.domain.com 301
ia.domain.com > iowa.domain.com 301
domain.com/sites > domain.com 301
domain.com/sites/iowa > iowa.domain.com 301
nonexistent.domain.com > domain.com 302
domain.com/sites/nonexistent > domain.com 302
我最大的问题是,如果我能发现一个不存在的子域名和重定向。我很想看到所有的上述完成。
My biggest question is if I can detect a nonexistent subdomain and redirect. I would love to see how all of the above is accomplished.
推荐答案
首先,你需要创建一个子站点的增加通配符子域 *
正如其名,只如果你的虚拟主机允许你这样做。而这必须在你的的.htaccess
,尝试测试它,看它是否工作:
First, you need to add wildcard subdomains by creating a subdomain with an *
as its name, only if your web host allows you to do so. And this must be in your .htaccess
, try to test it to see if it works:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.com
RewriteRule ^(.*)$ http://domain.com/$1 [R=301]
RewriteCond %{HTTP_HOST} ^ks\.domain\.com
RewriteRule ^(.*)$ http://kansas.domain.com/$1 [R=301]
RewriteCond %{HTTP_HOST} ^ia\.domain\.com
RewriteRule ^(.*)$ http://iowa.domain.com/$1 [R=301]
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteCond %{REQUEST_URI} ^/sites/?$
RewriteRule ^(.*) / [R=301]
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteCond %{REQUEST_URI} ^/sites/iowa/?$
RewriteRule ^(.*) http://iowa.domain.com/ [R=301]
RewriteCond %{HTTP_HOST} ([a-z0-9-]+)\.domain\.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) http://domain.com/ [R=302]
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteCond %{REQUEST_URI} ^/sites/([a-z0-9-_]+)/?
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*) http://domain.com/ [R=302]
只需使用 -f
来测试,如果一个请求的文件存在且是一个普通文件, -S
如果存在且文件大小大于0和 -d
来测试是否存在且是一个目录。
Just use -f
to test if a requested file exists and is a regular file, -s
if it exists and has a file size greater than 0 and -d
to test if it exists and is a directory.
这篇关于如何重定向不存在的子域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!