简体   繁体   English

使用.htaccess和HTTPS重写URL

[英]Rewrite URL with .htaccess and HTTPS

I have a web site where I have to use a .htaccess file to redirect all requests to index.php, where redirecting is handled. 我有一个网站,我必须在其中使用.htaccess文件将所有请求重定向到index.php,并在其中处理重定向。 I want to rewrite the URL, and at the same time use HTTPS. 我想重写URL,同时使用HTTPS。 Without HTTPS it works fine. 没有HTTPS,它可以正常工作。

Code from working .htaccess without HTTPS. 在没有HTTPS的情况下从.htaccess运行的代码。 Browser gets this input: alert/create 浏览器得到以下输入: alert/create

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

This works fine, but without HTTPS. 这可以正常工作,但是没有HTTPS。 Browser URL becomes http://localhost/mypage/alert/create, and that's what I want. 浏览器URL变为http://localhost/mypage/alert/create,这就是我想要的。

I found a solution that allows me to use HTTPS: 我找到了一种可以使用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]

Page navigation works like a charm, but browser displays the URL like this: 页面导航的工作原理就像一个超级按钮,但是浏览器却显示如下网址:

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

Requests are handled like this: 请求的处理方式如下:

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'];
    }
}

I need some hints. 我需要一些提示。 I've been looking all over internet without solving my problem... If I can use .htaccess, but implement HTTPS another way, that'll be perfect too. 我一直在互联网上寻找解决问题的方法...如果我可以使用.htaccess,但是以另一种方式实现HTTPS,那也将是完美的。 Server code written in PHP, running on apache2. 用PHP编写的服务器代码,在apache2上运行。

I would have done this in two steps. 我将分两步完成此操作。 The first that you already have. 您已经拥有的第一个。 And this one : 还有这个 :

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

Solved! 解决了! Solution: Added this to .htaccess, in that order spesifically: 解决方案:将其添加到.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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM