【问题标题】:htaccess redirect non-www to www the full pathhtaccess 将非 www 重定向到 www 的完整路径
【发布时间】:2017-06-20 14:53:16
【问题描述】:

我想将example.com/test 重定向到www.example.com/testexample.com/contactwww.example.com/contact

网络的所有路线也是如此。 我找到的所有东西,它所做的都是将example.com/test 重定向到www.example.com

以下是我的代码,我尝试过的所有方法都发生在我身上。他们工作正常,但他们没有做我想做的事。

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

请有任何建议。 谢谢!

【问题讨论】:

标签: php .htaccess redirect mod-rewrite no-www


【解决方案1】:
//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]

【讨论】:

    【解决方案2】:

    试试下面的规则,

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    

    【讨论】:

      【解决方案3】:
      【解决方案4】:

      方法一

      RewriteEngine On
      RewriteBase /
      RewriteCond %{SERVER_NAME} !^www\.
      RewriteRule ^(.*)$ http://www.%{SERVER_NAME}/$1 [R,NC,L]
      

      方法二

      RewriteEngine On
      RewriteBase /
      RewriteCond %{HTTP_HOST} !^www\.
      RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R,NC,L]
      

      您可以使用这两种中的任何一种。两者之间的唯一区别是%{SERVER_NAME}%{HTTP_HOST}

      希望对你有帮助!

      【讨论】:

        【解决方案5】:

        给网址添加www前缀很简单,请试试这个

        RewriteEngine on
        RewriteCond %{HTTP_HOST} !^www\.
        RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 
        

        希望对您有所帮助。您可能想访问此链接以了解更多信息 https://www.a2hosting.com/kb/developer-corner/apache-web-server/adding-or-removing-the-www-prefix-in-domain-urls

        【讨论】:

          猜你喜欢
          • 2012-11-16
          • 2011-04-12
          • 2015-12-31
          • 1970-01-01
          • 1970-01-01
          • 2012-08-16
          • 1970-01-01
          相关资源
          最近更新 更多