URL rewrite with .htaccess for wildcard domain

Try putting some rewrite rules like this in your .htaccess file:

RewriteEngine on
RewriteBase /

# Make sure it's not an actual file or 
# directory, being accessed. If you wish.
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d

# Match the subdomain
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$

# Rewrite the request to the main domain:
# %1: The subdomain matched against HTTP_HOST in the previous RewriteCond.
# $1: The request URI.
RewriteRule ^(.*)$ https://example.com/fixed/%1/$1 [L,R,QSA]

Feel free to drop the L flag, if it’s not the last rewrite rule. See RewriteRule Flags for more information on this.

You can test it online.

Leave a Comment