我有一个网站,在那里我必须使用.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上。
我会分两步完成。你已经拥有的第一个。这个:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
已解决!解决方案:将此添加到.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]