Can you set up mod_rewrite for wildcard domains in Apache?
Linode
Linode Staff
What's the best way to set up a rewrite to change a subdomain, such as sub.example.com into a page such as example.com/sub?
3 Replies
mjones
Linode Staff
I'm by no means an expert, but following the official Apache mod_rewrite documentation and examples, it looks like you can accomplish this in one of two ways:
With mod_rewrite:
RewriteEngine on
RewriteRule "^(.*).example.com" "http://example.com/$1" [R,L]
Or with RedirectMatch:
RedirectMatch "^(.*).example.com" "http://example.com/$1"
It may be there's some additional nuances in a specific case that would require different rules or syntax, so I'd recommend reviewing the official documentation when writing any mod_rewrite rules or conditions.