【问题标题】:How to use RewriteRule in order to do an invisibly search query?如何使用 RewriteRule 进行不可见的搜索查询?
【发布时间】:2013-10-20 21:03:43
【问题描述】:

首先我想说我一直在研究,但我仍然没有找到解决这个问题的方法。
我想做一个隐形重定向。使用此 URL http://www.localhost.com/@user_name/search/?q=string 进行搜索而不重定向到另一个页面,因为我应该检查 MySQL 数据库中的用户 @user_name 以检索其用户配置文件。

PHP 脚本搜索.php

<?php
    echo 'USER:'.$_GET['user'].'<br/>';
    echo 'QUERY q:'.$_GET['q'].'<br/>';
?>

.htaccess

#Begin invisibly redirect 
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteRule \.(css|jpe?g|gif|png)$ - [L]

RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]

#This works with this (www.localhost.com/@user_name/search/X55)
#RewriteRule ^@([_A-Za-z0-9]+)/search/X(.*)$ /profile/search/search.php?user=$1&q=$2 [L]

#But ,for search, this doesn't work with this (www.localhost.com/@user_name/search/?q=string)
RewriteRule ^@([_A-Za-z0-9]+)/search/?q=(.*)$ /profile/search/search.php?user=$1&q=$2 [L]
#END invisibly redirect

结果

1

有: #RewriteRule ^@([_A-Za-z0-9]+)/search/X(.*)$ /profile/search/search.php?user=$1&q=$2 [L]
和
www.localhost.com/@user_name/search/X55
它返回
用户:用户名
查询 q:55/

2

有: #RewriteRule ^@([_A-Za-z0-9]+)/search/?q=(.*)$ /profile/search/search.php?user=$1&q=$2 [L]
和
www.localhost.com/@user_name/search/?q=string
它给出了服务器错误。

请帮忙,谢谢。

【问题讨论】:

    标签: php mysql .htaccess mod-rewrite


    【解决方案1】:

    请记住,RewriteRule 与 QUERY_STRING 不匹配。它仅匹配 REQUEST_URI。您只需要在您的 URL 中添加 QSA(现有查询字符串附加)标志。你的规则应该是:

    RewriteRule ^@(\w+)/search/?$ /profile/search/search.php?user=$1 [L,QSA]
    

    参考:Apache mod_rewrite Introduction

    【讨论】:

    • 非常感谢,它运行良好。我不知道 QSA,再次非常感谢。
    【解决方案2】:

    您必须使用QSA 标志来传递查询字符串:

    RewriteRule ^@([_A-Za-z0-9]+)/search/?$ /profile/search/search.php?user=$1 [QSA,L]
    

    ..search/?$ 上的问号将斜杠字符 / 作为可选:

    localhost.com/@user_name/search/?q=string 
    localhost.com/@user_name/search?q=string 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      相关资源
      最近更新 更多