今天想为blog加一个重定向,访问主域名直接定向到blog目录,即主域名绑定子目录。之前采用index.html里的 meta和window.location ,好像对SEO不好,而且有明显的转向等待时间。所以搜索了一下其他解决方案。找到如下代码,需要借助
htaccess文件。
# .htaccess main domain to subdirectory redirect
# Copy and paste the following code into the .htaccess file
# in the public_html folder of your hosting account
# make the changes to the file according to the instructions.
# Do not change this line.
RewriteEngine on
# Change yourdomain.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$
# Change ‘subdirectory’ to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/subdirectory/
# Don’t change this line.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change ‘subdirectory’ to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /subdirectory/$1
# Change yourdomain.com to be your main domain again.
# Change ‘subdirectory’ to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$
RewriteRule ^(/)?$ subdirectory/index.php [L]
.htaccess指向index.php 最末一句使得首页访问yourdomain.com指向 blog的index.php
修改之后,再通过youdomain.com访问会直接跳转(并非URL跳转)到blog目录,如果blog是采用wordpress的话,如果想以后的链接URL中没有/blog/ 的话可以在后台Setting里的General 里修改URL 为youdomain.com,这样绑定就非常完美的完成了。
最新评论