So if you have a structure like this:
public_html/folder/example.com/index.php
public_html/folder/example.com/subdomain1/index.php
public_html/folder/example.com/subdomain2/index.php
public_html/folder/example.com/subdomain3/index.php
And you want each of the 'subdomain' folders to be visible at http://subdomain1.example.com instead of http://example.com/subdomain1/...
Can you achieve this with a combination of the wildcard subdomain DNS setting and just a .htaccess file?
For instance, I have the wildcard subdomain added via cPanel and I have in my .htaccess file (root of the example.com folder):
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^([^.]+).example.com$ [NC]
RewriteCond %{REQUEST_URI} !%1
RewriteRule ^(.*)$ /%1/$1 [L]
</IfModule>
But every time I try to visit one of the subfolders at the subdomain URL, I get a 500 error.
Is there something I'm missing? I've used a .htaccess test tool to make sure the rules were correct. I know mod_rewrite is enabled, and the Apache settings are correct. Is there an issue with the index.php piece of this for instance?
I can also still visit http://example.com/subdomain1/ and see the site. It doesn't redirect to the subdomain as I thought it would.
Any help in the right direction would be appreciated. I've been searching and testing different rules for a few hours at this point. I did reach out to my host and verify I had all the appropriate permissions and settings.
If I missed it - the idea here is that whenever a subfolder is added (unless a specific exception is made), it should be accessible as a subdomain address automatically - no manual subdomain add in cPanel / DNS.
///EDIT///
Below is the .htaccess file in the public_html/ root directory. There is no .htaccess in the public_html/folder/ directory.
# ----------------------------------------------------------------------
# CORS-enabled images (@crossorigin)
# ----------------------------------------------------------------------
# Send CORS headers if browsers request them; enabled by default for images.
# developer.mozilla.org/en/CORS_Enabled_Image
# blog.chromium.org/2011/07/using-cross-domain-images-in-webgl-and.html
# hacks.mozilla.org/2011/11/using-cors-to-load-webgl-textures-from-cross-domain-images/
# wiki.mozilla.org/Security/Reviews/crossoriginAttribute
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
# mod_headers, y u no match by Content-Type?!
<FilesMatch "\.(gif|png|jpe?g|svg|svgz|ico|webp)$">
SetEnvIf Origin ":" IS_CORS
Header set Access-Control-Allow-Origin "*" env=IS_CORS
</FilesMatch>
</IfModule>
</IfModule>
# ----------------------------------------------------------------------
# Webfont access
# ----------------------------------------------------------------------
# Allow access from all domains for webfonts.
# Alternatively you could only whitelist your
# subdomains like "subdomain.example.com".
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
AddType audio/ogg ogg ogv
AddType video/ogg ogg ogv
RewriteCond %{HTTP_HOST} ^([^.]+).example.com$ [NC] RewriteCond %{REQUEST_URI} !%1 RewriteRule ^(.*)$ /%1/$1 [L]
This would seem to be causing a rewrite loop (500 HTTP response). I think this is because the REQUEST_URI
server variable is not updating.
You only want this code block to execute on the initial request, so you can resolve this by checking against the REDIRECT_STATUS
environment variable, which is not set (ie. empty) on the initial request and set to "200" (as in 200 OK status) after the first successful rewrite.
For example, try the following instead:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule (.*) /%1/$1 [L]
No need for the <IfModule>
wrapper since your site won't function without mod_rewrite. (.*)
is the same as ^(.*)$
since the regex is greedy by default.
Aside:
Note that the mod_rewrite directives in this Addon domain's .htaccess
file will override the mod_rewrite directives in the parent .htaccess
(ie. public_html/.htaccess
), so no HTTP to HTTPS - unless you have explicitly enabled mod_rewrite inheritance in the server config.
However, the Header
directives in the parent .htaccess
should still be processed.