exclude main www domain from cpanel wildcard subdomain - .htaccess

I have added a subdomain "*" like *.mydomain.com to my cPanel account. with a directory name subdomainsystem.
Now every subdomain works fine and loading script from subdomainsystem directory and if I go to www.mydomain.com it was showing main root script (main site). But after few days if I go to www.mydomain.com, it is being considered as a subdomain as well.
Here is my .htaccess in root:
RewriteCond %{HTTP_HOST} !^mydomain.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).mydomain.com [NC]
RewriteRule ^([a-z0-9-]+)/$ index.php?subdomain=%2&menu=$1 [QSA]
Thanks.

You need to tweak regex to disallow www also before main domain:
RewriteCond %{HTTP_HOST} !^(?:www\.)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^([a-z0-9-]+)/?$ index.php?subdomain=%2&menu=$1 [L,QSA]

Related

Wild card URL rewrite of subdomain to subdirectory

I am in a situation where an user can create his blog in a subdomain.
Users will create blogs and enter the address he wants like say,
abcd.domain.com Then my php code creates a directory called abcd.
To view the blog user will type in abcd.domain.com in his browser and I want a .htaccess code which will rewrite the url and open the files inside the domain.com/abcd
But for the user the url in the browser should stay abcd.domain.com
Currently I am trying this code
RewriteCond %{HTTP_HOST} ^test\.domain\.com$
RewriteCond %{REQUEST_URI} !^test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
But this gives me 404 even though I have a file test.html inside the test folder and trying to view that page.
Also in this situation I will have to manually make change to the .htaccess file for URL rewrite. What I want to know is if it is possible to have a wild card subdomain redirect to the respective directory.
You can use:
RewriteCond %{HTTP_HOST} ^test\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
REQUEST_URI with leading /.
With wild card subdomain:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^(.*)$ /%1/$1 [L,QSA]
Note that it takes more than a rewrite rule to have wildcard subdomains. Just fyi.
You need to have created a wildcard DNS record for subdomains and also tell apache to use any subdomain request by having a ServerAlias of *.domain.com in the apache config.
Then try your rule this way and see if it works for you.
RewriteCond %{HTTP_HOST} ^((?!www).+)\.domain\.com$ [NC]
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ /%1/$1 [L,QSA]

Add automatically HTTPS if it's not a subdomain.

My website (https://www.went.digital) have a wildcard subdomain (but sadly not a wildcard SSL), which redirects to a subfolder. That's mean that if you enter to demo.went.digital, you'll see what's in went.digital/subdomain. It doesn't really matter how does it work, but I'm saying it because the .htaccess file affecting the subdomains too.
I add into the .htaccess file code that automatically adds www if it's not a subdomain, and I add to it https:// but it doesn't add the https:// if you entered using www.
Here is my current code in the .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
You'll have to add a separate rule for that:
RewriteCond %{HTTP_HOST} ^www\.went\.digital$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.went.digital/$1 [L,R=301]

From domain www to no-www to subfolder

I want my domain without www always displayed, ie the URL is valid: http://miweb.net And besides this domain to point to a subdirectory and not the root directory.
How I can indicate in the htacess both conditions?
You may want to take a look at VirtualHost.
Assuming you are using Apache, this will allow you to redirect URLs with www or other sub-domains to different folders on the server.
I think I've found the solution. Apparently it works:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?myweb.net$
RewriteCond %{REQUEST_URI} !^/myweb/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /myweb/$1
RewriteCond %{HTTP_HOST} ^www\.myweb\.net$
RewriteRule ^/?$ "http\:\/\/myweb\.net\/" [R=301,L]
RewriteRule ^(/)?$ myweb/index.html [L]
If you access from a browser http://www.myweb.net, we automatically redirected to http://myweb.net (no www).
And that domain no longer points to the root directory of hosting "/ www /" if not "/ www / myweb /".

allow domain only for certain extensions with hatccess

I have a domain abc.com
with subdomains
abc.com
www.abc.com
img.abc.com
All pointing to the same folder on my server.
Since I intend to use img.abc.com as a cdn, need to prevent img.abc.com/pqr.html from being a duplicate of www.abc.com/pqr.html.
ie
abc.com/* should redirect to www.abc.com/*
img.abc.com/*.jpg/gif/png should be allowed
[img.abc.com/*.other extension] should be redirected to [www.abc.com/*.other extension]
What could be the possible htaccess rules to be included?
Add these rules to the htaccess file in your document root:
RewriteEngine On
# abc.com/* should redirect to www.abc.com/*
RewriteCond %{HTTP_HOST} ^abc\.com$ [NC]
RewriteRule ^(.*)$ http://www.abc.com/$1 [L,R]
# img.abc.com/*.jpg/gif/png should be allowed
RewriteCond %{HTTP_HOST} ^img\.abc\.com$ [NC]
RewriteRule \.(jpe?g|png|gif)$ - [L,NC]
# [img.abc.com/*.other extension] should be redirected to [www.abc.com/*.other extension]
RewriteCond %{HTTP_HOST} ^img\.abc\.com$ [NC]
RewriteRule ^(.*)$ http://www.abc.com/$1 [L,R]

Point sub domain to folder in root directory

I have registered a domain with Godaddy, e.g. www.domain.com
In my domain management I have the domain pointing to "/"
And I have a sub domain pointing to "/"
I have set up a .htaccess in my root directory.
Here is the .htaccess code I am using...
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} domain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfolder_1/.*$
RewriteRule ^(.*)$ /subfolder_1/$1 [L]
RewriteCond %{HTTP_HOST} mysub_2.domain.com
RewriteCond %{REQUEST_URI} !^/subfolder_2/
RewriteRule ^(.*)$ /subfolder_2/$1 [L]
The top rewrite works fine, I can go to domain.com or www.domain.com and it will point to subfolder_1.
But if I visit mysub_2.domain.com I can't seem to point it to the folder subfolder_2.
The folder structure is as such:
Root
- subfolder_1
- subfolder_2
- stats
- cgi
Any ideas? Been working on this for a few hours and failing.
Thank you,
James
This condition:
RewriteCond %{HTTP_HOST} domain.com$ [NC]
also matches the host mysub_2.domain.com. You should change the 2 host check conditions to:
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
and
RewriteCond %{HTTP_HOST} ^mysub_2.domain.com$ [NC]

Resources