加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 空查询附加到主URL .htaccess问题

发布时间:2020-12-13 15:55:52 所属栏目:PHP教程 来源:网络整理
导读:我正在使用.htaccess来清理我的网址.它通常在点击www.example.com/el-nino-effect等不同链接时工作正常.但是,当我明确访问www.example.com时,它会将我带到www.example.com/index?iden=而不仅仅是www.example.com.虽然它们是同一页面,但这个主要URL在某种程度
我正在使用.htaccess来清理我的网址.它通常在点击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]

解决方法

实际上你的规则看起来不错,但它们的顺序并不正确.通常在内部重写之前保留重定向规则,如下所示:

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,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]

清除浏览器缓存后,请务必进行测试.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读