Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

regex - .htaccess redirect domain to subdirectory without changing URL

I have the following setup: -> domain1.com is my main domain and also document root -> domain2.com is another domain which is registered as an alias domain (means it does exactly the same as domain1.com), I have multiple of those domains.

when I now want to redirect that domain to something I usually use this to redirect the domain to something else (like an external website):

RewriteCond %{HTTP_HOST} ^(www.)?domain2.com$ [NC]
RewriteRule ^(.*) http://somewiredisp.com/user/bla/$1 [L,R]

this is usually used if I register a domain for someone who needs a nice url for their forums or whatever. Works like a charm - the only "problem" would be that the address bar in the browser changes, but I've read that there would be no way to do that with external URLs, and since no one ever complained about it I am fine with it.

However, now I would like to link some url to a subdirectory WITHOUT the url changing in the address bar.

domain2.com -> domain1.com/subdir (or domain2.com/subdir - that doesn't matter, since it's an alias domain).

my current approach would be

RewriteCond %{HTTP_HOST} ^(www.)?domain2.com$ [NC]
RewriteRule ^(.*) http://domain1.com/subdir/ [P]

which doesn't work (Error 404) - if I call domain1.com/subdir/ directly if works obviously.

I also tried several variations of

RewriteRule ^(.*) http://domain2.com/subdir/ [P]
RewriteRule ^(.*) /subdir/ [P]
RewriteRule ^(.*) http://domain1.com/subdir/index.html [P]

all with the same result.

maybe it's just a simple mistake but I am currently clueless :/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use this rule:

RewriteCond %{HTTP_HOST} ^(www.)?domain2.com$ [NC]
RewriteRule !^subdir/ /subdir%{REQUEST_URI} [L,NC]

This will prefix every request for domain2.com with /subdir if it is not already there.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...