Mod ReWrite Catch-All Subdomain

Basically I want * .domain.com to pull out domain.com/*/(not redirect).

I went into the cPanel subdomains section and installed a template for this domain. It seems that it resolves correctly, i.e. * .domain.com brings up domain.com.

Now I created a htaccess file in the public_html directory containing:

Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
RewriteRule (.*) %2/$1 [L]

The error I get is "500 Internal Server Error", any ideas?

+3
source share
1 answer

Try the following rule:

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com$ [NC]
RewriteCond %2%{REQUEST_URI} !^([^/]+)/\2
RewriteRule (.*) %2/$1 [L]

Extra RewriteCond %2%{REQUEST_URI} !^([^/]+)/\2must avoid endless loops caused by the flag L.

0
source

Source: https://habr.com/ru/post/1710836/


All Articles