caw
caw

Reputation: 31487

Change GET-URL to canonical (mod_rewrite)

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>

Upvotes: 0

Views: 638

Answers (1)

M&#39;vy
M&#39;vy

Reputation: 5774

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

Upvotes: 1

Related Questions