htaccess sudomain opens directory of root domain - .htaccess

I have redirected all subdomains to my main domain with the UI of my webhoster (*.domain.com -> domain.com)
Htaccess in the root (rewrite all subdomain requests to a corresponding named folder in a folder clients):
RewriteCond %{HTTP_HOST} ^([0-9A-Za-z\-_]+)\.domain\.com$ [NC]
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/clients/\1/ [NC]
RewriteRule ^(.*)$ /clients/%1/$1 [L]
That will show the content of /client/client1/index.html at client1.domain.com
The Problem: When i have a folder in domain.com, for example domain.com/test/, it will also open that folder from the subdomain client1.domain.com/test
How can i prevent that?

Related

HTACCESS redirect to folder based on hostname

I have multiple websites under the same web server.
My root structure is as following:
./
./site1/
./site2/
./.htaccess
I would like htaccess file to redirect to the correct folder.
If the user navigates to www.site1.com or site1.com it should rewrite the url to go to the ./site1/ folder and if the user navigates to www.site2.com or site2.com it should rewrite the url to go to the ./site2/ folder.
Is there any way to achieve this behaviour?
Thanks in advance.
Use following code in .htaccess file.
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com$ [NC]
RewriteRule !^site1/ /site1%{REQUEST_URI} [L,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?site2\.com$ [NC]
RewriteRule !^site2/ /site2%{REQUEST_URI} [L,NC]

exclude main www domain from cpanel wildcard subdomain

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]

Non-www to www rewrite htaccess on shared hosting

I have a shared hosting plan so my website is located in: /public_html/example.com/
The .htaccess is located in the root folder /public_html/
I want to redirect example.com to www.example.com
so far, I've used
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
When I use my browser on example.com/test/ it directs to www.example.com/example.com/test/
How can I fix that?

.htaccess redirect all subdomains to single folder

I have in a site with many user subdomains ie user1.example.com or user2.example.com.
I have the following rewrite rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^ %1%{REQUEST_URI} [L]
However this only works with same name folders. I want all subdomains to go to a single folder I have setup to parse the domain. How can I make all subdomains redirect to one folder?

.htaccess - redirect links on page by removing folder from link

I followed this tutorial to manage subdomains for a MODx Revo 2.1.3 installation. The idea is that the htaccess file in the web root folder redirects calls for subdomain.mydomain.com to a folder in the web root directory by the same name as the subdomain, ie /home/mydomain/www/subdomain. Subdomain contains the MODx files to run the page, as well as another htaccess to point all further requests back to the root folder.
The better part of it works; I can view the homepage of the site (which means MODx is doing its part), but none of the links to the css, js, images, etc work, and it seems like the Wayfinder and getResources packages are failing to output. The links that are being used for the images+css+js are /subdomain/assets...etc; I need the links to point directly to the /assets folder. It's like the root .htaccess works to redirect the request to the subdomain folder, but the .htaccess in the folder doesn't point anything back up to the root for the remaining requests.
Here's my root folder htaccess, the 2nd part taken from the tutorial:
RewriteEngine On
RewriteBase /
# The Multiple subdomains part
#REDIRECT SUBDOMAIN TO SUBDIRECTORY OF SAME NAME
RewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
RewriteCond %{HTTP_HOST} ([a-z0-9][-a-z0-9]+)\.mydomain\.com\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^(.*) %1/$1 [E=SUBDOMAIN:%1,L]
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]
and the subdomain folder htaccess is simply:
RewriteEngine On
RewriteBase /
I know the site works; I can access it using a subdomain that hasn't been processed like the tutorial yet. So it's all there, I just need to sort out the link requests. Can anyone help?
Thanks to some assistance, I managed to find the solution to this issue.
# The Multiple subdomains part
#REDIRECT SUBDOMAIN TO SUBDIRECTORY OF SAME NAME
RewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
RewriteCond %{HTTP_HOST} ([a-z0-9][-a-z0-9]+)\.spitfireresources\.com\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1 -d
#RewriteCond %{REQUEST_FILENAME} !^/js
#RewriteCond %{REQUEST_FILENAME} !^/css
#RewriteCond %{REQUEST_URI} !^/assets
RewriteCond %{REQUEST_URI} !^/
RewriteRule ^(.*) %1/$1 [E=SUBDOMAIN:%1,L]
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]
The key line is RewriteCond %{REQUEST_URI} !^/. Once that was in place everything becomes accessible. Thanks to everyone who commented.

Resources