简体   繁体   中英

.htaccess redirecting a specific route with both https and http

I am trying to create a .htaccess that does the following:

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)

So far, this is my .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.
The browser shows 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

Technically, it still works but the url is really ugly. 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

Update: To force to https you couls use something like:

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]

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