【问题标题】:Apache - how to make http requests into https only? [duplicate]Apache - 如何仅将 http 请求转换为 https? [复制]
【发布时间】:2016-08-30 03:31:18
【问题描述】:

如何判断http://ttt.comhttp://www.ttt.com是否被用户使用,重定向到https://www.ttt.com

httpd.conf:

<VirtualHost *:80>
 ServerName www.ttt.com
 ServerAlias ttt.com
 DocumentRoot /home/www/html/ttt/public
 <Directory /home/www/html/ttt/public>
    #Options ExecCGI
    #AddDefaultCharset utf-8
    DirectoryIndex index.php
    AllowOverride All
    Order allow,deny
    Allow from all
 </Directory>
</VirtualHost>

.htaccess:

RewriteEngine On
############################################
## always send 404 on missing files in these folders
#RewriteCond %{REQUEST_URI} !^/(files)/
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

【问题讨论】:

  • 不重复。 Zend Framework 2 已经用过了,你引用的那个不行(已经试过了)
  • @YumYumYum:您能澄清一下starkeen 建议的答案有什么问题吗?
  • paste.fedoraproject.org/362573/23763341 - 当我将 Zend Framework 2 与 .htaccess 一起使用时出现此错误
  • @starkeen:请注意它不是同一个问题。我在使用 Zend 框架的不同环境中,它已经有自己的 .htaccess 要求。从“此处链接”查看正在运行的配置
  • @YumYumYum 接受的答案与重复问题的其他答案相同。你还认为这不是..?

标签: apache .htaccess zend-framework2 centos httpd.conf


【解决方案1】:

在主目录 .htaccess 文件中尝试以下代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]

【讨论】:

  • 不工作。当我输入http://www.example.com 时,它会转到https://www.example.com 当我输入http://example.com 时,它会转到https://example.com 而不是https://www.example.com,当我输入http://www.example.com/contactus 时,它甚至无法通过移动到https://www.example.com/contactus 来找到页面页面在那里。
  • 它对我来说很好,重新启动服务器并从浏览器清空缓存和 cookie,然后再试一次
  • 我刚刚重启了 Apache 50 次,你能检查它为什么不工作吗? http://example.org 没有进入 https://www.example.org
  • 只需将 RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R] 替换为 RewriteRule ^%{HTTP_HOST}%{REQUEST_URI} [L,R]
  • 从 chrome 浏览器中删除历史 cookie 和缓存
【解决方案2】:

给你:https://wiki.apache.org/httpd/RedirectSSL

将请求重定向到 SSL

假设您希望始终发送http://www.example.com/secure/ 通过 SSL(我在这里假设普通虚拟主机和 SSL 虚拟主机都有 相同的内容)。您可以通过链接到正确的页面来做到这一点 从您的 HTML 页面中......但总会有一些用户 会偷偷溜过去的。

使用虚拟主机(使用重定向)

使用 SSL 时,您通常会拥有至少两个虚拟主机: 一个在端口 80 上处理普通请求,一个在端口 443 上 提供 SSL。如果您希望将用户从非安全站点重定向到 SSL 站点,您可以在 非安全虚拟主机:

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName mysite.example.com
   DocumentRoot /usr/local/apache2/htdocs
   Redirect permanent /secure https://mysite.example.com/secure
</VirtualHost>

<VirtualHost _default_:443>
   ServerName mysite.example.com
   DocumentRoot /usr/local/apache2/htdocs
   SSLEngine On
# etc...
</VirtualHost>

重定向所有内容时,您甚至不需要 DocumentRoot:

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName www.example.com
   Redirect permanent / https://secure.example.com/
</VirtualHost>

<VirtualHost _default_:443>
   ServerName secure.example.com
   DocumentRoot /usr/local/apache2/htdocs
   SSLEngine On
# etc...
</VirtualHost>

注意:重定向也可以在 .htaccess 文件中使用或寻址 特定的 URL,如:

例子:

重定向永久/login https://mysite.example.com/login

使用 mod_rewrite

虽然建议使用该解决方案,因为它更简单 更安全,你也可以使用 mod_rewrite 来获得与 此处描述:RewriteHTTPToHTTPS

来自https://httpd.apache.org/docs/trunk/mod/mod_alias.html#redirect

# Redirect to a URL on a different host Redirect "/service" "http://foo2.example.com/service"

# Redirect to a URL on the same host Redirect "/one" "/two"

如果客户端请求http://example.com/service/foo.txt,它将是 被告知访问http://foo2.example.com/service/foo.txt。这 包括带有 GET 参数的请求,例如 http://example.com/service/foo.pl?q=23&a=42,它将被重定向到 http://foo2.example.com/service/foo.pl?q=23&a=42。请注意,POST 将 被丢弃。只有完整的路径段被匹配,所以上面 示例与请求不匹配 http://example.com/servicefoo.txt。对于更复杂的匹配,使用 表达式语法,省略 URL-path 参数,如下所述。 或者,要使用正则表达式进行匹配,请参阅 RedirectMatch 指令。

如果您希望 www.example.com/*example.com/* 都被重定向,您可以创建两个具有不同 ServerName 的 VirtualHost,或者您可以使用 Rewrite 插件。

【讨论】:

  • 我需要将任何网址 theGiallo.comtheGiallo.comtheGiallo.com/contact 放入 theGiall.com..
  • Redirect permanent / https://example.org/ 这一行是否转换了所有页面?比如如果页面是/page1,它会移动到https://example.org/page1吗?
  • 什么?我不明白。
  • 更新答案。
  • 它不工作。当我访问 example.org 时只有页面 www.example.org 但是当我输入 www.example.org/test 时它没有找到该页面并抛出错误页面未找到
【解决方案3】:

您可以从 httpd.conf 执行以下操作:

<VirtualHost *:80>
    ServerName www.ttt.com
    Redirect "/" "https://www.ttt.com/"
</VirtualHost>

<VirtualHost *:443>
    ServerName www.ttt.com
    ...
    ...
</VirtualHost>

或来自 .htaccess 文件:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-31
  • 2016-07-28
  • 2021-07-20
  • 2021-03-31
相关资源
最近更新 更多