zish zish - 1 year ago 186
Apache Configuration Question

Remove query string from URL using MOD_REWRITE

Okay I am facing a lot of problems while writing clean URLs using mod_rewrite.
Say I have a link like


I want to convert it into a clean URL like


which I have done so far using the following rewrite rule

RewriteRule ^/?([a-zA-Z_]+)$ search.php?q=$1 [L]

Now when I enter this clean URL directly into my browser it works fine but when I open up my website and inquire anything through search box of my website it still shows


I just want to how to fix this issue as when I enter clean URL it works fine but upon searching from search box instead of directly writing the URL in browser it still shows the url with query string.
THANKS ALOT any help will be appriciated

Answer Source

Change your HTML code to disable form submit and always use pretty URL by using this HTML snippet:

function prettySubmit(form, evt) {
   window.location = '/' + form.q.value.replace(/ /g, '+');
   return false;

<form method="get" action="search" onsubmit='return prettySubmit(this, event);'>
   <input type="text" name="q" class="txtfield" 
       value="<?php if (isset($_GET['q'])) echo $_GET['q']; ?>" 
        placeholder="Search Keyword" />
   <input type="submit" name="submit" class="btn" value="Search" />

Then have this rule in site root .htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-zA-Z_]+)$ search.php?q=$1 [L,QSA]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download