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
(但仍保留浏览器上的urlhttps://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)