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

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]

Related

htaccess to redirect root to subfolder

We have a GoDaddy hosting. Within public_html we have a number of folders, each for a different site.
The main site (mysite.com) should normally be in the public_html but we moved them to public_html/mysite. We had an htaccess file to handle this change.
mysite.com is a slim (PHP) site. Within its public folder is another htaccess file that redirects to the public subfolder.
We tried to redirect mysite to a different web address and something got broken. Now if I go to mysite.com it shows Forbidden. Also, the FTP no longer works. When I try to connect with Filezilla I get
Connection attempt failed with "EAI_NONAME - Neither nodename nor servname provided, or not known".
This is because I try to connect to ftp.mysite.com. If I change that to an IP it does work. But clearly something is off with mysite.com.
The current htaccess within public_html/mysite is as follows:
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
RewriteRule ^(?!public)(.*)$ /public/$1 [L]
Any ideas on how can I get this to work again? I think the problem is that the domain is pointing to the root of the public_html folder instead of public_html/mysite but no idea how to fix this.
UPDATE
We have an htaccess file in the public_html folder
RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
RewriteRule ^(?!mysite)(.*)$ /mysite/$1 [L]
If I remove it, all subdomains work but mysite.com shows Forbidden. If I enable the file, then every single subdomain, including mysite.com shows Internal Server Error.
You may have this .htaccess in site root:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
RewriteRule ^(?!mysite/)(.*)$ mysite/$1 [L,NC]
You should remove RewriteOptions inherit directive as that is not serving any purpose here.
Also note some minor fixes in your rewrite rule.

How to redirect to another page using htaccess

I have 2 websites 1 old website and 1 new website. both are on different domains. now the old website had many urls like www.test.com/sadfd/dsf etc etc... all these urls are indexed by google. So when users click these urls I want them to redirect to www.newdomain.com
I had added a redirect using redirect in htaccess but then if the base url is www.domain.com/something/something it will redirect to www.newdomain.com/something/something.. but that url does not exist. I need all visits to redirect to www.newdomain.com
how can I do this?
I'm not the master of the .htaccess file but you can use this snippet, it redirects all pagerequests to the domain without adding the path:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?name-of-domain\.net$ [NC]
RewriteRule ^(.*)$ http://www.new-domain.net/ [R=301,L]

Using Apache, how can I redirect a subfolder of a site to a subdomain but redirect everything but that subfolder to the primary domain?

I have the same website running on Apache at two different subdomains using vhosts.
www.vhost1.com
www.vhost2.com
Whenever someone accesses a particular subfolder on www.vhost1.com, I want the user to be redirected to that same subfolder on www.vhost2.com.
Example:
User accesses www.vhost1.com/my-special-folder
User should be redirected to www.vhost2.com/my-special-folder
And whenever someone is on www.vhost2.com and accesses anything except that subfolder, I want the user to be redirected to www.vhost1.com/any-other-url
Example:
User accesses www.vhost2.com/any-other-url
User should be redirected to www.vhost1.com/any-other-url
Also, if the user accesses www.vhost2.com (with no path), the user should be redirected to www.vhost1.com
Example:
User accesses www.vhost2.com
User should be redirected to www.vhost1.com
I've tried several different Apache redirect rules and have gotten several of the scenarios to work, but I can't get them all to work at the same time.
Because of the server configuration, I must perform the redirects using a .htaccess file.
What should the Apache rules be?
Place this rule in root .htaccess of both domains:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?vhost1\.com$ [NC]
RewriteRule ^(my-special-folder)(/.*)?$ http://www.vhost2.com/$1$2 [L,NC,NE.R=302]
RewriteCond %{HTTP_HOST} ^(www\.)?vhost2\.com$ [NC]
RewriteRule ^((?!my-special-folder/).*)$ http://www.vhost1.com/$1 [L,NC,NE,R=302]

htacces redirect to one specific page depending on .tld AND excluding a specific folder

I can't find the solution to this problem:
I'd like to redirect domain.com and all subdirectories and documents to one specific page: special_info.html
But I want ONLY domain.com to be redirected (NOT domain.de)
AND I want to exclude a specific folder from redirection.
Examples:
domain.com should be redirected to domain.com/special_info.html
domain.de/anything should not be redirected
domain.com/any_directory/any_page.html should be redirected to domain.com/special_info.html as well
but
domain.com/shop should NOT be redirected
Any help is appreciated, Thanks a lot for your time
You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule !^(shop/|special_info\.html) /special_info.html [L,NC,R]
It can be possible by using domain management console , like godaddy's.( I have done it earlier)
Following link may help for your problem , if you have your domain with godaddy.
http://support.godaddy.com/help/article/422/forwarding-or-masking-your-domain-name
Did you try from following,
http://www.branded3.com/blogs/htaccess-mod_rewrite-ultimate-guide/

Redirect for path but only for specific domain

We have different domains that are hosted on our server. Recently one of the main sections of our site has been moved to another server and been given a subdomain:
http://www.mysite.com/store
Has been moved to
http://store.mysite.com
Within our apache VirtualHost we wanted to redirect all traffic from the old domain to the new one:
Redirect permanent /store http://store.mysite.com
The problem is, we have other hosted sites that are being redirected now:
http://www.othersite.com/store
http://api.greatsite.com/store
We don't want this. How can I only have apache do redirects if http://www.mysite.com/store which has the /store path, and ignore other domains with /store?
Use mod_rewrite based code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^/?store(/.*|)$ http://store.mysite.com [L,R=301,NC]