【问题标题】:.htaccess force to HTTPS in a folder.htaccess 强制文件夹中的 HTTPS
【发布时间】:2018-08-07 00:26:14
【问题描述】:

我正在尝试重定向论坛索引以对所有内容都使用 HTTPS,但 .htaccess 对我来说很新,我正在学习,所以任何人都可以帮忙,我将不胜感激 :)

所以我将论坛放在一个名为 SMF 的子目录中。目前一切都在 www.website.net/smf/index.php

但我想将所有内容重定向到 HTTPS,同时保留 /smf/index.php 结构。

这是我在主 html_public 中的当前 .htaccess:

# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/subdirectory/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /subdirectory/$1
# Change example.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subdirectory/index.php [L] 

当我改变任何东西时,一切都会变得很糟糕。论坛中的所有内容都在子目录文件夹中,包括 index.php 等。 (请注意我将网站 url 更改为 example.com 和子目录,仅在复制粘贴代码中更改为子目录)

我正在努力让网站显示 https,但是每当我尝试某些东西时它都不起作用并给我https://www.website.com//smf/index.php 并且我无法删除 // :(

我是否可以清理代码并使其更简单易懂?

谢谢!

【问题讨论】:

    标签: php .htaccess https


    【解决方案1】:

    我已经为 .htaccess 文件苦苦挣扎了很多个晚上……但事实是,这个文件实际上与 php 无关,但实际上与 Apache 相关联。这是一个独立于 PHP 的程序(它本身就是一种编程语言和编译器)。并且不要过多地切线,但这意味着您可以在没有浏览器的终端中运行 php。任何 hoot,因为 Apache 不是用 PHP 编程的,并且仅设计用于处理 HTTP(s) 服务器请求,因此使其工作的一些代码可能取决于操作系统。这是在本地运行并尝试部署到我的远程服务器时遇到很多困难的原因。以上都是我个人的经验,如果有知道的朋友请多多交流。

    这是我现在用来发送到 https 的。

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

    但我以前也使用过这些变体。

    #Turn on https ( Cent OS ) RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # Turn on https ( Mac OS ) RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    为了可移植性,我建议直接在 php 中自己制作此功能。

    您可以使用以下类似的方式检测 https,这将设置一个名为“HTTPS”的全局常量布尔值

    \define('HTTPS', ($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? false) === 'https' || (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');

    这是 php 7 代码

    如果它是错误的,那么你只需重定向你认为合适的方式。

    【讨论】:

      【解决方案2】:
      Options +FollowSymLinks -MultiViews
      # Turn mod_rewrite on
      RewriteEngine On
      RewriteBase /
      
      ## hide .php extension
      # To externally redirect /dir/foo.php to /dir/foo
      RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
      RewriteRule ^ %1 [R=307,L,NC]
      
      ## To internally redirect /dir/foo to /dir/foo.php
      RewriteCond %{REQUEST_FILENAME}.php -f
      RewriteRule ^ %{REQUEST_FILENAME}.php [L]
      
      RewriteCond %{REQUEST_FILENAME}.html -f
      RewriteRule ^ %{REQUEST_FILENAME}.html [L]
      
      RewriteCond %{SERVER_PORT} 80 
      RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
      
      #ErrorDocument 404 /404.php
      #ErrorDocument 403 /403.php
      
      Options -Indexes
      

      【讨论】:

        猜你喜欢
        • 2011-06-11
        • 2012-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-23
        • 1970-01-01
        • 2012-02-02
        相关资源
        最近更新 更多