Search code examples
regexapache.htaccessmod-rewriteurl-rewriting

How to (.htaccess) rewrite the url of a specific search query?


I wish to (.htaccess) rewrite the url of a search query when it has the "custom_title" parameter. Anyone can help me? I'd like to rewrite this url:

http://www.mysite.com/search.php?search_query=myquery&custom_title=mytitle

to this:

http://www.mysite.com/results/myquery/mytitle/


Solution

  • Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^results/([^/]+)/([^/]+)/?$ /search.php?search_query=$1&custom_title=$2 [L,NC,QSA]