1

After asking this question: Clean URLs for search query? I tried something with mod_rewrite:

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

RewriteRule ^s/([a-z]+)$ /?s=$1 [NC,L]

What's the goal?

  1. Redirect http://example.com/?s=query to http://example.com/s/query
  2. Rewrite http://example.com/s/query to http://example.com/?s=query

This looks like double work but if you take a good look you see what I try to accomplish:

  1. Redirect any search querystring to a cleaner equivalent (be it a form, or somebody typing it in directly)
  2. Rewrite (not redirect) that URL back to dynamic querystring so that I can get it with PHP via $_GET

If I think about it like this it should be possible. So I would like to seek the help of the experienced mod rewriter to help me out with this one.

Number 2 works but that's it.

Community
  • 1
  • 1
DADU
  • 6,482
  • 7
  • 42
  • 64
  • Would there be any chance you could help me with my query? http://stackoverflow.com/questions/11486211/generating-clean-urls-when-using-forms – CraigColes Jul 15 '12 at 09:53

1 Answers1

2

This should work, I tested it with some different names and dirs, but that should be ok in your case.

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

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

RewriteRule ^s/([a-z]+)$ /?s=$1 [NC,L] 

Edit for debug (see comments) :

my test is :

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

My apache rewrite is

RewriteCond %{QUERY_STRING} ^doc=([a-z]+)$ [NC]
RewriteRule ^$ /doc/%1? [NC,R,L]

RewriteRule ^doc/([a-z]+)$ /doc/doc.php?doc=$1 [NC,L]

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

Works for me.

M'vy
  • 5,696
  • 2
  • 30
  • 43
  • The first two rules seem to work but when I uncomment the third it gives this error: "This webpage has a redirect loop" in Chrome and a similar one in Firefox. I do not quite understand this since the first two rules redirect, then the htaccess file gets loaded again but the first two rules aren't matching anymore since it is already redirected and only the third rule applies. – DADU Mar 29 '11 at 13:39
  • 1
    Edited. I forgot a blank line, did you saw that? It may be the problem. I added my conf to debug. Keep me informed. – M'vy Mar 29 '11 at 13:52
  • The only thing I can see changed is this part: /doc/doc.php instead of /. I really hope we can solve this problem together. With this code it gives a 500 internal server error: "mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary" – DADU Mar 29 '11 at 14:04
  • The first two lines work when used apart from the third line and visa versa. Even with your first example. So apart they work correct but not when used together. – DADU Mar 29 '11 at 14:08
  • 1
    Maybe the .htaccess is reloaded afterwards. I guess when it does : `xx/?s=query --> xx/s/query --> xx/?s=query` and loops. I thing you need to make some file to receipt the last line like `s.php?s=query` then if would not match the rewrite condition since it include a filename. BTW you have already that file that process the query string, so just add it in front of the `/?s=$1 [NC,L] `. – M'vy Mar 29 '11 at 14:27
  • If I add index.php before it like this: `index.php/?s=$1` it seems to work indeed. Thanks, really appreciate it! Since the new URL after the redirect of the first search query is this: **/s/query** we get this with a new query: **/s/query?s=query**. Is there any solution to solve this side effect? – DADU Mar 29 '11 at 14:37
  • 1
    You mean you still have the query string? – M'vy Mar 29 '11 at 14:38
  • Let's clear things up: Your code works. But when I do a second search (GET), an extra query string is attached to the URL. This has no influence on search results but it looks strange: **/s/query?s=query** – DADU Mar 29 '11 at 14:43
  • 1
    How do you call the second search? make sure you use the correct relative path so it also goes through the rewrite rule. – M'vy Mar 29 '11 at 14:51
  • Hi M'vy I am sorry I had some code in the action attribute of the form that caused this behaviour. I want to thank you a million times because you are the only one that helped me out with this question and you have followed up with what was going on very well. Thanks! – DADU Mar 29 '11 at 14:56