Point sub domain to folder in root directory - .htaccess

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]

Related

htaccess redirect subdomain into folder that has similiar name with subdomain

I did many way to do this, but always getting 404 error.
I want to redirect subdomain
[username].domain.com/
to
domain.com/user/[username]/index.php
and the url on browser address is not changed still
[username].domain.com/
[username] is dynamic according to registered user in my site.
my last trial httaccess setting is
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(^.*)\.domain\.com$
RewriteRule ^ ^.domain.com/user%{REQUEST_URI} [L,P]
Note: I did set my DNS wildcard *.domain.com
Thanks in advance,
Nanang K
============================================================
SOLVED
I had to create 3 htaccess files.
in root directory (redirect to user)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(^.*)\.domain.com
RewriteRule ^(.*)$ /user/$1 [NC,L]
in user directory (redirect to username_folder)
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([aA-zZ])$ user/$1/index.php
RewriteCond %{HTTP_HOST} ^(^.*)\.domain.com
RewriteRule (.*) user/%1/index.php
in username_folder directory (rewrite the url into original url)
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([aA-zZ])$ index.php?siteName=$1
RewriteCond %{HTTP_HOST} ^(^.*)\.domain.com
RewriteRule (.*) index.php?siteName=%1
now, when user access robert.domain.com, he will get the content from domain.com/user/robert/index.php. And the url browser still robert.domain.com
hope helps someone who get similiar problem.
Hello Can you please try this :-
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ /users/$1 [L,NC]
It may use helpful.
It should be like this, try it out.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^[username].domain.com$
RewriteRule ^/?$ "http\:\/\/domain\.com\/users\/" [R=301,L]
hope this helps!

htaccess redirect root to subfolder - but keep other root subfolders beyond

My server has several system folders and files by default. so for better overview I want to put my website in a subfolder like this:
www.domain.de/web/index.html
ALL links and website-related stuff is found in this subfolder "web". BUT the folder "web" should not be seen in the url!
I got this with these lines in my htaccess and it works:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.de [NC]
RewriteCond %{REQUEST_URI} !^/web/.*$
RewriteRule ^(.*)$ /web/$1
But now the problem:
I also want other folder on my server to work. So www.domain.de/downloads/ should work, too. This folder is NOT in the subfolder "web". So if I write the direct url "www.domain.de/downloads" in my webbrowser, I don't want to get a redirect to "www.domain.de/web/downloads/", you know?
Is there any solution? :)
Thanks!
You Can use a RewriteCond to exclude your folder :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.de [NC]
RewriteCond %{REQUEST_URI} !^/download [NC]
RewriteCond %{REQUEST_URI} !^/web/.*$
RewriteRule ^(.*)$ /web/$1

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]

htaccess - Point domain and a sub domain to a folder

I have two folders on FTP root. site_v1 and site_v2. site_v2 has new version of website and site_v1 has older version. I wanted to point my domain to site_v2, the latest version. So I made an .htaccess on root as under.
RewriteCond %{HTTP_HOST} example.com
RewriteCond %{REQUEST_URI} !site_v2/
RewriteRule ^(.*)$ site_v2/$1 [L]
This works well. When I open http://example.com; site_v2 loads seamlessly.
Then, for site_v1, I created a sub domain http://old.example.com and pointed it to site_v1 folder; I get this error when I run http://old.example.com.
The requested URL /site_v1/site_v2/site_v1/index.html
was not found on this server.
I am clueless. It seems that http://old.example.com is still pointing to site_v2 because of .htaccess on root. What could be the solution so I can force old.example.com to load in site_v1?
Try changing your rules to:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/site_v[0-9]/
RewriteRule ^(.*)$ /site_v2/$1 [L]
RewriteCond %{HTTP_HOST} ^old\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/site_v[0-9]/
RewriteRule ^(.*)$ /site_v1/$1 [L]
This way, the host matching against the `%{HTTP_HOST} var won't overlap.

Hide folders in url, rewrite url

I have a project with a special hosting structure:
www
domains
projectName (example.com)
I have to set the baseUrl config.baseURL = http://example.com/domains/example.com/.
Url example http://example.com/domains/example.com/index.php?id=4.
If I use only config.baseURL = http://example.com/. The site works but sessions do not work and user cannot log (he can log in but after reload he is logged out)
I need to have url only with http://example.com/index.php?id=x.
There is 1 htaccess in the www folder with these rules.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^domains/
RewriteCond %{REQUEST_URI} !^/domains/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteCond %{DOCUMENT_ROOT}/domains/%2 -d
RewriteRule (.*) domains/%2/$1 [DPI]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,QSA,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^domains/[^/]+/(.+[^/])$ /$1/ [R]
And another is in my project folder.
I am sure there will be just an easy htaccess rule. But I do not have any experience with it. I tried to use the solution from Mod Rewrite Hide Folder but it did not help. I tried to use it in both htaccess files.
Any ideas?
Thank you!
You could try setting the TypoScript option config.absRefPrefix to /domain/www.example.com/
See http://buzz.typo3.org/people/soeren-malling/article/baseurl-is-dead-long-live-absrefprefix/ and http://wiki.typo3.org/TSref/CONFIG

Resources