zish
zish

Reputation: 627

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

http://www.example.com/search.php?q=variable I want to convert it into a clean URL like

http://www.example.com/variable

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

http://www.example.com/search.php?q=variable

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

Upvotes: 1

Views: 426

Answers (2)

anubhava
anubhava

Reputation: 784968

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

<html>
<head>
<script>
function prettySubmit(form, evt) {
   evt.preventDefault();
   window.location = '/' + form.q.value.replace(/ /g, '+');
   return false;
}

</script>
</head>
<body>
<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" />
</form>
</body>
</html>

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]

Upvotes: 1

Rajpal Singh
Rajpal Singh

Reputation: 311

you can see all instruction available in following website: Complete Tutorial about how to clean url with php and .htacess

Upvotes: 0

Related Questions