【问题标题】:Rewrite URL with .htaccess and HTTPS使用 .htaccess 和 HTTPS 重写 URL
【发布时间】:2015-05-22 15:32:18
【问题描述】:

我有一个网站,我必须使用 .htaccess 文件将所有请求重定向到处理重定向的 index.php。 我想重写 URL,同时使用 HTTPS。如果没有 HTTPS,它可以正常工作。

来自没有 HTTPS 的工作 .htaccess 的代码。浏览器得到这个输入:alert/create

RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]

这工作正常,但没有 HTTPS。浏览器 URL 变为 http://localhost/mypage/alert/create,,这就是我想要的。

我找到了一个允许我使用 HTTPS 的解决方案:

RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ https://%{SERVER_NAME}/mypage/index.php?controller=$1&action=$2&id=$3 [NC,L]

页面导航就像一个魅力,但浏览器显示的 URL 是这样的:

https://localhost/mypage/index.php?controller=alert&action=create&id=

请求的处理方式如下:

public function __construct($urlvalues) {
    $this->urlvalues = $urlvalues;
    if ($this->urlvalues['controller'] == "") {
        $this->controller = "home";
    } else {
        $this->controller = $this->urlvalues['controller'];
    }
    if ($this->urlvalues['action'] == "") {
        $this->action = "index";
    } else {
        $this->action = $this->urlvalues['action'];
    }
}

我需要一些提示。我一直在寻找整个互联网而没有解决我的问题...... 如果我可以使用 .htaccess,但以另一种方式实现 HTTPS,那也将是完美的。 用 PHP 编写的服务器代码,在 apache2 上运行。

【问题讨论】:

    标签: php .htaccess https url-rewriting


    【解决方案1】:

    我会分两步完成。你已经拥有的第一个。还有这个:

    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
    

    【讨论】:

    • 网站正常运行,但 URL 格式如下:localhost/mypage/index.php?controller=&action=&id=
    • 您的意思是 HTTPS 重定向可以工作,但不能使用正确的 url?
    • 你有没有把这个 RewriteRule 放在 RewriteRule 之前(用你的控制器和动作)?
    【解决方案2】:

    解决了!解决方案:将其添加到 .htaccess,特别是:

    RewriteEngine on
    
    #force https 
    RewriteEngine On 
    RewriteCond %{SERVER_PORT} 80 
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    
    RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]
    

    【讨论】:

      猜你喜欢
      • 2021-01-30
      • 2021-12-28
      • 1970-01-01
      • 2014-07-18
      • 2012-07-30
      • 2010-11-04
      • 1970-01-01
      • 2016-12-05
      • 2012-04-29
      相关资源
      最近更新 更多