Rewriting dynamic URLs

前端 未结 3 1136
醉酒成梦
醉酒成梦 2020-12-11 04:49

I\' trying to rewrite 1 dynamic URL to another dynamic URL as follows;

/category?category=News to
/categorysearch/result/?q=news

I was wond

3条回答
  •  时光取名叫无心
    2020-12-11 05:23

    Depending on what you're trying to do and how specific things need to be, there are two options.

    The first is Rob W's solution, which should redirect the user from /category?category=News to /categorysearch/result/?q=News

    If, as it appears, your URLs are case-sensitive, you might want to look at a PHP script like the following:

    $matches = array();
    if( preg_match( "/^/category\?category=(.+)/", $_SERVER['REQUEST_URI'], $matches ) ) {
        header( 'Location: http://' . $_SERVER['HTTP_HOST'] . '/categorysearch/result/?q= . strtolower($matches[1]) );
    }
    else {
        //ERROR HANDLING
    }
    

    Once that's settled, you can use a rule like the following to redirect using a fully lowercase parameter.

    RewriteRule ^category\?category=(.+)$ redirect.php [L]
    

    Put together, this solution would redirect /category?category=News to /categorysearch/result/?q=news

提交回复
热议问题