【问题标题】:Redirect user to the secure connection when hit the domain访问域时将用户重定向到安全连接
【发布时间】:2018-06-27 09:06:15
【问题描述】:

我在 .htaccess 中写了一条规则,它根据要求运行良好,现在要求略有变化,我很困惑如何通过更改我以前的规则来实现这一点,第一个有效的规则是当用户点击域 example.com 或 www.example.com 时,用户应该被重定向到

https://www.example.com/blog/

此规则现在有效,下一个要求是当用户点击域时

http://www.hkdcrandom.com/blog/lorem-ipsum/

http://www.hkdcrandom.com/blog/lorem-ipsum/

他应该重定向到

https://www.hkdcrandom.com/blog/lorem-ipsum/

但这件事在这里不起作用是我到目前为止写的规则。

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# END WordPress

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

谁能告诉我我做错了什么

【问题讨论】:

  • 所以基本上你希望 url 始终是 https?
  • 是的,但我需要更多的条件才能运行
  • 就像当用户点击example.comwww.example.com 他应该重定向到https://www.example.com/blog/ 这个条件是有效的条件是不工作的条件是当用户点击像http://www.hkdcrandom.com/blog/lorem-ipsum/ 这样的域时他不是重定向到安全连接
  • 试试这个 --> RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  • 不,先生,这不起作用

标签: php apache .htaccess redirect mod-rewrite


【解决方案1】:

我们目前使用

<IfModule mod_rewrite.c>
    # Force https and www
    RewriteCond %{SERVER_PORT} !=443
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

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

    # The rest of your rules
    RewriteBase /blog/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /blog/index.php [L]
 </IfModule>

【讨论】:

  • 这在这种情况下不起作用http://www.example.com/blog/lorem-ipsum/
  • 而且它还重定向了 4 次
  • 我稍微编辑了顶级条件和规则。我认为这是因为 RewriteBase 规则http://www.example.com/blog/lorem-ipsum/ 不起作用。并且您正在检查文件或目录是否不是由 url 定位的,但 ^index\.php$ - [L] 在检查条件之前放入文件?
  • 那我想我帮不了你:(
  • 没什么好伤心的 会以一种或其他方式解决这个问题 实际上有一个肯定的捷径可以这样做,但不推荐这样做,或者你可以说这是一个非常糟糕的做法
猜你喜欢
  • 2023-02-16
  • 2016-05-16
  • 1970-01-01
  • 2020-02-22
  • 2021-11-05
  • 1970-01-01
  • 1970-01-01
  • 2011-09-19
  • 1970-01-01
相关资源
最近更新 更多