.htaccess重定向自动添加URL参数问题求助
问题与解决方案
问题描述
将域名lgbtmilitary.org绑定到主域名delisoft.ca后,访问https://lgbtmilitary.org/时,会被重定向到https://delisoft.ca/expertise/marketing-lgbtq/?lang=fr&page=accueil&url=1,而非预期的https://delisoft.ca/expertise/marketing-lgbtq/。
原.htaccess配置如下:
RewriteOptions inherit RewriteEngine On RewriteBase / RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L] RewriteCond %{HTTP_USER_AGENT} libwww-perl.* RewriteRule .* ? [F,L] ErrorDocument 404 https://delisoft.ca/plan-du-site/ # Compress HTML, CSS, JavaScript, Text, XML and fonts AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/vnd.ms-fontobject AddOutputFilterByType DEFLATE application/x-font AddOutputFilterByType DEFLATE application/x-font-opentype AddOutputFilterByType DEFLATE application/x-font-otf AddOutputFilterByType DEFLATE application/x-font-truetype AddOutputFilterByType DEFLATE application/x-font-ttf AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE font/opentype AddOutputFilterByType DEFLATE font/otf AddOutputFilterByType DEFLATE font/ttf AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilterByType DEFLATE image/x-icon AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/xml # Remove browser bugs (only needed for really old browsers) BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html Header append Vary User-Agent </IfModule> RewriteRule ^$ index.php?lang=fr&page=accueil&url=1 RewriteRule ^expertise/strategie-web-b2b/$ index.php?lang=fr&page=expb2b RewriteRule ^expertise/strategie-web-b2c/$ index.php?lang=fr&page=expb2c RewriteRule ^expertise/construction/$ index.php?lang=fr&page=expconstruction RewriteRule ^expertise/professionnels-de-la-sante/$ index.php?lang=fr&page=expsante RewriteRule ^expertise/immobilier-site-web/$ index.php?lang=fr&page=expimmobilier RewriteRule ^expertise/site-web-esthetique/$ index.php?lang=fr&page=expbeauty RewriteRule ^expertise/marketing-lgbtq/$ index.php?lang=fr&page=explgbtq&service=marketing RewriteRule ^expertise/([a-z-A-Z0-9]+)-lgbtq/$ index.php?lang=fr&page=explgbtq&service=$1 # php -- BEGIN cPanel-generated handler, do not edit # Set the “ea-php72” package as the default “PHP” programming language. AddHandler application/x-httpd-ea-php72 .php .php7 .phtml # php -- END cPanel-generated handler, do not edit RewriteCond %{HTTP_HOST} ^lgbtmilitary.org$ [OR] RewriteCond %{HTTP_HOST} ^www.lgbtmilitary.org$ RewriteRule ^(.*)$ "https://delisoft.ca/expertise/marketing-lgbtq/" [R=301,L]
原因分析
当前配置中,根目录内部重写规则(RewriteRule ^$ index.php?lang=fr&page=accueil&url=1)的执行顺序早于域名重定向规则。访问lgbtmilitary.org根目录时,会先触发内部重写,将请求转换为带参数的index.php路径,之后执行域名重定向时,默认会保留这些查询参数,最终导致重定向URL带上多余参数。
解决方案
调整规则顺序,将域名重定向规则移至根目录内部重写规则之前,确保域名重定向优先执行;同时在重定向规则中添加QSD标志,明确丢弃原查询参数(即使存在也会被清除)。
修改后的完整.htaccess配置如下:
RewriteOptions inherit RewriteEngine On RewriteBase / RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L] RewriteCond %{HTTP_USER_AGENT} libwww-perl.* RewriteRule .* ? [F,L] ErrorDocument 404 https://delisoft.ca/plan-du-site/ # Compress HTML, CSS, JavaScript, Text, XML and fonts AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/vnd.ms-fontobject AddOutputFilterByType DEFLATE application/x-font AddOutputFilterByType DEFLATE application/x-font-opentype AddOutputFilterByType DEFLATE application/x-font-otf AddOutputFilterByType DEFLATE application/x-font-truetype AddOutputFilterByType DEFLATE application/x-font-ttf AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE font/opentype AddOutputFilterByType DEFLATE font/otf AddOutputFilterByType DEFLATE font/ttf AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilterByType DEFLATE image/x-icon AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/xml # Remove browser bugs (only needed for really old browsers) BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html Header append Vary User-Agent </IfModule> # 域名重定向规则移至内部重写规则之前,并添加QSD标志 RewriteCond %{HTTP_HOST} ^lgbtmilitary.org$ [OR] RewriteCond %{HTTP_HOST} ^www.lgbtmilitary.org$ RewriteRule ^(.*)$ https://delisoft.ca/expertise/marketing-lgbtq/ [R=301,L,QSD] RewriteRule ^$ index.php?lang=fr&page=accueil&url=1 RewriteRule ^expertise/strategie-web-b2b/$ index.php?lang=fr&page=expb2b RewriteRule ^expertise/strategie-web-b2c/$ index.php?lang=fr&page=expb2c RewriteRule ^expertise/construction/$ index.php?lang=fr&page=expconstruction RewriteRule ^expertise/professionnels-de-la-sante/$ index.php?lang=fr&page=expsante RewriteRule ^expertise/immobilier-site-web/$ index.php?lang=fr&page=expimmobilier RewriteRule ^expertise/site-web-esthetique/$ index.php?lang=fr&page=expbeauty RewriteRule ^expertise/marketing-lgbtq/$ index.php?lang=fr&page=explgbtq&service=marketing RewriteRule ^expertise/([a-z-A-Z0-9]+)-lgbtq/$ index.php?lang=fr&page=explgbtq&service=$1 # php -- BEGIN cPanel-generated handler, do not edit # Set the “ea-php72” package as the default “PHP” programming language. AddHandler application/x-httpd-ea-php72 .php .php7 .phtml # php -- END cPanel-generated handler, do not edit
内容的提问来源于stack exchange,提问作者Pierre-Luc Lamoureux
相关产品推荐
相关产品推荐

