Last active
November 21, 2020 18:38
-
-
Save shijij/e08946a9e989b7fd1e69e440519af4ff to your computer and use it in GitHub Desktop.
Apache 2.4. Map subdomain to directory with mod_rewrite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RewriteEngine On | |
RewriteMap lc int:tolower | |
RewriteCond %{HTTP_HOST} ^([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)$ | |
RewriteRule ^.*$ - [R=404,L] | |
RewriteCond %{REQUEST_URI} \/icons\/.* | |
RewriteRule ^.*$ - [L] | |
RewriteCond %{HTTP_HOST} ^([^.]*)\.([^.]*)\.([^.]*)$ | |
RewriteRule ^(.*)$ /var/www/${lc:%2}\.${lc:%3}/${lc:%1}/$1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This rewrite will map subdomain to directory | |
# For example: | |
# 123.example.com -> /var/www/example.com/123/ | |
# test.example.com -> /var/www/example.com/test/ | |
# www.example.com -> /var/www/example.com/www/ | |
# An extra Vhost for example.com -> /var/www/example.com/www/ will be required. | |
# Enable Rewrite | |
RewriteEngine On | |
# Define a mapping to convert string to lowercase | |
RewriteMap lc int:tolower | |
# Optional. 404 for extra level of subdomain(a.b.example.com). | |
RewriteCond %{HTTP_HOST} ^([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)$ | |
RewriteRule ^.*$ - [R=404,L] | |
# Optional. If URI matches the alias defined, exclude it from mapping | |
# Example: Alias of /icons/ is defined in mods-available/autoindex.conf (by enabling mod_autoindex) | |
# If it's not excluded from mapping here, 404 will be returned. | |
# Repeat this for each Alias defined. | |
RewriteCond %{REQUEST_URI} \/icons\/.* | |
RewriteRule ^.*$ - [L] | |
# Here comes the real mapping | |
# Explanation | |
# Split the host by '.' | |
# Convert the domain part to lowercase, and map to directory | |
# Other Styles: | |
# 123.example.com -> /var/www/com/example/123/ | |
# RewriteRule ^(.*)$ /var/www/${lc:%3}/${lc:%2}/${lc:%1}/$1 | |
# laravel.example.com -> /var/www/com/example/laravel/public/ | |
# RewriteRule ^(.*)$ /var/www/${lc:%3}/${lc:%2}/${lc:%1}/public//$1 | |
RewriteCond %{HTTP_HOST} ^([^.]*)\.([^.]*)\.([^.]*)$ | |
RewriteRule ^(.*)$ /var/www/${lc:%2}\.${lc:%3}/${lc:%1}/$1 | |
# FAQ: | |
# Why lowercase? | |
# Yes, your browser always convert domains to lowercase | |
# However, the value comes from HTTP Header "Host", which could be upper case while using other tools(Postman/Telnet..) | |
# Securiy concerns? | |
# For the Host header, only [a-zA-Z-_\.] are possible. so the file system would be fine |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You will need an additional "RewriteBase /" in your app's public directory. (public/.htaceess for Laravel as an example)