Redirecting and rewriting with mod_rewrite

By asking this question: Clear search query URLs? I tried something with mod_rewrite:

RewriteCond %{QUERY_STRING} ^s=([az]+)$ [NC] RewriteRule / /s/$1? [NC,R=301,L] RewriteRule ^s/([az]+)$ /?s=$1 [NC,L] 

What is the purpose?

This seems like a double job, but if you look good, you see what I'm trying to accomplish:

  • Redirect any search sequence to a cleaner equivalent (be it a form or someone entering it directly)
  • Rewrite (do not redirect) this URL back to the dynamic request sequence so that I can get it using PHP via $ _GET

If I think about it, it should be possible. Therefore, I would like to seek the help of an experienced fashion copyist to help me with this.

Number 2 works, but what is it.

0
redirect query-string apache .htaccess mod-rewrite
source share
1 answer

This should work, I tested it with different names and directories, but it should be fine in your case.

NB: for the matched group from RewriteCond you must use %1 not $1 .

 RewriteCond %{QUERY_STRING} ^s=([az]+)$ [NC] RewriteRule ^$ /s/%1? [NC,R,L] RewriteRule ^s/([az]+)$ /?s=$1 [NC,L] 

Edit for debugging (see comments):

my test:

 | / | --> doc | | | --> doc.php (takes doc as GET parameter) | | index.php 

My apache rewrite

 RewriteCond %{QUERY_STRING} ^doc=([az]+)$ [NC] RewriteRule ^$ /doc/%1? [NC,R,L] RewriteRule ^doc/([az]+)$ /doc/doc.php?doc=$1 [NC,L] 

Then the query for domain.com/?doc=query displays the doc is query

It works for me.

+1
source share

All Articles