Two Domains, 1 Shared Server, .htaccess file - .htaccess

I looked through a lot of the other "two domains, one server" questions and didn't see exactly what I'm looking for...
I have two domains, www.example1.com and www.example2.com. They are in a shared hosting environment with apache. I want:
A) example1.com to just load the normal index.html file (on a wordpress install)
B) example2.com to go to a subfolder /forums/
So, in other words, I want example1.com to be my main (normal and happy) domain and example2.com to only load up the /forums/ directory.
I'm thinking that I may only need a rewrite rule for example2.com going to /forums/?
I hope that makes sense :). Thanks so much for any help you can offer.

Try adding the following to your htaccess file in the root folder of your domain.
RewriteEngine on
RewriteBase /
#if on www.example1.com domain
RewriteCond %{HTTP_HOST} ^www\.example1\.com$ [NC]
#and not an existing file of directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#then send all requests to index.php
RewriteRule ^ index.php [L]
#if on www.example2.com domain
RewriteCond %{HTTP_HOST} ^www\.example2\.com$ [NC]
#if not already in forums directory
RewriteCond %{REQUEST_URI} !/forums/ [NC]
#rewrite requests to forums directory
RewriteRule (.*) forums/$1 [L]

Maybe it will be useful to someone. After scanning and trying various methods following seems to work perfectly. I am using Apache server. In my htaccess file, after IfModule I added <If> condition and it works fine.
<IfModule>
.
.
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
<If "%{HTTP_HOST} == 'www.xyzs.co.uk'">
Redirect permanent /xyz1 /yyy1
Redirect permanent /xyz2 /yyy2
Redirect permanent /xyz3 /yyy3
Redirect permanent /xyz4 /yyy4
<If>
More reading: http://httpd.apache.org/docs/2.4/expr.html

Related

htaccess - assistance forcing WWW and HTTPS. Currently results in https://www.example.com/example.com

I'm having issues setting up htaccess properly and was hoping to get some help getting it squared away. Currently I have my primary domain on my shared hosting routed to /public_html/example.com (for consistency's sake) using the following htaccess in the /public_html/ folder:
# .htaccess main domain to subdirectory redirect
RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/example.com/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /example.com/$1
# Change example.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ example.com/ [L]
That works all fine but now I want to add lines to force WWW and to force HTTPS. I have a htaccess file that works great for my subdomains but when I add that to my /public_html/example.com/.htaccess file it creates an odd behavior. Any traffic to example.com redirects to https://www.example.com/example.com. This code is shown below.
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Can anyone help me merge the two so I get the directory relocation to /public_html/example.com, forced WWW and forced HTTPS all in one go? Thank you very much in advance!
This is being caused by another rule in your .htaccess. The rule in question is:
RewriteRule ^(/)?$ example.com/ [L]
Or to be more precise the [L] flag at the end of the rule. Which stands for LAST. This is telling your .htaccess to stop any rules pass this rewrite rule.
You can simply fix this by either putting your new rules to the top of your .htaccess, removing the [L] flag or by removing the other rule altogether.
Buyers choice.

Domain redirection with folder separation. htaccess

After redirecting my client domain to my server i created rewrite rule in root folder .htaccess file to point domain to a subfolder1 (joomla website):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?myclientdomain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !/subfolder/
RewriteRule ^(.*)$ /subfolder1/$1 [L]
Everything worked fine but today i found out that i can access any other subfolder in my server by entering for example: myclientdomain.com/subfolder2 and what's worse google can index that and show it in search results.
If there is any way to redirect a domain in a way that I won't be able to access any other folder on my server?
I would really appreciate help as I searched throughout google for answer, my server tech support said that they don't really support these kind of problems (they only gave me a piece of code from above) and I don't really know anything about .htaccess rules and how it works.
Try this
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?myclientdomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfolder\/?(.*)?$
RewriteRule ^(.*)$ /subfolder1/$1 [L]
Change above rule to:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?myclientdomain\.com$ [NC]
# if current URI is not starting with /subfolder/ then route to /subfolder/
RewriteRule ^((?!subfolder1/).*)$ subfolder1/$1 [L,NC]

htaccess for multiple subdomains from folders

I am dealing with following problem:
I have one main domain "example.com" and multiple subdomains:
one.example.com
two.example.com
three.example.com
etc. on server.
I have content for this subdomains in folders:
./one/ -> one.example.com
./two/ -> two.example.com
./three/ -> three.example.com
etc.
So to serve content from these folders to subdomains, I use following htaccess file:
RewriteCond %{HTTP_HOST} ^one.example.com [NC]
RewriteCond %{REQUEST_URI} !^/one/.*
RewriteRule ^(.*) /one/$1 [L]
It works fine, but my content for subdomains is also accessable from URL:
one.example.com/one/my-content
such as
one.example.com/my-content
etc. So it is duplicite.
This is one problem I need to solve.
Second is, that content for example.com is accessable from
whatever.example.com
(a.example.com, b.example.com etc.)
so again duplicite.
So my question is: how to redirect/disable URLs that are creating duplicite?
Thanks a lot, I've tried a lot of versions of code, but with no effect.
I solved this issue by redirecting all requests to a subdomain to that folder and all requests to the base domain to another, like this:
RewriteCond %{HTTP_HOST} ^one\. [NC]
RewriteRule (.*) /one$1 [L]
RewriteCond %{HTTP_HOST} ^two\. [NC]
RewriteRule (.*) /two$1 [L]
RewriteRule (.*) /zero$1
It seems to work, but I'm having my URL modified on the browser too. How do I avoid this from happening?
For your first issue, you can add a rule, that redirects any requests to that folder and withing that directory to a page that does not exists (so that it throuws a 404 error)
RewriteCond %{HTTP_HOST} ^one\.example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} ^one/
RewriteRule . /notexists.html [L]
For your second issue, this should not happen since when you ask for a.example.com/folder/file.html it gets redirected internaly to a.example.com/a/folder/file.html (there is no way to get the content in the parent directory of 'a' )

Htaccess redirect with cakePHP

I have a cakePHP app working at http://domain1.com/domain2, and want to point http://domain2.com/ to this application.
I have done this change the document root of domain2 to public_html/domain2, but, when I go to: domain2.com all css, javascript and images are not loaded, with a message saying controller not found.
What I can do?
All domains have a folder with their domains without the TLD at domain1.com, I think if I create a htaccess and get the domain without TLD, and finally change rewriteBase, will work as expected, but don't know how to do this.
Current htaccess of domain2:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
You can try adding an htaccess file in domain2's document root with the following rules:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.(css|js|png|jpe?g|gif|bmp|ico)$ [NC]
RewriteRule ^/?domain2/(.*) /$1 [L,PT]
This should remove the domain2 part of the links.

2 domains, htaccess redirect one to index.html but not the other

I'm working on a site that has 2 domains pointing to the same public_html folder.
The server server index.html by default. My code uses htaccess to route all calls to index.php but I only want this to happen for the development URL for now and not the live URL.
I tried routing live.com and www.live.com to www.live/index.html - but I get a redirect loop as I don't know how to make it break if index.html is in the URL.
Can anyone help?
I tried:
# Disable live domain
RewriteCond %{HTTP_HOST} ^live.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.live.com$
RewriteRule (.*)$ http://www.live.com/index.html [L]
Thanks!
# Disable live domain
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?live\.com$
RewriteCond $1 !^index\.html
RewriteRule ^(.*)$ http://www.live.com/index.html [L]

Resources