2

I want when search form submitted, the URL be like this :

localhost/xampp/external/proj3/search/searchquery

Instead of this:

localhost/xampp/external/proj3/tools/search/search.php?query=searchquery

anubhava
  • 761,203
  • 64
  • 569
  • 643
AliN11
  • 2,387
  • 1
  • 25
  • 40

2 Answers2

3

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /xampp/external/proj3

# external redirect from action URL to pretty one
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?query=([^\s&]+) [NC]
RewriteRule ^ search/%1? [R=302,L,NE]

# internal forward from pretty URL to actual URL
RewriteRule ^search/([^/.]+)/?$ search.php?query=$1 [L,QSA,NC]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thank for reply. I think my condition in question wasn't clear enough.I've edited m question.please see it again. – AliN11 Jan 31 '15 at 09:39
  • First `RewriteRule` doesn't redirect. I should modify URL manually to work. – AliN11 Jan 31 '15 at 10:06
  • Root `http://localhost/xampp/external/proj3/` (htaccess file located here with `RewriteBase /xampp/external/proj3`) The exact URL is : `http://localhost/xampp/external/proj3/search?query=h` There is no more more rules about `search` – AliN11 Jan 31 '15 at 20:16
  • The `http://localhost/xampp/external/proj3/search?query=h` shows 404 error. but it works when changing to this `http://localhost/xampp/external/proj3/search/h ` – AliN11 Jan 31 '15 at 20:19
  • 1
    Thanks. Finally worked ! You are **htaccess** hero :) – AliN11 Feb 01 '15 at 20:13
0

Create a .htaccess file in your document root. And put this:

RewriteEngine   On
RewriteRule     ^search/([a-zA-Z0-9]+)/?$   \
                search.php?query=$1 \
                [NC,L]

Note: Be careful with the regular expression. Filter it cautiously.

psiyumm
  • 6,437
  • 3
  • 29
  • 50