[英].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/acb
到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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.