HTTPS 重定向,从 URL 中排除脚本参数

HTTPS 重定向,从 URL 中排除脚本参数

我的目标:

1. http://example.com/page/index.php -> https://example.com/page/
2. http://example.com/page/ -> https://example.com/page/
3. http://www.example.com/page/ -> https://example.com/page/
4. Any nonexistent file or directory requests -> index.php?&q=$1 for further processing.

遵循规则可以正常工作,但在浏览器 URL 中重定向而不是干净的 URL

https://example.com/page/

我懂了

https://example.com/index.php?&q=page/

如何隐藏 index.php?&q=page 并显示https://example.com/page/在 URL 中?在我添加 HTTPS 重定向之前,它对 HTTP 运行良好。我尝试了 2 个解决方案,结果相同。

RewriteEngine on
RewriteBase /

#1 solution:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ [NC]
RewriteRule ^index\.php$ https://%{HTTP_HOST}/ [L,NE,R=301]

RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,NE,R=301]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?&q=$1 [QSA]


#2 solution:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ [NC]
RewriteRule ^index\.php$ https://%{HTTP_HOST}/ [L,NE,R=301]

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,NE,R=301]

RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?&q=$1 [QSA]

相关内容