htaccess rewrite to sub-domain

不问归期 提交于 2019-12-24 14:37:03

问题


Hi I have a independent site that sits within a sub-directory of a main server.

All links within the sub-directory site point to the root and I'd like to capture those and send them back to the sub-directory rather than the root.

Is this possible with htaccess?


回答1:


This will not redirect to the subdir, but make the other domain behave as if the subdir is the document-root. Which I think is even better.

RewriteEngine on
RewriteBase     /

RewriteCond %{HTTP_HOST} ^seconddomain.nl$ [NC]
RewriteCond $1 !^subfolder
RewriteRule ^(.*)$ /subfolder/$1 [L]



回答2:


As @Gerben pointed out (whose answer you should totally go and upvote right now) you can make the other domain behave as if the subdir is the document-root like this:

RewriteEngine on
RewriteBase     /

RewriteCond %{HTTP_HOST} ^seconddomain.example.com$ [NC]
RewriteCond $1 !^subdir
RewriteRule ^(.*)$ /subdir/$1 [L]

Another way to do what you are asking about is to use a <base> tag in your HTML documents. One advantage of this is that you don't have extra .htaccess processing for every single document served by the web server (including all documents not in the subdirectory/subdomain).

If you want to do it inside .htaccess and have it redirect regardless of whether or not it is the subdomain, it would look this:

RewriteEngine on
RewriteCond %{PATH_INFO} !^subdir/
RewriteRule ^(.*)$ subdir/$1 [L]


来源:https://stackoverflow.com/questions/8617302/htaccess-rewrite-to-sub-domain

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