Redirecting Subdirectory to Subdomain with htaccess

。_饼干妹妹 提交于 2019-12-19 03:41:17

问题


I'm relatively new to using .htaccess, and have never done any coding besides what I've read online. I'm using Bluehost, and I'd like to redirect my blog subdirectory to a subdomain. Example: I'd like to redirect www.example.com/blog to blog.example.com.

I already have code in place to always add www. to the beginning of my blog address in the root folder, but I don't know how to accomplish the above redirect by using code in .htaccess. Any help would be appreciated!


回答1:


A lot of web hosts today provide an easy implemention for subdomain creation in their administration panels. You just need to to go there, choose you subdomain name, and then point it to a directory in your tree.

If you can't, then it will be a little more complicated (You will need to resolve that subdomain to your server ip, configure some virtual hosts ... etc) and you may not have enough privileges to do that (unless you are on a dedicated server).

Edit 2

To redirect requests to www.example.com/blog to blog.example.com, try this :

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [L,QSA,R=301]

RewriteCond %{HTTP_HOST} ^blog\.example\.com$
RewriteCond %{REQUEST_URI} !^blog/
RewriteRule ^(.*)$ /blog/$1 [L,QSA]



回答2:


Have you tried this one?

RewriteEngine on
RewriteBase /
RewriteRule ^/blog/(.*)$ http://blog.subdomain.com/$1 [R=301,L]



回答3:


I wanted to add my two cents,

1) to answer the question above, this rewrite should fix it:

RewriteEngine on
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^/blog$ http://blog.example.com [R=302,L]

2) but, I think this is not enough by itself, you also need to change DNS, so that blog.example.com is pointed at the right server, and this can be done by a cname similar to this:

blog.example.com  CNAME example.com TTL 1080

(not exactly how it will look, but use your DNS webinterface to set this up).




回答4:


RewriteCond %{HTTP_HOST} ^check.domain.info$

RewriteCond %{REQUEST_URI} !^/check/

RewriteRule (.*) /check/$1



来源:https://stackoverflow.com/questions/12415896/redirecting-subdirectory-to-subdomain-with-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!