Host Primary Domain from a subfolder - apache

I am having a problem making a sub directory act as the public_html for my main domain, and getting a solution that works with that domains sub directories too.
Background
My hosting allows me to host multiple sites, which are all working great. I have set up a subfolder under my ~/public_html/ directory called /domains/, where I create a folder for each separate website. The folder structure on my server looks something like this:
public_html
domains
websiteone
websitetwo
websitethree
...
This keeps my sites nice and tidy. The only issue was getting my "main domain" to fit into this system. It seems my main domain, is somehow tied to my account (or to Apache, or something), so I can't change the "document root" of this domain. I can define the document roots for any other domains ("Addon Domains") that I add in cPanel no problem. But the main domain is different.
The problem
I was told to edit the .htaccess file, to redirect the main domain to a subdirectory. This seemed to work great, and my site works fine on it's home/index page.
The problem I'm having is that if I try to navigate my browser to say the images folder (just for example) of my main site, like this:
www.yourmaindomain.com/images/
then it seems to ignore the redirect and shows the entire server directory in the url, like this:
www.yourmaindomain.com/domains/yourmaindomain/images/
It still actually shows the correct "Index of /images" page, and shows the list of all my images. It's just the "pretty" URL that's the problem.
Example of my .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteCond %{REQUEST_URI} !^/domains/yourmaindomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domains/yourmaindomain/$1
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteRule ^(/)?$ domains/yourmaindomain/index.html [L]
Does this htaccess file look correct? I just need to make it so my main domain behaves like an addon domain, and it's subdirectories adhere to the redirect rules.

Don't use mod_rewrite for that, use virtual hosts for that. I won't explain that here in detail, you'll find all the necessary information at the above link. (Hint: You'll need the name-based ones probably.)
You probably want to move your stuff out of ~/public_html/ to some more generic place like /var/www/ or /srv/www.
Besides that: Don't use .htaccess files on production.

I have it organized the same way yo do. Had the same issue with my main domain.
This .htaccess works for me and even solves the ugly URL thing.
# .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} !^/subdirectory/
# 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 ^(.*)$ /subdirectory/$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 ^(/)?$ subdirectory/ [L]

Related

301 redirect not working properly (my guess is b/c I'm dealing with Add-on domains)

I have a primary/account domain (PrimaryDomain.com) and a couple add-on domains (AddOnDomain1.com and AddOnDomain2.com) I've recently switched the content from AddOnDomain1.com to AddOnDomain2.com and I want everything to redirect to the exact same location (folders, files, images, etc.)
Here's what I have in .htaccess of my public_html folder (for PrimaryDomain.com):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.AddOnDomain1\.com$
RewriteRule (.*) http://AddOnDomain2.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^AddOnDomain1\.com$
RewriteRule (.*) http://AddOnDomain2.com/$1 [R=301,L]
It trys to redirect but here's what happens (with an image as an example). When I enter:
http://AddOnDomain1.com/img/logo.png
It redirects to:
http://AddOnDomain2.com/AddOnDomain1/img/logo.png
Notice how it adds that folder (in bold)?...which of course is the add-on domain directory name. Anyone aware of a fix for this scenario?

Moving domain to subdirectory with CPanel

I recently moved several of my domains to GoDaddy, and am currently struggling to get the directory structure properly set up with each of them. I have each of my addon domains in its own directory under public_html/. Additionally, I want to move my primary domain into its own subfolder for cleanliness. As a result, my directory setup looks like this:
public_html/
primarydomain/
addondomainA/
addondomainB/
addondomainC/
I setup my .htaccess file under public_html as follows:
RewriteEngine on
# Rewrite direct addondomain access to their proper domains
RedirectMatch ^/addondomainA.com/(.*)$ http://addondomainA.com/$1
RedirectMatch ^/addondomainB.com/(.*)$ http://addondomainB.com/$1
RedirectMatch ^/addondomainC.com/(.*)$ http://addondomainC.com/$1
# Rewrite primary domain access to the primarydomain/ folder
RewriteCond %{HTTP_HOST} ^(www.)?primarydomain.com/
RewriteCond %{REQUEST_URI} !^/primarydomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /primarydomain/$1
RewriteCond %{HTTP_HOST} ^(www.)?primarydomain.com$
RewriteRule ^(/)?$ primarydomain/index.html [L]
The addon domain redirects work perfectly, and if I try to access primarydomain.com, the request is properly rewritten to the primarydomain/ subdirectory. However, if I try to access primarydomain.com/primarydomain, nothing gets rewritten (as far as I can tell) and the user can directly access the page. I would like to disallow all direct access requests for the primarydomain/ folder, but nothing I try seems to work. I thought that removing:
RewriteCond %{REQUEST_URI} !^/primarydomain/
Would do something, but it seems to have no effect on the behavior. Similarly, both:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
...seem to have no effect if deleted. I even tried adding a fourth RedirectMatch clause up top, hoping to catch the folder name if it's inserted, but it leads to a redirect loop, as if it's processing the commands out of order. I've devoured all the answers I could find on stack overflow, but none of them solve the issue. I've tried multiple fresh browsers, and am pretty certain that this is not a cache problem.
Does anybody have any ideas how to accomplish this seemingly super-basic task? Is there something I've overlooked? Thanks in advance.
you could try using a cname back to the proper directory (cname in advanced dns in cPanel)

Apache mod_rewrite fake pushState directory mapping

If have played around a lot with mod_rewrite rules in my httpd.conf file. Regardless of my research i haven't been able to get a couple of things working.
This is my file structure:
/
-index.php
-app.php
/css
-style.css
/js
-script.js
The server should either serve the index.php (home page) or app.php (single application page). Both the script and style files are included in both php files.
Goals
My domain domain.com should serve the index.php, the address bar should show www.domain.com
(This seems to work already, per default.)
The subdomain domain.com/a should be changed to domain.com/a/ if necessary. This domain should serve the app.php file.
(This is already working to an extend. One problem is that the relative links inside app.php are wrong, because the file "thinks" it is in a subdirectory instead of root. This I would like to change)
Anything after domain.com/a/ e.g. domain.com/a/user/10 should stay in the address bar and serve the app.php as usual. Ideally, to preserve relative links again, the file should "know" it is in the root folder.
(This is in order to support a "fake" pushState server) EDIT Clarification: Everything after the /a/ will be interpreted by my Javascript app. When the client clicks a link like domain.com/a/user/10 there will be no extra request to the server.
Bonus
Add trailing slashes to all URLs except the root url.
e.g. turn domain.com/a/user/10 into domain.com/a/user/10/
Add www to URL in case it is missing.
What I've tried
-add www
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]
-add slashes
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301]
-redirect /a/
RewriteCond %{REQUEST_URI} ^(/a/)$
RewriteRule ^ /app.php
Any pointers or help are greatly appreciated! Thanks.
Edit
I have used this tool http://htaccess.madewithlove.be/ to test some conditions.

Yii framework: Using htaccess for site in sub directory not working on Hostgator

I'm trying to set up Yii site to be loaded from sub directory to root domain. In my root site folder I have only root .htaccess file and sub directory "subdir" which contains Yii site. I found a solution that works on my local environment:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) subdir/index.php/$1 [QSA,L]
But when I upload the site to HostGator it just does not work correctly
For example
if I use http://localhost/contact on my local environment, correct page is opened (called site/contact - site controller and contact action. I've added Yii 'contact'=>'site/contact' rule to the urlManager, so both http://localhost/contact and http://localhost/site/contact can work)
If I use http://mydomain.com/contact or (http://mydomain.com/site/contact) on HostGator, I get default index page (called site/index - site controller and default index action instead)
When I choose to access subsites directly like http://mydomain.com/subdir/contact it works fine, but it does not work if I use http://mydomain.com/contact
I guess that I need somehow to change this last rule in htaccess, but not sure how. Thanks!
I've found alternative solution that works. I was forced to use old fashioned URLs instead of paths, so I've changed my urlManager to:
'urlManager'=>array(
//'urlFormat'=>'path',
'showScriptName'=>true,
'caseSensitive'=>false,
'rules'=>array(
...
)
)
And my root htaccess looks now like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdir/index.php?r=$1 [QSA,L]
So, when accessing
http://mydomain.com/site/contact
Actual mapped URL is
http://mydomain.com/subdir/index.php?r=site/contact
which as result returns correct contact page
Since, you stated in your previous question that www.mysite.com/mysite/frontend/www/controller/action works fine; you should be using:
AddHandler application/x-httpd-php53s .php .html
Options +SymLinksIfOwnerMatch
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !^/mysite/frontend/www
RewriteRule ^(.*)$ /mysite/frontend/www/$1 [L]
You dont need to edit .htaccess. You just need to move the Yii entry script (index.php) and the default .htaccess up from the subdirectory to the webroot (so that they reside directly under public_html). Once you move index.php and .htaccess to the root directory, all web requests will be routed directly to index.php under webroot (rather than to the subdirectory), thus eliminating the /subdirectory part of the url.
After you move the files, you will need to edit index.php to update the references to the yii.php file (under the Yii framework directory) as well as the Yii config file (main.php). Lastly, you will need to move the assets directory to directly the webroot, since by default, Yii expects the assets directory to be located in the same location as the entry script).
That should be all you need to do, but if you need more details, I describe the approach fully here:
http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory

mod-rewrite to ignore the subdomain

I'm using a mod-rewrite for pretty URLs, meant to run on the domain root. Working fine but now I'm trying to make it run on a subdomain and it keeps giving "500 Internal Server Error".
The subdomain automatically redirects to the folder with that name on my hosting account (sub.domain.com shows the content of domain.com/sub/). Does it fail because this request is already being mod-rewritten automatically or can I simply change something in the htaccess to address the subdomain instead?
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
Contact your ISP and check if they are setting the DocumentRoot for the sub domains to the /domain/sub/ directory (it's probable) or alternatively using an internal RewriteRule to direct traffic to that directory (you can see if there's an external rewrite / redirect in place via Chrome or Firefox + firebug, use the developer tools to check the response header, on the network Tab). If they have set the document root you will need to copy or symbolically link ALL the files you want accessible via the sub-domain, to the /domain.com/sub/ directory e.g. the .htaccess, index.php, images, js and css files and sub directories. If they are using an internal rewrite, a quick tweak to your own existing internal rewrite, in the existing .htaccess file, should suffice e.g.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]