How to route multiple domains to multiple subfolders - .htaccess

I have a setup where I have a couple of sites hosted in different subfolders in the root of my FTP
ROOT
-site1folder
-site2folder
I have 2 domain names not associated with the account. I have changed the DNS records of the domains to point to my hosting account but I need the 2 different URLs to go to the correct subfolder to load the correct site rather than transfer the domains. The domains are
www.site1.com
www.site2.com
Ideally I would like site1 to load the contents of the site1folder and the same for site 2 and its corresponding folder.
I currently have a .htaccess file in my root which I have mashed together from looking at different posts. I am a newbie to .htaccess. I am currently trying to get site 1 to point to the site1 folder
Options +FollowSymLinks
#Turns the rewrite engine on.
RewriteEngine on
#Fix missing trailing slash character on folders.
RewriteRule ^([^.?]+[^.?/])$ $1/ [R,L]
RewriteBase /website1
#www.site1.co.uk and site1.co.uk will map to the folder
RewriteCond %{HTTP:Host} ^(?:www\.)?site1\.co.uk
RewriteRule (.*) /site1 [NC,L,NS]
What happens when I load this is I get the contents of the site1 folder but at this URL site1.co.uk/site1
I don't want the URL to change at all. I just want it to load the contents of the subfolder it is pointed to.

Ok people I managed to solve this one myself in the end after lots of head banging and reading a lot of the docs to decyfer others code. Here is what I did...
Options +FollowSymLinks
RewriteEngine on
#========================================================================
# FIRST Handle the http requests first before removing the additional url junk
#========================================================================
#rule for site1.com to link to site1folder directory
RewriteCond %{HTTP_HOST} ^(www.)?site1.com$
RewriteCond %{REQUEST_URI} !^/site1folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site1folder/$1
#rule for site2.com to link to site2folder directory. Its the same as above just with site2 URL and subfolder
RewriteCond %{HTTP_HOST} ^(www.)?site2.com$
RewriteCond %{REQUEST_URI} !^/site2folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site2folder/$1
#==========================================================
# SECOND Remove the additional url junk once the new url is loaded
#==========================================================
#rule for site1 url rewrite to remove /site1foler/index.php from the URL
RewriteCond %{HTTP_HOST} ^(www.)?site1.com$
RewriteRule ^(/)?$ site1folder/index.php
#rule for site2 url rewrite to remove /site2foler/index.php from the URL. Again its the same as above just with the site2 URL and sub folder info.
RewriteCond %{HTTP_HOST} ^(www.)?site2.com$
RewriteRule ^(/)?$ site2folder/index.php
Hopefully you can see where the repetition is in this so if you wished to add more sites down the line to it you could by copying and pasting the data and changing the site URL and sub folder to match the additional ones you wish to add.
I hope this has managed to help some of you. Also if you can see issues with this then it would be very much appreciated as I am a newbie at .htaccess files and as you can imagine, I figured as it works, it must be right.

I have used this in a Dedicated Windows Server using xampp. It works fine to me with SSL
# mydoamin.com starts ############################
RewriteCond %{HTTP_HOST} ^(www.)?mydoamin.com$
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# mydoamin.com ends ############################
# mydoamin.in starts ############################
#RewriteCond %{HTTP_HOST} ^(www.)?mydoamin.in$
#RewriteCond %{HTTPS} !on
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# mydoamin.in ends ############################

Related

URL rewrite to remove trailing folder name

We have a server, and have several folders under the /var/www/ folder.
We have pointed our domain name to the IP of the server, and by default we expect to load one folder as the website (since its not possible to point a folder with DNS).
I have written .htaccess in such a way that when you enter the IP or the domain name, the request redirects to the website folder.
However, whenever we enter the IP or the domain name, the name of the folder is getting added to the URL.
Here is the present .htaccess:
Options +FollowSymlinks -Multiviews
#DirectoryIndex folder/
RewriteEngine on
ReWriteBase /
RewriteRule ^$ /folder [L]
RewriteRule ^$ folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^folder/)^(.*)$ /folder/$1 [L,NC]
where the folder is the folder's website
so,
www.domain.com
becomes
www.domain.com/folder/
Is there a way to rewrite the URL to remove the folder name?
Thanks in advance :)
EDIT : Added .htaccess code
Have your rule like this in DocumentRoot/.htacess:
DirectorySlash On
Options +FollowSymlinks -MultiViews
RewriteEngine on
ReWriteBase /
# redirect /folder/abc123 to /abc123
RewriteCond %{THE_REQUEST} \s/+folder/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# skip rewrites for real files/directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule !^(index\.)?$ - [L]
# route everything internally to /folder
RewriteRule ^((?!folder/).*)$ folder/$1 [L,NC]
It sounds like you made an external redirect instead of an internal rewrite. An external redirect is denoted by the [R] flag (with optional parameter) and causes Apache to send a 301 or 302 header back to the client with a different url that client should request. This causes the client to show the new url in the address bar.
What you want is an internal rewrite. When you request the url, the url is internally rewritten to the place where the resource is actually located. You do this by omitting the [R] flag, and not using a domain name in the rewritten part. It typically looks something like this:
RewriteCond %{REQUEST_URI} !^/folder
RewriteRule ^(.*)$ /folder/$1 [L]

