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条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算