2017-08-12 102 views
0

所以我正在为当地一所学校开设一个小型教育网站。目前,该网站托管在一个子域中进行开发和测试。然而,不管怎样,谷歌设法抓取我的网站,所以每当我谷歌主题和网站的名称,子域出现在结果中。用HTACCESS重定向到根域

我想将网站移到主域,并将访问子域的用户重定向到主域中的相应路由。

这是包含在子域中的RewriteRules的.htaccess文件:

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] 

因此,这些都是应该发生

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... 

的重定向的一些例子我基本上要重定向用户转到相同的路由,但在主域中。任何人如何才能实现这一点,并妥善管理我的重定向,以便我的搜索引擎优化排名不会受到伤害?理想情况下,我想用htaccess文件管理重定向。

回答

0

试试这个:

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

我必须把这个在我的子域的htaccess的文件? –

+0

我会完全删除子域,并添加我在该网站的主.htaccess文件中发布的内容。 如果您需要保留子域名,您可以粘贴我发布的内容,或者仅仅是RewriteRule,因为在您的子域中总会遇到RewriteCond。 –

+0

啊,好的,还有一个问题。我看到RewriteRule在域名之前有'https'。如果我没有SSL证书,我应该这样写:''RewriteRule。* http://example.com% {REQUEST_URI} [R = 301,L]'没有协议中的's'? –