1

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

0

2 Answers 2

1

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]
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.