问题
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:

回答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
回答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]
来源:https://stackoverflow.com/questions/14967934/use-htaccess-to-mask-domain-and-folder-name