0

For my search engine, I have the page ...

/index.php?q=TERM

... which displays the search results.

Using mod_rewrite, I made it accessible via:

/q/TERM

The rule I used in the .htaccess was something like this:

RewriteEngine on 
RewriteRule ^q/(.+)$ index.php?q=$1

This works well. But when I enter a term into my HTML form and click the submit button, I'm still redirected to ...

/index.php?q=TERM

How can I make my GET-form directly calling the new and short URL? Its code is:

<form action="/index.php" method="get" accept-charset="utf-8">
...
</form>
caw
  • 30,999
  • 61
  • 181
  • 291

1 Answers1

1

A from will always call your page with ?q=TERM and your rewrite rule only say : /q/TERM is in fact ?q=TERM

Then you need to rewrite ?q=TERM to /q/TERM first. But we need to take care of loops.

So let's try :

EDIT1: other flags. (I found that [C] re run the rules, so we don't want that)

RewriteCond %{QUERY_STRING} ^q=(.*)$ [NC]
RewriteRule ^$ /q/%1 [NC,L]

RewriteRule ^q/(.+)$ index.php?q=$1 [NC,L]

Tell me how that behave.

Related: SO entry

Community
  • 1
  • 1
M'vy
  • 5,696
  • 2
  • 30
  • 43
  • Thank you for this answer :) Unfortunately, my browser tells me: The website redirects this request so that it can never be finished. So the first two lines (RewriteCond + RewriteRule) don't work. The last lines works as it is the line I had before as well. – caw Apr 04 '11 at 13:43
  • Hum, I bet it would fail but can't test it. Gonna check another thread where there was a similar loop. – M'vy Apr 04 '11 at 13:47
  • Now I get this error message: "Internal Server Error: The server encountered an internal error or misconfiguration and was unable to complete your request." – caw Apr 04 '11 at 14:18
  • You have access or error logs to help debug? EDIT: set it up on my config : redirect loop. fixing. – M'vy Apr 04 '11 at 14:20
  • You simply made a mistake while copying the code from your answer to the other question ;) It works perfectly with the second line being: RewriteRule ^$ /q/%1? [NC,R,L] – caw Apr 04 '11 at 14:34
  • Oh nice, did not catch that! good job. I'll edit to make it match the correct anwser. – M'vy Apr 04 '11 at 14:37