Links are broken when installing CakePHP on shared hosting

I have set up CakePHP on shared hosting, within a sub-directory (cakeDomain). Using htaccess in the root directory (mainDomain), I have pointed another domain to it (cakeDomain), like so:
# /.htaccess
# cakeDomain redirect rules
RedirectMatch 301 ^/cakeDomain/$ http://cakeDomain.com/
# handle domain root and skip subfolders
RewriteCond %{HTTP_HOST} cakeDomain.com
RewriteCond %{REQUEST_URI} !^/cakeDomain/
RewriteCond %{REQUEST_URI} \..+$
RewriteRule ^(.*)$ cakeDomain/$1 [L]
# add trailing slash to subfolders (eg abc to: abc/)
RewriteCond %{HTTP_HOST} cakeDomain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} [^/]$
RewriteRule ^(.*)$ $1/ [L,R=301]
# handle files in subfolders
RewriteCond %{HTTP_HOST} cakeDomain.com
RewriteCond %{REQUEST_URI} !^/cakeDomain/
RewriteRule ^(.*)$ cakeDomain/$1/ [L]
RewriteCond %{HTTP_HOST} www.mainDomain.com/cakeDomain/
RewriteRule %{HTTP_HOST} www.cakeDomain.com [L]
Visiting mainDomain.com/cakeDomain correctly redirects you to cakeDomain.com
CakePHP's files are stored as so:
/cakeDomain/app/...
/cakeDomain/lib/...
/cakeDomain/...
etc.
Visiting cakeDomain.com brings up the correct front page but all of the links have the installed directory prepended to them:
cakeDomain.com/cakeDomain/controller/action/param1
Instead of:
cakeDomain.com/controller/action/param1
Any ideas how to fix this?
Please note, from my many searches, that it appears many people immediately suggest virtual hosts. This is not an option here; my hosting is a virtual host. I presume this solution requires htaccess and/or routing tricks. Thank-you in advance for any help you can provide.
Basically, you're putting website B (Cake) inside the public web directory of website A (Maindomain).
I would REALLY discourage you to do so because this way all 'private' CakePHP directories are also inside your public web directory (for example cakeDomain/app/tmp/logs/error.log). Please check properly if those locations are secured properly
Dou you have access to the directories 'outside' your public webfoot?
Having said that, this may be of help:
http://cookingwithcakephp.blogspot.nl/2008/04/installing-cakephp-into-subdirectory.html

htaccess rewrite rule, old URL to new

A bit of help fellow SO people.
What I have at the moment (based on some code I used for a different type of URL).
I want the first URL to redirect to the second, with no query string included afterwards
This is what I have to so far.
RewriteRule ^(page.php?id=missionstatement+)/?$ http://example.com/why/mission-statement [R=301,L]
RewriteRule ^(page.php?id=ofsted+)/?$ http://example.com/how/ofsted-report [R=301,L]
RewriteRule ^(page.php?id=governingbody+)/?$ http://example.com/governors [R=301,L]
Here is the rule (will redirect 1 URL):
RewriteCond %{QUERY_STRING} ^id=whatever
RewriteRule ^page\.php$ http://%{HTTP_HOST}/how/somehow? [R=301,L]
This rule intended to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
I have used %{HTTP_HOST} -- this will redirect to the same domain as requested URL. If domain name has to be different, replace it by exact domain name.
The ? at the end of new URL will get rid of existing query string.
Ahoy!
Give this a whirl:
#check mod_rewrite is enabled
<IfModule mod_rewrite.c>
#enable mod rewrite
RewriteEngine On
#set working directory
RewriteBase /
#force trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [R=301,L]
#bootstrap index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page.php\?id=(.*)$ http://www.willans.com/page.php/$1 [R=310,L]
#end mod rewrite check
</IfModule>
It's been a while since i've done any web dev, but that should be a push in the right direction at least ;)

