简体   繁体   English

使用HTACCESS重定向到根域

[英]Redirect To Root Domain With HTACCESS

so I'm making a small educational site for a local school. 所以我正在为当地学校制作一个小型教育网站。 Right now the site is hosted in a sub-domain for development and testing. 现在,该站点托管在一个子域中,用于开发和测试。 However, somehow Google managed to crawl my site so whenever I google a topic and the website's name, the sub-domain shows up in the results. 但是,以某种方式Google设法抓取了我的网站,因此每当我搜索主题和网站名称时,结果中都会显示该子域。

I want to move the site to the main domain and redirect the users visiting the sub-domain to the corresponding route in the main domain. 我想将站点移到主域,并将访问子域的用户重定向到主域中的相应路由。

This is the .htaccess file containing the RewriteRules in the sub-domain: 这是.htaccess文件,在子域中包含RewriteRules:

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^log-in$ index.php?section=log-in [QSA]
RewriteRule ^register$ index.php?section=register [QSA]
RewriteRule ^courses$ index.php?section=courses [QSA]
RewriteRule ^contact$ index.php?section=contact [QSA]
RewriteRule ^instructor$ index.php?section=instructor [QSA]
RewriteRule ^my-profile$ index.php?section=my-profile [QSA]

RewriteRule ^404$ index.php?section=404 [QSA]

RewriteRule ^course/([0-9]+)$ index.php?section=course&course_id=$1 [QSA]
RewriteRule ^course/([0-9]+)/lecture-id/([0-9]+)$ index.php?section=course&course_id=$1&lecture_id=$2 [QSA]

RewriteRule ^articles$ index.php?section=articles [QSA]
RewriteRule ^articles/([a-zA-Z0-9\-]+)$ index.php?section=articles&article_category=$1 [QSA]
RewriteRule ^article/([a-zA-Z0-9\-]+)$ index.php?section=article&slug=$1 [QSA]

RewriteRule ^log-out$ res/php/user_actions/logout.php [QSA]

So these are some examples of the redirections that should happen 这些是应该发生重定向的一些示例

sub.domain.com          => domain.com
sub.domain.com/courses  => domain.com/courses
sub.domain.com/course/5 => domain.com/course/5
sub.domain.com/articles => domain.com/articles
sub.domain.com/article/article-name => domain.com/article/article-name
and so on...

I basically want to redirect the user to the same route but on the main domain. 我基本上想将用户重定向到相同的路由,但是在主域上。 Anyone how I can achieve this and manage my redirections properly so my SEO rankings wont get hurt? 有人可以实现此目标并正确管理我的重定向,以便我的SEO排名不会受到影响吗? Ideally I want manage the redirections with an htaccess file. 理想情况下,我想使用htaccess文件管理重定向。

Try this: 尝试这个:

RewriteCond %{HTTP_HOST} ^sub\.example\.com [NC]
RewriteRule .* https://example.com%{REQUEST_URI} [R=301,L]

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

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