0

I have 2 websites, one hosted in a root directory (root.com), and one hosted in a subdirectory (root.com/sub). I use Host Gator shared, cPanel, and these are both WordPress websites.

I have recently purchased a domain and changed the DNS for the subdirectory website (root.com/sub to sub.com).

I want to create a 301 redirect for all traffic going to root.com/sub to sub.com

What I have tried for both sub.com/.htaccess and root.com/.htaccess:

RewriteEngine ON
RewriteCond %{HTTP_HOST} root\.com
RewriteCond %{REQUEST_URI} sub
RewriteRule ^sub(.*)$ http://sub.com/$1 [R=301,L]

I have tested it with http://htaccess.madewithlove.be/ and it works there, but it has no effect when on the server.

Daniel
  • 293
  • 4
  • 10

2 Answers2

1

In sub.com/.htaccess

RewriteEngine ON
RewriteBase /sub/
RewriteCond %{HTTP_HOST} root\.com
RewriteCond %{REQUEST_URI} sub
RewriteRule ^(.*)$ http://sub.com/$1 [R=301,L]

This is what fixed it.

Daniel
  • 293
  • 4
  • 10
-2

Good Day Everybody, I hope this solution will help you to Redirect Domain from Subfolder.

Enjoy It!!!!!!!!!!!!!!

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{HTTP_HOST} ^(www.)?domainname.com/$
RewriteCond %{REQUEST_URI} !^/www.domainname.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www.domainname.com/$1
RewriteCond %{HTTP_HOST} ^(www.)?domainname.com$
RewriteRule ^(/)?$ www.domainname.com/index.php [L] 
RewriteRule ^robots.txt - [L]
  • Welcome to Stack Overflow! While this piece of code may answer the question, it is better to include a description of what the problem was, and how your code will tackle the given problem. For the future, here is some information, how to crack a awesome answer on Stack Overflow: http://stackoverflow.com/help/how-to-answer – dirtydanee Dec 25 '16 at 15:48