.htaccess or cPanel redirects. Redirect pagename in folder to subdomain/pagename (all folder contents) - apache

I am trying to get a redirect set up for my site. I have three subdomains which I use
1) (www.)mysite.com
2) sub.mysite.com
3) anothersub.mysite.com
I initially didn't use subdomains and had all my pages in a www.mysite.com/*.php format.
I am trying to add a redirect to make all the pages under the /myfolder/ folder redirect to sub.mysite.Com/ (NB sub is the same name on my site as the myfolder folder)
I have tried setting up a redirect through cPanel but all the combinations have resulted in 404 errors when I try them. I have also tried various things in .htaccess which resulted in many 404 errors.
I added a simple redirect in cPanel and get the following
e.g.
www.mysite.com/myfolder/myfolder_mypagename.php
should redirect to
sub.mysite.Com/myfolder_mypagename.php
but instead it redirects to
sub.mysite.com/_mypagename.php (missing out the "myfolder" part of the pagename)
Can you assist me in this please? It's driving me bonkers and my hosting company said it wasn't possible (which I find hard to believe!)

This answer is assuming your are facing the issue that, when creating a subdomain through cpanel and pointing it to the desired directory, some part of the urls get striped.
Create you subdomain through cpanel, and make it point to the root directory (just like www.mysite.com), then use this in your htaccess :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$
RewriteRule ^myfolder/(.*)$ http://sub.mysite.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^sub\.mysite\.com$
RewriteRule ^(.*)$ myfolder/$1 [L,QSA]

Related

How do I redirect an entire site to a new domain using .htaccess?

I have a site that is moving to a new URL, an exact copy of the original with the same pages and URL structure. I'm working with the old domain's .htaccess file to redirect to the new one, and have this:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldsite.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.com$
RewriteRule (.*)$ http://newsite.ca/$1 [R=301,L]
The redirect works when I go to oldsite.com or www.oldsite.com works as expected, and redirects to newsite.ca
However, when I go to something like oldsite.com/blog/, no redirect happens. It just stays at the same URL.
I want to be able to redirect every page to the new site, not just the root.
oldsite.com/blog/ should redirect to newsite.ca/blog/
I need this for the whole site without having to go through and redirect every single page individually. Is this possible?
Figured it out-
I moved my code to the top of the .htaccess file.
The code wasn't being executed because there was a www to non-www before it. Apparently the code is run in order, and if a redirect is found, it will not run another kind of redirect following it.

The folder "www" doesn't seem to work properly on hostgator

I've developed a website using Yii framework and now I need to move it to hostgator cheap hosting for a single site. It looks like it expects that the website must be placed into the root folder but my website has a www folder with index.php and resource files like js,css,images,etc. Also this folder contains a file htaccess with following content:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
But I suppose I have to add another htaccess into the root folder so all requests will redirect to the www folder. (I did it and tried different combinations of the rewrite rules but they didn't work properly). I'm guessing I should remove htaccess from the www folder and leave only one in the root folder.
Sorry guys, I always have problems with htaccess even after reading documentation.
Thanks in advance.
I think that you can configure your domain as an addon domain with the document root set in www as you prefer.
Of course I am presuming that you have cPanel on Hostgator.

Point add-on domain to a subfolder and redirect direct access to that subfolder to add-on domain using .htaccess

I have a site example.com/main and another site example2.com.
I have set up an add-on domain for example2.com which shows files in the main subfolder in the example.com domain. What I want is that when users go to example2.com, they REMAIN on example2.com while they see files from example.com/main. So if they access other files like main/contact.php, it shows as example2.com/contact.php.
I have successfully done what I mentioned above in the add-on page of cPanel.
My issue:
I want it so that when users visit example.com/main directly, I want them to be redirected to example2.com and then see the main subfolder. I have had no luck as I keep getting stuck in a loop.
Doing Redirect 301 / http://example2.com/ doesn't work.
Thank you.
You can use:
RewriteCond %{HTTP_HOST} example\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/+main[\s/?] [NC]
RewriteRule ^main(/.*)?$ http://example2.com$1 [NC,R=301,L]

htaccess | redirect 301 all files to homepage

Sorry to be asking yet another htaccess question but I have searched at length and can't crack it.
I have changed my website to a single page, www.mysite.com/index.html. Previously there were subdirectories and sub-subdirectories, all with a combination of html and other files in them.
I now want all file requests to be directed to the homepage, and for the homepage to display in the browser bar. Eventually I want to remove the old directories and images, but I'm assuming for now that Google needs me to keep them in order to transfer any ranking (is this right?).
There is just one exception to the rule that all traffic should go to www.mysite.com. I have bought another domain www.mysite2.com for SEO reasons. Currently this site has no hosting and is set to redirect to www.mysite.com. If users find www.mysite2.com, I would like them to see content from www.mysite.com but display the domain name www.mysite2.com.
This is the htaccess code I am currently using:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^?]*)$ /index.html?path=$1 [NC]
Options -Indexes
IndexIgnore *
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule ^(.*) http://mysite2.com/$1 [P]
I have several questions/problems:
1) the www.mysite2.com redirect threw a 404 file not found error when i tried to access www.mysite.com. (I am having to test in live production... not great).
2) typing in www.mysite.com/products/ (former directory, still existing) takes me to www.mysite.com/// - it works but is unsightly.
3) typing in www.mysite.com/products/subdir/file.html still takes me to that original file
4) typing in www.mysite.com/lsdhfskdhf (i.e. a non-existent location) displays the homepage but continues to show the string.
5) typing in www.mysite.com/products/ksjdhfkdsjfh (i.e. non-existent location) displays the homepage but without any css formatting (!).
6) Should i be using Redirect 301 instead of RewriteRule? I heard that Google only likes 301 redirect.
I am a bit stuck and would welcome any help! Even if there is a comprehensive guide somewhere to whatever language apache is using in its directories that would be good. Lots of sites claim to this but i haven't found a good one.
Apologies for length.
Thanks
Emma

multiple domains with htaccess rewrite rule

I'm having difficulties getting an htaccess to work with a subdomain.
my server structure:
root / index.php ---//codeigniter index file, for application A, main domain points here.
root/staging/StagingWebsite ---// my subdomain is pointing here.
the folder StagingWebsite has a file called temp.html
moving on, my root htaccess file is this :
RewriteEngine on
RewriteCond
RewriteCond $1 ^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Which turns any access to MyDomain/foo to MyDomain/index.php/foo. (without showing the index.php).
The problem:
When I try to access my subdomain/temp.html, I get a 500 internal error.
when I remove my root/.htaccess, it works fine. So it's obviously my htaccess file.
I've figured out the problem is that the root/.htaccess rule is being applied to the subdomain, which breaks everything, But I have no ideahow to sort it out.
I've placed an empty .htaccess file inside the root/staging/StagingWebsite hoping it would just over-write any previous htaccess settings, But that didn't work.
EDIT
I fixed the issue specifically but I don't like the solution.
I added a RewriteCond to only run the rewrite rule for as specific domain.
Is there a way to solve this without specifying a domain?
Create /root/staging/StagingWebsite/.htaccess with this line only:
RewriteEngine on
This will overwrite any parent's rewrite rules.