用.htaccess重写将主域名指向一个子目录

分类:网络 标签:无


代码当然是网上找的,不过亲测可用。
# .htaccess main domain to subfolder 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.)?yourmaindomain.com$
# 这里将yourmaindomain.com改成你的域名,比如我的是wuxue.ml
# Change ’subfolder’ to be the folder you will use for your main domain.

RewriteCond %{REQUEST_URI} !^/subfolder/
#这里将subfolder改成你的子目录,比如wordpress或者bbs
# Don’t change this line.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Change ’subfolder’ to be the folder you will use for your main domain.

RewriteRule ^(.*)$ /subfolder/$1
#这里将subfolder改成你的子目录,比如wordpress或者bbs
# Change yourdomain.com to be your main domain again.
# Change ’subfolder’ to be the folder 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.)?yourmaindomain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
#这里第一句将yourmaindomain.com改成你的域名,比如我的是wuxue.ml
#第二句是设立默认文件,比如去掉论坛域名的后缀名forum.php,就需要将subfolder/index.php改成bbs/forum.php
#如果是wordpress就改成wordpress/index.php

最后附上不带www域名301跳转到www上,
#301 redirect www 
RewriteEngine On
RewriteCond %{HTTP_HOST} ^wuxue.ml [NC]
RewriteRule ^(.*)$ http://www.wuxue.ml/$1 [L,R=301]
#将我的域名改为你自己的
如果要将带www的域名跳到不带www的上,将代码中的两个网址换个顺序即可。
你可能还会对下列文章感兴趣:

0条评论 你不想来一发么↓

    想说点什么呢?

    您需要登录您的Google账号才能进行评论。