zack
zack

Reputation: 484

Rewriteing a GET request with mod rewrite?

Can anyone explain me how to rewrite get request in a search form.

This is the code of my form

<form action="searchpage.php" method="get ">
        <input type="text" name="search"  placeholder="Search" required>
        <button type="submit">Search</button>
</form>

and this is the line i use in my .htaccess

RewriteRule searchpage-search-(.*)\.html$ searchpage.php?search=$1 [L]

But this doesn't seems to work. if anyone can help me out with this, it will be most appreciated.

Upvotes: 0

Views: 46

Answers (1)

Sumurai8
Sumurai8

Reputation: 20737

The form does a request to /searchpage.php?search=term, so you'll first need an external redirect to redirect the user from that url to your custom url. Then you need an internal rewrite to properly interpret that. To do an external redirect for every search request your users do seems however counter-productive to me.

#The external redirect
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /searchpage\.php\?search=(.*)\ HTTP
RewriteRule ^searchpage\.php$ /searchpage-search-%2.html? [R,L]

#Internal rewrite
RewriteRule searchpage-search-(.*)\.html$ searchpage.php?search=$1 [L]

Upvotes: 1

Related Questions