空查询附加到主 URL .htaccess 问题
Posted
技术标签:
【中文标题】空查询附加到主 URL .htaccess 问题【英文标题】:Empty Query getting attached to main URL .htaccess issue 【发布时间】:2015-07-24 05:50:22 【问题描述】:我正在使用 .htaccess 来清理我的 URL。单击 www.example.com/el-nino-effect 等不同链接时,它通常工作正常。但是,当我明确访问 www.example.com 时,它会将我带到 www.example.com/index?iden= 而不仅仅是 www.example.com。尽管它们是同一个页面,但这个主 URL 不知何故搞砸了。你能帮忙吗?
第 4 段是 .htaccess 中更清晰的 URL 代码的位置,但我仍然发布了整个文件。同样有趣的是,这个问题并没有发生在 Ubuntu 上的 Chrome 浏览器中,而是发生在 chromebook 上的 Chrome 浏览器上。
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension snippet
RewriteCond %THE_REQUEST ^[A-Z]3,\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_FILENAME.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
# For cleaner URLs making ?q=el-nino to /el-nino
RewriteRule ^([^/\.]+)?$ index.php?iden=$1 [L]
RewriteRule ^([^/\.]+)/?$ index.php?iden=$1 [L]
# RewriteRule ^downloads/([^/\.]+)/?$ downloads.php?id=$1 [L]
# For rewriting to HTTPS
RewriteCond %HTTPS off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%HTTP_HOST%REQUEST_URI [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %HTTP_HOST !^www\.
RewriteRule .* https://www.%HTTP_HOST%REQUEST_URI [L,R=301]
【问题讨论】:
【参考方案1】:实际上,您的规则看起来不错,但顺序不正确。通常在内部重写之前保留重定向规则,如下所示:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# For rewriting to HTTPS
RewriteCond %HTTPS off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule ^ https://%HTTP_HOST%REQUEST_URI [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %HTTP_HOST !^www\. [NC]
RewriteRule ^ https://www.%HTTP_HOST%REQUEST_URI [L,R=301]
## hide .php extension snippet
RewriteCond %THE_REQUEST ^[A-Z]3,\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_FILENAME.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# For cleaner URLs making ?q=el-nino to /el-nino
RewriteRule ^([^.]+)/?$ index.php?iden=$1 [L,QSA]
# RewriteRule ^downloads/([^/\.]+)/?$ downloads.php?id=$1 [L,QSA]
请务必在清除浏览器缓存后对此进行测试。
【讨论】:
以上是关于空查询附加到主 URL .htaccess 问题的主要内容,如果未能解决你的问题,请参考以下文章
将查询字符串附加到 htaccess PDF 重定向 URL