【问题标题】:How to redirect HTTP to HTTPS using XAMPP如何使用 XAMPP 将 HTTP 重定向到 HTTPS
【发布时间】:2017-07-01 17:51:36
【问题描述】:

我正在尝试使用 XAMPP 将 HTTP 重定向到 HTTPS。
我已经在推荐这个网站的多个网站上看到了所有问题和多个答案:XAMPP: SSL Encrypt the Transmission of Passwords with HTTPS | Rob's Notebook

这就是我所做的:


我取消了该行的注释:

LoadModule rewrite_module modules/mod_rewrite.so

C:/xampp/apache/config/httpd.conf 中,删除前面的#

然后我在C:/xampp/apache/config/extra/httpd-xampp.conf里面输入了这个:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Redirect /xampp folder to https
    RewriteCond %{HTTPS} !=on
    RewriteCond %{REQUEST_URI} xampp
    RewriteRule ^(.*) https://%{SERVER_NAME}$1 [R,L]

    # Redirect /phpMyAdmin folder to https
    RewriteCond %{HTTPS} !=on
    RewriteCond %{REQUEST_URI} phpmyadmin
    RewriteRule ^(.*) https://%{SERVER_NAME}$1 [R,L]

    # Redirect /security folder to https
    RewriteCond %{HTTPS} !=on
    RewriteCond %{REQUEST_URI} security
    RewriteRule ^(.*) https://%{SERVER_NAME}$1 [R,L]

    # Redirect /webalizer folder to https
    RewriteCond %{HTTPS} !=on
    RewriteCond %{REQUEST_URI} webalizer
    RewriteRule ^(.*) https://%{SERVER_NAME}$1 [R,L]
</IfModule>

我三重检查了我所做的一切是否正确,我保存了所有内容,并重新启动了 XAMPP,但我仍然无法重定向到 HTTPS。请帮忙!

【问题讨论】:

  • if( !isset( $_SERVER['HTTPS'] ) ) 那么你可以简单地使用你的header('Location: https://...'); exit();
  • user2078421 这正是我尝试过的,但它对我不起作用。
  • 您应该准确描述您想要实现的重定向。您的示例代码不仅仅是将 HTTP 重定向到 HTTPS。
  • w3dk 那它还在做什么

标签: php apache .htaccess mod-rewrite xampp


【解决方案1】:

我自己找到了答案;我有虚拟主机设置,我必须放置

<IfModule mod_rewrite.c>
RewriteEngine On

# Redirect /xampp folder to https
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} xampp
RewriteRule ^(.*) https://%{SERVER_NAME}$1 [R,L]

# Redirect /phpMyAdmin folder to https
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} phpmyadmin 
RewriteRule ^(.*) https://%{SERVER_NAME}$1 [R,L]

# Redirect /security folder to https
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} security
RewriteRule ^(.*) https://%{SERVER_NAME}$1 [R,L]

# Redirect /webalizer folder to https 
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} webalizer
RewriteRule ^(.*) https://%{SERVER_NAME}$1 [R,L]
</IfModule>

C:/xampp/apache/config/extra/httpd-vhosts.conf 中我要重定向的目录内

这在 rob notebook 的最后几行中进行了解释:
此重定向要记住的一点是,如果您有虚拟主机,则需要放置重定向代码(使用 RewriteCond 和 RewriteRule ) 在您的虚拟主机声明中,否则重定向将不起作用。

【讨论】:

  • 您的 4 条规则可以大大简化为一条规则。但是,您现有的指令重定向不仅仅是“/xampp 文件夹”等。如果您使用的是虚拟主机,那么您应该能够避免检查 HTTPS。 (?)
猜你喜欢
  • 2018-05-23
  • 1970-01-01
  • 2015-04-23
  • 2010-09-05
  • 1970-01-01
  • 2016-08-31
  • 2014-06-11
  • 2014-04-18
相关资源
最近更新 更多