简体   繁体   中英

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.

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:

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? Ideally I want manage the redirections with an htaccess file.

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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