Use htaccess to mask domain and folder name

13,130

Solution 1

You don't need to escape -, except inside a character class. And even there, you can have it unescaped as the first or last character.

In a RewriteRule, the pattern is tested against the URL-path and not the domain. If you need to test against the domain, you can use a RewriteCond

RewriteCond %{HTTP_HOST} !example-2.com
RewriteRule ^/?sub/sub-sub/(.*) http://example-2.com/$0 [R,L]

RewriteCond %{HTTP_HOST} example-2.com
RewriteCond %{REQUEST_URI} !^/?sub/sub-sub
RewriteRule .* sub/sub-sub/$0

Solution 2

You may try this:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  !^/sub/sub-sub         [NC]
RewriteCond %{REQUEST_URI}  ^/(.*)                 [NC]
RewriteRule .*   http://example.com/sub/sub-sub/%1 [L]

Redirects permanently

http://example-2.com/anything

To:

http://example.com/sub/sub-sub/anything

All string are assumed to be fixed, except the segment path anything.

For permanent an visible redirection, replace [L] with [R=301,L].

The above rule-set must be included in one .htaccess file in http://example-2.com root directory.

UPDATE:

To skip the rule for any incoming URL without a path, like http://example-2.com/, replace this line:

RewriteCond %{REQUEST_URI} ^/(.*) [NC]

with this one:

RewriteCond %{REQUEST_URI} ^/(.+) [NC]

Share:
13,130
elli mccale
Author by

elli mccale

Updated on June 04, 2022

Comments

  • elli mccale
    elli mccale almost 2 years

    I'm trying to use htaccess and mod_rewrite to mask one domain and two of its sub-folders with another domain. For example: http://example.com/sub/sub-sub/ to http://example-2.com/ So that http://example-2.com/ is what shows in the browser address bar, but the content of http://example.com/sub/sub-sub/ is what's displaying on the page.

    I found this question/answer which should accomplish this, but it doesn't work when I implement it.

    Current htaccess:

    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^/(.+) [NC]
    RewriteRule .*   http://example.com/sub/sub-sub/%1 [L]
    
    Options -Multiviews
    

    Here's a view of my directory:

    http://i.imgur.com/nihz1al.png

  • elli mccale
    elli mccale about 11 years
    This works, but seems to create an infinite loop when I visit http://example-2.com directly (http://example.com/sub/sub-sub/sub/sub-sub/sub/sub-sub/sub/‌​sub-sub/sub/sub-sub/ etc). Also, now when I visit http://example.com/sub/sub-sub/anything, I get a 404 error. It's because I'm using another rewrite rule to remove .php from pages. I've edited my original question with my current htaccess.
  • elli mccale
    elli mccale about 11 years
    This works in that when I visit http://example-2.com/page, it displays correctly in the browser and address bar. But I also want it to display as http://example-2.com/page in the address bar when I visit http://example.com/sub/sub-sub/page. So no matter what domain I use, http://example-2.com always shows in the address bar, but the content of http://example.com/sub/sub-sub always displays on the page. Does that make sense? It's hard to explain.
  • Felipe Alameda A
    Felipe Alameda A about 11 years
    Switch the sets of rules position. The loop is not generate by the set in my answer and it does what you asked for.
  • Olaf Dietsche
    Olaf Dietsche about 11 years
    @ElliPetersen I added an external redirect, for all domains not example-2.com. Please check, if this works for you.
  • elli mccale
    elli mccale about 11 years
    I completely removed the other set of rules, and now this works, though visiting http://example-2.com/page redirects in the address bar to http://example.com/sub/sub-sub/page. I tried changing [R=301,L] to [L] but that created the same redirect problem. I've updated my current htaccess for http://example.com/sub/sub-sub/ in the main question. I probably wasn't clear enough in what I'm wanting to do. See my comment on the answer below for what I hope is a better explanation.
  • elli mccale
    elli mccale about 11 years
    Now when I visit http://example-2.com/page it redirects to http://example.com/sub/sub-sub/page in the address bar too. So it's almost there! Now we just need to make http://example-2.com/page stay in the address bar. I'm sorry if this is frustrating, I don't understand htaccess very well.
  • Felipe Alameda A
    Felipe Alameda A about 11 years
    @Elli I don't quite understand. What do you want to do when the URL is just http://example-2.com/, leave it like that without redirection? If so, just replace ^/(.*) with this ^/(.+)
  • elli mccale
    elli mccale about 11 years
    Correct, because http://example-2.com/ already points to http://example.com/sub/sub-sub/ via server settings. I replaced that line, but it's still creating a loop. I've updated my current htaccess in the main question.
  • Felipe Alameda A
    Felipe Alameda A about 11 years
    OK. I will update my answer with another condition to try to prevent loops. What you asked makes sense and I should have realized that any URL without path should not be redirected.
  • Felipe Alameda A
    Felipe Alameda A about 11 years
    @Elli Updated it. If necessary, please fill in the correct folder names in the new condition in my answer: RewriteCond %{REQUEST_URI} !^/sub/sub-sub [NC]
  • Olaf Dietsche
    Olaf Dietsche about 11 years
    @ElliPetersen I forgot another RewriteCond to prevent an endless loop.
  • Olaf Dietsche
    Olaf Dietsche about 11 years
    @ElliPetersen I just looked at your current .htaccess. The difference between your and my RewriteRule is, I do an internal and you do an external rewrite. If you omit http://example.com/, http://example-2.com/page should stay in the browser's address bar. It does so in my test environment.
  • elli mccale
    elli mccale about 11 years
    Now visiting http://example-2.com/ goes to http://example.com/sub/sub-sub/index.html and creates a 404. Thank you so much for your help and patience.
  • Felipe Alameda A
    Felipe Alameda A about 11 years
    Well, it goes where the server setting is redirecting it: "because http://example-2.com/ already points to http://example.com/sub/sub-sub/ via server setting" . You should try to combine all rules into one set. The loop you had was never originated by the rules in my answer, but by those previous conflicting rules.
  • Felipe Alameda A
    Felipe Alameda A about 11 years
    +1 @OlafDietsche I still don't understand why ends up in another domain as the question requires, but obviously you did. Good analysis.
  • Olaf Dietsche
    Olaf Dietsche about 11 years
    @faa Often, this evolves with the question and especially the answers. And sometimes, you can figure it out from the comments. And again, with the answers the OP sometimes realizes, what she really needs. But I'm still barking up the wrong tree often enough. ;-)