【问题标题】:how to remove multiple query string variable from url using htaccess or other technique如何使用 htaccess 或其他技术从 url 中删除多个查询字符串变量
【发布时间】:2015-02-11 08:49:14
【问题描述】:

我的网站有动态页面如

http://www.example.com/detailpage.php?sn=1%20&&%20database=news
http://www.example.com/detailpage.php?sn=1%20&&%20database=article
http://www.example.com/detailpage.php?sn=1%20&&%20database=interview

还有很多其他

我想从 url 中删除或隐藏查询字符串变量并显示为

http://www.krishisansar.com/detailpage/news/1
http://www.krishisansar.com/detailpage/article/1

是否可以将查询字符串更改为斜线?如果没有,如何删除 & 并制作为

/news=1 or /news=2, 
/article=1 or /article=2 .

通过这个,我可以从 w3c 进行验证。

【问题讨论】:

    标签: php database .htaccess query-string


    【解决方案1】:

    您可以在 DOCUMENT_ROOT/.htaccess 文件中使用此代码:

    Options -MultiViews
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{THE_REQUEST} /detailpage\.php\?sn=([^\s&]+)&database=([^\s&]+) [NC]
    RewriteRule ^ detailpage/%2/%1? [R=302,L,NE]
    
    RewriteRule ^detailpage/(\w+)/(\d+)/?$ detailpage.php?sn=$2&database=$1 [L,QSA,NC]
    

    【讨论】:

    • ok 检查更新的规则。确保这是在根 .htaccess 中(高于新闻级别)
    • 太棒了。你真的节省了我的时间。这对我来说非常有效。根据您的代码,url 显示为我的愿望,但打印 mysql 错误。我想最后一行 RewriteRule ^detailpage/(\w+)/(\d+)/?$ detailpage.php?sn=$2database=$1 [L,QSA,NC] is missing & between sn=$2database=$1。当我添加 & 错误已解决但 css 规则受到干扰。我很困惑,添加 & 是错误的还是我的内部结构错误?
    • 啊,没错,我在最后一条规则中缺少&。我现在已经修好了。对于 css/js 问题,将其添加到页面 HTML 的 <head> 部分:<base href="/" /> 以便从该 URL 而不是当前页面的 URL 解析每个相对 URL。
    • 不错的答案。原始链接一团糟。
    • 是的! 有效。 @anubhava 感谢您宝贵的时间和持续的支持
    猜你喜欢
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    相关资源
    最近更新 更多