簡體   English   中英

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

[英].htaccess redirecting a specific route with both https and http

我正在嘗試創建執行以下操作的.htaccess

當我訪問https :// www.test.com/register/abc它將加載文件https :// www.test.com/register/index.php?path=abc (但仍保留網址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/acbwww.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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM