2017-08-12 101 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'? –