【问题标题】:Why are clean URLs not working when redirecting to https?为什么重定向到 https 时干净的 URL 不起作用?
【发布时间】:2015-06-11 22:48:46
【问题描述】:

我在我的 htacess 文件中添加了一些行来将所有 http 请求重定向到 https。现在,当我输入如下 URL 时:http://example.com/frames/view/4701362,它会重定向到:https://example.com/index.php?q=frames/view/4701362。我可以手动转到该页面:https://example.com/frames/view/4701362,并且 URL 不会更改。

我设置了干净的 URL

Htaccess 文件

<IfModule mod_rewrite.c>
  RewriteEngine on

  #some more stuff here, unrelated 

  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

  RewriteEngine On
  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>

编辑: 我尝试像这样反转 Drupal index.php?q= 行和 https 行...

  RewriteEngine On
  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

但后来我的网站没有正确加载,并说即使在主页上也找不到页面。

【问题讨论】:

  • 看起来它正在按照您的规则执行。你想让它表现得不同吗?
  • 如果我输入 _http://example.com/frames 我希望它重定向到 _https://example.com/frames 而不是 _https://example.com/index.php?q =frames(忽略下划线)
  • 第一个重写规则负责该更改,第二个负责 HTTPS 切换。看起来您只需要第二个,所以请尝试消除第一个。
  • 我认为这是 Drupal 放在那里的东西
  • 我其实觉得需要切换一下……先HTTPS重定向,再重写到index.php

标签: .htaccess mod-rewrite https drupal-6 clean-urls


【解决方案1】:

感谢上面的@MikeRockett,我能够弄清楚这一点。除了他的建议之外,我还必须在 RewriteRule 行中添加 [L,R=301]

<IfModule mod_rewrite.c>
  RewriteEngine on

  #some more stuff here, unrelated 

  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]  

  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

</IfModule>

【讨论】:

  • 确实 - 这是正确的。不过,我认为只需要 L 标志,因为通过指定完整的 URL 来暗示重定向。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-22
  • 2013-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多