简体   繁体   English

htaccess从子文件夹重定向到新域

[英]htaccess redirect from subfolder to new domain

I have 2 websites, one hosted in a root directory (root.com), and one hosted in a subdirectory (root.com/sub). 我有2个网站,一个托管在根目录(root.com)中,另一个托管在子目录(root.com/sub)中。 I use Host Gator shared, cPanel, and these are both WordPress websites. 我使用Host Gator共享,cPanel,这些都是WordPress网站。

I have recently purchased a domain and changed the DNS for the subdirectory website (root.com/sub to sub.com). 我最近购买了一个域并更改了子目录网站的DNS(root.com/sub到sub.com)。

I want to create a 301 redirect for all traffic going to root.com/sub to sub.com 我想为所有要转到root.com/sub到sub.com的流量创建301重定向

What I have tried for both sub.com/.htaccess and root.com/.htaccess: 我为sub.com/.htaccess和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. 我已经使用http://htaccess.madewithlove.be/对其进行了测试,并且可以在其中运行,但是在服务器上时没有任何作用。

In sub.com/.htaccess 在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. 这就是修复它的原因。

Good Day Everybody, I hope this solution will help you to Redirect Domain from Subfolder. 大家好,我希望此解决方案将帮助您从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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM