douzi0609 2016-07-02 20:04
浏览 44
已采纳

有关动态子目录的查询字符串网址的.htaccess重写规则需要帮助

I am working on a project and I am stuck with some .htaccess rewrite rule which is not working & I tried many things but no luck, so please help me.

Here is what I am doing:

when I open: domain.com/us/search.html?keyword=xyz&location=alabama

it should have to read the file search.php from root directory & work like this: search.php?directory=us&keyword=xyz&location=alabama

it's properly getting subdirectory, but not getting keywords and location data.

Here is my .htaccess code:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteRule ^(.*)/(.*)-ad(.*).html ad.php?countrycode=$1&title=$2&id=$3 [NC]
RewriteRule ^(.*)/$ country.php?countrycode=$1 [NC]
RewriteRule ^(.*)/(.*)-author\.html author.php?countrycode=$1&q=$2 [NC]
RewriteRule ^(.*)/(.*)-author/currentpage=(.*) author.php?countrycode=$1&q=$2&currentpage=$3 [NC]
RewriteRule ^(.*)/search\.html?keyword=(.*)&location=(.*) search.php?countrycode=$1&keyword=$2&location=$3 [NC,L]
RewriteRule ^(.*)/search\.html$  search.php?countrycode=$1 [NC,L]
RewriteRule ^(.*)/latest\.html$  latest.php?countrycode=$1 [NC,L]
RewriteRule ^(.*)/submit\.html$  submit.php [NC,L]
RewriteRule ^error\.html$  error.php [NC,L]
ErrorDocument 400 /error.html
ErrorDocument 401 /error.html
ErrorDocument 403 /error.html
ErrorDocument 404 /error.html
ErrorDocument 500 /error.html
  • 写回答

2条回答 默认 最新

  • dsk88199 2016-07-02 20:28
    关注

    The relevant rule is

    RewriteRule ^(.*)/search\.html$ search.php?countrycode=$1 [NC,L]
    

    Usually RewriteRule keeps an existing query string, unless you add one yourself

    Modifying the Query String

    By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.

    To keep the previous query string, you must add the QSA|qsappend flag

    RewriteRule ^(.*)/search\.html$ search.php?countrycode=$1 [NC,L,QSA]
    

    This rewrites search.html unconditionally.


    If you want to rewrite it only when the request contains the query string keyword=xyz&location=alabama, you must prefix the rule with a RewriteCond with %{QUERY_STRING}

    RewriteCond %{QUERY_STRING} keyword=.+?&location=.
    RewriteRule ^(.*)/search\.html$ search.php?countrycode=$1 [NC,L,QSA]
    

    P.S. At the beginning, you write search.php?directory=us&..., but in the rules you have search.php?countrycode=.... Adjust the rules accordingly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题