【问题标题】:Can not access query string in .htaccess?无法访问 .htaccess 中的查询字符串?
【发布时间】:2022-01-01 02:28:58
【问题描述】:

由于某种原因,我无法访问仅用于 .htaccess 中的内部重写的查询值?

Options +FollowSymLinks

RewriteEngine ON
RewriteBase /

##external redirect
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ /test/user-profile/%1? [R=301,L]

#internal rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)$ /test/user-profile.php?id=$3 [L]

这是整个 htaccess 文件。它完美地重定向了url,只是不能自己获取id值?谢谢

【问题讨论】:

  • 您请求什么网址?预期的 URL 目标是什么?
  • 请求 www.example.com/test/user-profile?id=1 然后我希望 url 以 www.example.com/test/user-profile/1 的形式显示给用户,然后仍然可以通过 PHP 提取 id 值

标签: php apache .htaccess config


【解决方案1】:

这是因为您在重写中使用了id=,并且您将第二个 (/test/user-profile.php?id=N) 重定向到第一个 (/test/user-profile/N)

改第一个:

##external redirect
RewriteCond %{REQUEST_URI} !^/test/user-profile\.php
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^ /test/user-profile/%1? [R=301,L]

【讨论】:

  • 所以这确实抓取了查询变量,但现在它不会将 URL 重定向到 /test/user-profile/3
  • 那不改变正确的重写,只用user-profile.php
  • 所以我尝试将www.example.com/test/user-profile?id=3 重写为www.example.com/test/user-profile/3,同时还可以使用GET['id']。你能帮忙吗?谢谢你的时间
  • RewriteRule ^(.*)$ /test/user-profile.php?id=$1 [L] 一直说查询是id=test/user-profile/7
  • @BlakePrice 你从哪里得到这条规则的?问题中没有提到,还是上面的答案?
猜你喜欢
  • 2021-01-16
  • 2011-05-16
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
  • 2018-03-05
  • 2012-02-16
  • 1970-01-01
  • 2016-10-25
相关资源
最近更新 更多