【问题标题】:Apache2 rewrite URL in .htaccessApache2 重写 .htaccess 中的 URL
【发布时间】:2011-08-08 08:13:16
【问题描述】:

我想用 www.example.org、https://www.example.orghttp://example.org 将任何请求重写为 https://example.org

有简单的方法吗?

我目前在我的 .htaccess 中执行以下操作,但是 https://www.example.org -> https://example.org 的情况不起作用。

  1. 有什么想法可能会出错吗?
  2. 还会因为这种重定向而影响任何搜索引擎排名。
  3. 这是 URL 重定向的最佳实践吗?

我当前的 .htaccess 看起来像这样。

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} example\.org
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://example.com%{REQUEST_URI}
</IfModule>

谢谢

【问题讨论】:

    标签: .htaccess url-rewriting apache2 url-routing


    【解决方案1】:

    在你的 .htaccess 文件中试试这个:

    Options +FollowSymLinks
    RewriteEngine on
    
    # redirect for http
    RewriteCond %{HTTP_HOST} ^(www\.)?example\.org$ [NC]  
    RewriteCond %{SERVER_PORT} =80
    RewriteRule ^/?(.*)$ https://example.org/$1 [R=301,QSA,L,NE]
    
    # redirect for https
    RewriteCond %{HTTP_HOST} ^www\.example\.org$ [NC]  
    RewriteCond %{SERVER_PORT} =443
    RewriteRule ^/?(.*)$ https://example.org/$1 [R=301,QSA,L,NE]
    

    R=301 将使用 https 状态 301 重定向
    L 将制定最后一条规则
    NE 用于禁止转义查询字符串
    QSA 将附加您现有的查询参数

    $1 是您的 REQUEST_URI

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-30
      • 1970-01-01
      • 2011-08-30
      • 2011-03-19
      相关资源
      最近更新 更多