Links are broken when installing CakePHP on shared hosting - .htaccess

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

Related

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 help! wildcard subdomain rewrite rule to a specific folder for only some subdomains

Through a little research I came up with an htaccess rewrite rule in my development directory that takes a wildcard subdomain to its matching instance name in my development folder. I will admit I know little to nothing about this so help is really needed. So the current htaccess behaves as such:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.mydomain\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]
example
instance_folder_name.mydomain.com
redirects to
public_html/development/instance_folder_name
this works great because it allows me to have development websites in the development directory and i dont have to be constantly creating subdomains for each site.
the problem I am having is how to approach the following scenerio:
I use virtual domains as an add on component in Joomla so that several domains are being managed by one single Joomla install. So i may have the following subdomains...
client1.mydomain.com
client2.mydomain.com
client3.mydomain.com
that all need to go to
/public_html/development/client1
I guess what i need is a general rule on how to handle all wildcard subdomains, but with exceptions for client1, client2, client3 for example
You can have a separate rules for these 3 subdomains and add an exclusion condition in the older rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(client1|client2|client3)\. [NC]
RewriteRule ^((?!client1/).*)$ /client1/$1 [L,NC]
RewriteCond %{HTTP_HOST} !^(client1|client2|client3)\. [NC]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.mydomain\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [L]

How to route multiple domains to multiple subfolders

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 ############################

htaccess get domain name from %{HTTP_HOST} into variable, minus the www

I currently have the following in a htaccess file which redirects certain requests to the requested domain's individual folder if the file does not exist in the root..
RewriteRule ^contact/?$ ASSETS/%{HTTP_HOST}/contact.php
RewriteCond %{REQUEST_URI} !^/ASSETS/%{HTTP_HOST}/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /ASSETS/%{HTTP_HOST}/$1
RewriteCond %{HTTP_HOST} ^(www.)?%{HTTP_HOST}$
RewriteRule ^(/)?$ ASSETS/%{HTTP_HOST}/index.php [L]
Which works fine when my domain folders are named like /ASSETS/www.domain1.com, /ASSETS/www.domain2.com etc
However, I've built a lot of my code already with the assumption that the folders will be labeled /ASSETS/domain1.com etc (domain name, minus the www.)
I'm thinking there is a way to get the domain name from %{HTTP_HOST} into a new variable and strip out the www. part (like an str_replace or something), but all the answers I have found are around a basic redirect all to www. or no www. which isn't going to work as I still want users to access the site from the forced www. version.
I was previously running the rules for each domain individually with a RewriteCond %{HTTP_HOST} ^(www.)?domain1.com$ but there are so many domains now, it's not feasible to do it for each one.
Hope you can help! Thanks in advance.
You can capture the needed host part with a RewriteCond and use it as %1 in the RewriteRule
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^$ ASSETS/%1/index.php [L]
An alternative solution at the file system level (without htaccess) might be symbolic links. Instead of extracting the necessary host part, have symbolic links to the real directory, e.g.
- ASSETS -+-domain1.com (directory)
|
+-www.domain1.com -> domain1.com (symbolic link)
|
+-domain2.com (directory)
|
+-www.domain2.com -> domain2.com (symbolic link)
To illustrate the different back references, see another example
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^foo/(.+)$ $1/%1/index.php [L]
Here we have a $1, which takes the capture group from the RewriteRule pattern foo/(.+).
And we have a %1, which takes the capture group from the previous RewriteCond (?:www\.)?(.+).
Note that (?:...) is a non-capturing group. This is why we use %1 instead of %2.

.htacces Redirect to different based on subdomain

I have a question.
I am trying to setup a TEST, CERT and PROD webspace on my hosting with 1 domain.
The problem is that I am already using subdomains in my webpage so DNS wildcard record configuration is not possible.
The url looks as following on PROD:
company1.domain.com or company2.domain.com
Both pointing to the same directory on the hosting.
What I want to achieve is:
company1.domain.com -> points to directory1
company1.test.domain.com -> points to directory2
company1.cert.domain.com -> points to directory3
"company1" must be a wildcard.
Additionally I would not like to see the directory being displayed in the URL.
Anyone has an idea on how to accomplish this?
Should I remove existing DNS wildcard records?
Ideally, you should be making each of these subdomains into a separate vhost and point each vhost to its own directory. Some hosting companies even allow you to assign subdomains to separate directories.
But if for some reason, they all need to point to the same document root, try adding these rules to the htaccess file in the document root:
RewriteEngine On
# for company1
RewriteCond %{HTTP_HOST} company1.domain.com [NC]
RewriteCond %{REQUEST_URI} !^/directory1
RewriteRule ^(.*)$ /directory1/$1 [L]
# for company1.test
RewriteCond %{HTTP_HOST} company1.test.domain.com [NC]
RewriteCond %{REQUEST_URI} !^/directory2
RewriteRule ^(.*)$ /directory2/$1 [L]
# for company1.cert
RewriteCond %{HTTP_HOST} company1.cert.domain.com [NC]
RewriteCond %{REQUEST_URI} !^/directory3
RewriteRule ^(.*)$ /directory3/$1 [L]

Resources