Apache - Rewrite dynamic subdomains and URL parame

2019-08-16 19:20发布

问题:

I have a problem with rewrite condition and rule in Apache2. I want to have dynamic subdomains and dynamic folders, which will rewrite to php script. For example:

sub1.example.com/folder1  => do.php?a=sub1&b=folder1
sub2.example.com/folder2  => do.php?a=sub1&b=folder2

It will looks like every subdomain has own folders. But some subdomain (admin etc) will not be rewrited.

I know how to rewrite subdomain:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(^.*)\.example.com/ [NC]
RewriteRule ^(.*)$ do.php?a=%1 [L,P]

but I don't know, how to do dynamic folders in subdomains.

Thanks a lot

回答1:

Maybe this example can help you:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/?do\.php$
RewriteRule .? do.php?url=%1&path=%{REQUEST_URI} [L,QSA]