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
  • 713,503
  • 59
  • 514
  • 593
AliN11
  • 1,943
  • 1
  • 20
  • 36

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
  • 713,503
  • 59
  • 514
  • 593
  • 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.

activatedgeek
  • 6,250
  • 3
  • 27
  • 48