24

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 :/

anubhava
  • 761,203
  • 64
  • 569
  • 643
Shark5060
  • 275
  • 1
  • 2
  • 6

3 Answers3

48

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This should work irrespective of `http` or `https`. Make sure `https` URL is also going to same htaccess – anubhava Jan 31 '15 at 14:49
  • It works for me only if there is a slash. eg domain.com/demo/ but without slash domain.com/demo, I got domain.com/domain.com/demo/ Any idea? – Adrian Rodriguez Oct 07 '16 at 20:12
  • Comments are not good section for answering but you can keep a trailing slash fix rule before this rule. – anubhava Oct 07 '16 at 20:21
  • What if it works/redirects perfectly EXCEPT you don't want the "subdir" text showing in the URL in the address bar? Any way to do that? – user3035649 Apr 18 '19 at 09:31
  • This rule doesn't do any redirect so `subdir` won't show in browser after rewrite. – anubhava Apr 18 '19 at 10:46
0

Below code works perfectly for me.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/folder_name/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder_name/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ folder_name/index.html [L]
Rohit Parte
  • 3,365
  • 26
  • 26
0

Show subdirectory pages without changing url in root domain

Change according to your need subfolder_name in code

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteRule ^(.*)$ /subfolder_name/$1

</IfModule>
Vishal Vaghasiya
  • 1,857
  • 2
  • 14
  • 21