Change the document root of a parked domain?

I have run into a problem which is that my web host doesn't appear offer addon domains.
I currently have a domain name pointing to my name servers.
I went into cPanel to add an addon domain that points to a sub-directory, but all that is available in cPanel is to park domain at the document root which is 'public_html/'.
So traffic coming from the parked domain would get the wrong content, which is obviously not good.
I get the feeling that this isn't possible, but can I change the parked domain document root from 'public_html/' to 'public_html/sub-directory' ?
Or perhaps can I edit the .htaccess file to redirect traffic from the parked domain to the sub-directory?
Basically I want this address; www.parked-domain.com/
To show the content of this sub-directory; www.first-domain.com/parked-directory
I hope this is possible otherwise I need to look at a new web host.
Regards,
Dean.
Add the following to your .htaccess file:
RewriteRule ^parked-directory - [L]
RewriteCond %{HTTP_HOST} ^(www\.)?parked-domain\.com$ [NC]
RewriteRule ^(.*)$ parked-directory/$1 [L]
While frowned upon, it is possible to dynamically set up your parked domain sub-hosts using mod_rewrite.
Here's a .htaccess sample for multihosting via mod_rewrite using a dynamic format that requires parked-domain-name = sub-folder-name, and covers other parked domains on your site being redirected or ignored.
# tell mod_rewrite to activate and give base for relative paths
RewriteEngine on
RewriteBase /
# permanent redirect of unused parked domains so they do not try to resolve.
# only use this redirect if you want multiple names to point to another.
# otherwise, replace the rewrite rule with: RewriteRule . - [F,L]
RewriteCond %{HTTP_HOST} ^(www\.)?parked-domain1\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?parked-domain2\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?parked-domain3\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?parked-domain4\.com$ [NC]
RewriteRule ^(.*)$ http://main-site.com/$1 [L,R=301]
# if you have an active site in hosting root folder,
# then tell it not to look for a subfolder by skipping next rule
RewriteCond %{HTTP_HOST} ^(www\.)?main-site\.com [NC]
RewriteRule ^(.*)$ - [S=1]
# the domain-name = sub-folder automation
# thus parked-domain5.com in /parked-domain5/
RewriteCond %{HTTP_HOST} ([^.]+)\.com
RewriteCond %{REQUEST_URI} !^/%1
RewriteRule ^(.*)$ %1/$1 [L]
# if you're hosting wordpress sites, this is a tweaked variant of the WP code.
# add the rest of this example only if:
# 1. main-site.com is a WP site
# 2. you remove .htaccess in subfolders
# if (1.) true, yet keeping subfolder .htaccess, uncomment next 2 lines:
# RewriteCond %{HTTP_HOST} !^(www\.)?main-site\.com [NC]
# RewriteRule ^(.*)$ - [S=1]
#### BEGIN WordPress, improved
# if this request is for "/" or has already been rewritten to WP
RewriteCond $1 ^(index\.php)?$ [OR]
# or if request is for image, css, or js file
RewriteCond $1 \.(gif|jpg|jpeg|png|css|js|ico)$ [NC,OR]
# or if URL resolves to existing file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# or if URL resolves to existing directory
RewriteCond %{REQUEST_FILENAME} -d
# then skip the rewrite to WP
RewriteRule ^(.*)$ - [S=1]
# else rewrite the request to WP
RewriteRule . /index.php [L]
# END WordPress
instead of using "parked domain", use the addon domain - and here just the root directory to public_html/sub-directory where you want to point your new "parked" domain...

301 All aspx files to a Subdomain

We just redesigned a site for a client in EE, located at example.com (with and without www.). Their original site is ASPX. They've still got a number of ASPX pages that they want to keep, so their IT people created a subdomain, www2, which is basically a clone of their old site.
I need an htaccess rule that will check if the requested page ends in .aspx, then redirects to the www2 subdomain. It should also make sure that the requested page doesn't exist
I tried using the following rule, but it doesn't work.
RewriteRule ^http://[www\.?]example.com/(.*)\.aspx$ http://www2.example.com/$1 [R=301,L]
My htaccess file (including the above rule) looks like this:
RewriteEngine On
# redirect all .aspx pages to www2
RewriteRule ^http://[www\.?]example.com/(.*)\.aspx$ http://www2.example.com/$1 [R=301,L]
# strip index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Does anyone have a solution for this?
The RewriteRule directive does only test the URL path. If you want to test any other part of the requested URL, you need to use the RewriteCond directive:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^(.*)\.aspx$ http://www2.example.com/$1 [R=301,L]

Resources