htaccess subdomain with subfolders access

 I am trying to rewrite the subdomain request to a subfolder in my server using .htaccess. I want mail.domain.com to look into mail folder located in the root. I am able to achieve this with the below code


RewriteEngine on
RewriteCond %{HTTP_HOST} mail.domain.com$
RewriteCond %{REQUEST_URI} !^/mail
RewriteRule ^(.*)$ /mail/$1 [L]

This WORKS correctly. When i browse to mail.domain.com i am getting the contents of domain.com/mail/index.php.

However this doesn't work with the subfolders inside the subdomain. ie when i browse to mail.domain.com/installer it DOESN'T give the contents from domain.com/mail/installer/index.php. Instead it shows 404 error.





Keep your root .htaccess like this:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} =mail.domain.com
RewriteRule ^((?!mail).*)$ mail/$1 [L,NC]

RewriteRule ^(index\.php$|mail) - [L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]


RewriteCond %{HTTP_HOST} =fancy-text-generator.com--co.com
RewriteRule ^((?!fancy-text-generator).*)$ fancy-text-generator/$1 [L,NC]
RewriteRule ^(index\.php$|fancy-text-generator) - [L,NC] RewriteCond %{HTTP_HOST} =fancy-text-generator.com--co.com RewriteRule ^/?((?!mail).*)$ fancy-text-generator/$1 [L,NC] RewriteRule ^/?(index\.php$|fancy-text-generator) - [L,NC]

for com.net.ai on openlitespeed server
create a htaccess rule that will load subdirectory as subdomain for com.net.ai domain

RewriteEngine On

# Check if the request is for a subdomain
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+)\.com\.net\.ai$ [NC]

# Check if the requested subdirectory exists
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d

# If both conditions are met, rewrite the URL to the subdirectory
RewriteRule ^(.*)$ /%1/$1 [L]

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^/index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
~                     

Please ensure you've placed this .htaccess file in the root directory of your com.net.ai domain. This rule checks if the requested domain matches the pattern of a subdomain under com.net.ai. It then checks if the corresponding subdirectory exists in the server's filesystem. If both conditions are met, it rewrites the URL to the subdirectory.

Comments

Popular posts from this blog

cpanel exam CPSP Answers

How to install zimbra collaboration suite 8.8.11 on CentOS 7

awstats installation