[英].htaccess redirecting a specific route with both https and http
I am trying to create a .htaccess
that does the following: 我正在尝试创建执行以下操作的
.htaccess
:
When i visit https :// www.test.com/register/abc
it loads the file https :// www.test.com/register/index.php?path=abc
(but still retains the url https :// www.test.com/register/abc
on the browser) 当我访问
https :// www.test.com/register/abc
它将加载文件https :// www.test.com/register/index.php?path=abc
(但仍保留网址https :// www.test.com/register/abc
在浏览器上)
So far, this is my .htaccess
到目前为止,这是我的
.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}
It works well if i visit the url already on https. 如果我已经在https上访问了URL,它将很好地工作。
The browser shows www.test.com/register/abc
. 浏览器显示
www.test.com/register/abc
。
However, if i visit the url on http, the url on the browser shows as this www.test.com/register/index.php?path=abc
但是,如果我访问http上的URL,则浏览器上的URL会显示为此
www.test.com/register/index.php?path=abc
Technically, it still works but the url is really ugly. 从技术上讲,它仍然有效,但是URL确实很丑陋。 Is there anyway to fix this?
有没有什么办法解决这一问题?
This should do what your looking for: 这应该可以满足您的需求:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^register/([a-zA-Z0-9-_]+) /index.php?path=$1 [QSA,NC,L]
It will internally rewrite: 它将在内部重写:
www.example.com/register/acb
to www.example.com/index.php?path=abc
www.example.com/register/acb
到www.example.com/index.php?path=abc
Update: To force to https you couls use something like: 更新:要强制使用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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.