【问题标题】:Redirect To Root Domain With HTACCESS使用 HTACCESS 重定向到根域
【发布时间】:2017-08-12 02:29:11
【问题描述】:

所以我正在为当地学校制作一个小型教育网站。现在,该站点托管在一个用于开发和测试的子域中。然而,不知何故,谷歌设法抓取了我的网站,所以每当我用谷歌搜索主题和网站名称时,子域都会显示在结果中。

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

这是包含子域中的 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...

我基本上想将用户重定向到相同的路线,但在主域上。任何人我如何才能实现这一目标并正确管理我的重定向,以便我的 SEO 排名不会受到伤害?理想情况下,我想使用 htaccess 文件管理重定向。

【问题讨论】:

    标签: php apache .htaccess redirect


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 我必须把它放在我的子域的 htaccess 文件中吗?
    • 我将完全删除子域并添加我在站点的主 .htaccess 文件中发布的内容。如果您需要保留子域,您可以粘贴我发布的内容,或者只粘贴 RewriteRule,因为 RewriteCond 将始终在您的子域中得到满足。
    • 啊,好吧,还有一个问题。我看到 RewriteRule 在域名前有 en https。如果我没有 SSL 证书,我应该像这样留下它:RewriteRule .* http://example.com%{REQUEST_URI} [R=301,L],协议中没有 s
    猜你喜欢
    • 2012-11-18
    • 1970-01-01
    • 2016-05-16
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 2014-08-19
    • 2013-02-20
    相关资源
    最近更新 更多