Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php .htaccess使用https和http重定向特定路由_Php_Apache_.htaccess_Redirect_Mod Rewrite - Fatal编程技术网

Php .htaccess使用https和http重定向特定路由

Php .htaccess使用https和http重定向特定路由,php,apache,.htaccess,redirect,mod-rewrite,Php,Apache,.htaccess,Redirect,Mod Rewrite,我正在尝试创建一个执行以下操作的.htaccess: 当我访问https://www.test.com/register/abc时,它加载文件https://www.test.com/register/index.php?path=abc(但仍保留浏览器上的urlhttps://www.test.com/register/abc) 到目前为止,这是我的.htaccess Options +FollowSymLinks -MultiViews -Indexes RewriteEngine On R

我正在尝试创建一个执行以下操作的
.htaccess

当我访问
https://www.test.com/register/abc
时,它加载文件
https://www.test.com/register/index.php?path=abc
(但仍保留浏览器上的url
https://www.test.com/register/abc

到目前为止,这是我的
.htaccess

Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?path=$1 [NC,L,QSA]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
如果我访问https上已有的url,效果会很好
浏览器显示
www.test.com/register/abc

但是,如果我访问http上的url,浏览器上的url显示如下
www.test.com/register/index.php?path=abc


从技术上讲,它仍然有效,但url真的很难看。有没有办法解决这个问题?

这应该可以满足您的要求:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^register/([a-zA-Z0-9-_]+) /index.php?path=$1 [QSA,NC,L]
它将在内部重写:

www.example.com/register/acb
to
www.example.com/index.php?path=abc

更新:要强制使用https,您可以使用以下内容:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{HTTPS} !=on 
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteRule ^register/([a-zA-Z0-9-_]+) /index.php?path=$1 [QSA,NC,L]

谢谢你的回答。只是一个简单的问题,如果我还想将所有http重定向到https呢?上面的答案很好,但不会将http更改为https(我不想要任何非http URL)