.htaccess rewrite multiple urls without redirects

2019-07-27 07:15发布

问题:

Ok so i have urls like these demo1.domain.net, demo2.domain.net, demo3.domain.net and all needs to show the contents of a subfolder like this example demo1.domain.net will show the contents of domain.net/websites/demo1 i need to do it in a way that all the domains like $whatever.domain.net into domain.net/websites/$whatever.domain.net, i already enabled wildcard dns, and i cant seem to fiqure it out, please keep in mind i dont want it to redirect.

Here is what i tried

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?[^.]+\.domain\.net.*$
RewriteRule /websites/$1 [L]

I just get the main page of domain.net with this.

Your help will be greatly appreciated.

回答1:

Close, try this:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/websites/
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+\.domain\.net.*)$ [NC]
RewriteRule ^(.*)$ /websites/%2/$1 [L]

Note that this will only work if the all the demo*.domain.net point to the same document root as the main domain (domain.net).



回答2:

I think, I can manage this! Could you please try these .htaccess directives AGAIN:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.net$
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.net$
RewriteRule ^/?$ http://domain.net/websites/%2 [P]

It will rewrite $whatever.domain.net into domain.net/websites/$whatever.

NOW if it's suppose to be $whatever.domain.net into domain.net/websites/$whatever.domain.net then you could try this one:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.net$
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.net$
RewriteRule ^/?$ http://domain.net/websites/%2.domain.net [P]

Or maybe this code below is what you've wanted in the future:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.net$
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.net$
RewriteRule ^(.*)$ http://domain.net/websites/%2.domain.net/$1 [P]