简体   繁体   中英

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. I want to rewrite the URL, and at the same time use HTTPS. Without HTTPS it works fine.

Code from working .htaccess without HTTPS. Browser gets this input: 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. Browser URL becomes http://localhost/mypage/alert/create, and that's what I want.

I found a solution that allows me to use 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. Server code written in PHP, running on 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:

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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