Change the document root of a parked domain? - .htaccess

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

Related

.htaccess rewriterule does not redirect

My website is located in a subfolder (gng2) of my public_html folder. I used the second part (##2) of the below .htaccess file to rewrite the url to add the gng2 subfolder to the URI. It works fine, the app loads when I enter my url to the browser.
Now I added the first part (##1)to redirect any requests where the url contains the subfolder as well. I want www.staging.gonativeguide.com/gng2/en to be redirected to www.staging.gonativeguide.com/en. However this does not work: the first url does not get redirected to the second. Checking the below code on htaccessTester, it says it is correct and it should redirect.
My websited is hosted by a shared hosting service and I think the web server is nginx. Any idea why the redirect does not work?
RewriteEngine On
## 1. redirect request when it contains the gng2 subfolder.
RewriteCond %{HTTP_HOST} ^staging.gonativeguide.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.staging.gonativeguide.com$
RewriteCond %{REQUEST_URI} gng2/
RewriteRule gng2/(.*) www.staging.gonativeguide.com/$1 [R=301,L]
## 2. rewriting url to add the gng2 subfolder containing the app.
RewriteCond %{HTTP_HOST} ^staging.gonativeguide.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.staging.gonativeguide.com$
RewriteCond %{REQUEST_URI} !gng2/
RewriteRule (.*) /gng2/$1 [L]
There are a number of issues with your current attempt. So I sketched an alternatate version which simplifies the rules and enhances robustness:
RewriteEngine On
## 1. redirect request when it contains the gng2 subfolder.
RewriteCond %{HTTP_HOST} ^(www\.)?staging\.gonativeguide\.com$
RewriteRule ^/?gng2/(.*)$ https://www.staging.gonativeguide.com/$1 [R=301]
## 2. redirect request that do not contain the "www" prefix in the host name.
RewriteCond %{HTTP_HOST} ^staging\.gonativeguide\.com$
RewriteRule ^ https://www.staging.gonativeguide.com%{REQUEST_URI} [R=301]
## 3. rewriting url to add the gng2 subfolder containing the app.
RewriteCond %{HTTP_HOST} ^(www\.)?staging\.gonativeguide\.com$
RewriteCond %{REQUEST_URI} !^/gng2/
RewriteRule ^ /gng2%{REQUEST_URI} [END]
You need to make absolutely sure that you are not looking at cached results when testing. So always test using a fresh anonymous browser window and use "deep reloads" (CTRL-F5 typically) instead of just reloading.

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.

How to rewrite or redirect in subdirectory with htaccess?

I have difficulties to understand how htaccess rewrite / redirect is working. I have been trough already more than 20 article about it but I don't catch the logic and the syntax of how to write rules properly.
I currently have a server with public_html folder
We have our main site under the subfolder /site , this is a wordpress site
We have other site (also a wordpress site) we use for e-learning under the subfolder /training
I need to make our site (domain.com) to point to /public_html/site while having domain.com/learning to point to public_html/training
I have tried to understad how queries in htaccess are working such as RewriteCond or RewriteRule
Here is my current code:
# Use PHP5.4 Single php.ini as default
AddHandler application/x-httpd-php54s .php
# File modified on Sun Mar 30 01:22:23 2014 by server
# For security reasons, mod_php is not used on this server. Use a php.ini file for php directives
# php_value max_execution_time 90
# BlueHost.com
# .htaccess main domain to subdirectory redirect
# Do not change this line.
# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymLinks
Options +SymLinksIfOwnerMatch
RewriteEngine on
#RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
#RewriteCond %{REQUEST_URI} !^/site/
# 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 ^(.*)$ /site/$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.)?domain.com$
RewriteRule ^(/)?$ site_en/index.html [L]
# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change domain.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
# Change 'site to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/learning/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'site' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /learning/$1
# Change domain.com to be your main domain again.
# Change 'site' 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.)?domain.com$
RewriteRule ^(/)?$ learning/index.html [L]
The following code will need to be added to the .htaccess file in the public_html folder of your hosting account.
You will need to insert the following code block and make modifications as noted in the (#) comments.
# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change domain.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
# Change 'site to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/site/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'site' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /site/$1
# Change domain.com to be your main domain again.
# Change 'site' 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.)?domain.com$
RewriteRule ^(/)?$ site/index.html [L]
Repeat Same for Learning domain .
Visitors to your Web site will not be able to tell that your main domain is using a subdirectory, they will still see the Web site address as
http://www.domain.com/index.html.
Note :
Please note that this will not work with some website software. You
may also need to modify the $base_url, $live_site or other
configuration settings to finish the process.
Since you mentioned that your case is WordPress , don't forget to have a look at this too
https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory
Update :
RedirectMatch 301 /learning/(.*) /training/$1
# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change domain.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
# Change 'site to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/site/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'site' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /site/$1
# Change domain.com to be your main domain again.
# Change 'site' 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.)?domain.com$
RewriteRule ^(/)?$ site/index.php [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 ############################

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