Please help me to solve the problem.I want to rewrite the url from

http://localhost/demo/index.php?keyword=sap

TO

http://localhost/demo/sap

but i am facing problem in rewriting url i.e unable to rewrite url to

http://localhost/demo/sap

i have written my htaccess as

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?keyword=$1


Please help...

Thankyou

<?php 
if( (isset($_GET['action'])) && ($_GET['action']=="add")){
   $key=$_POST["keyword"];
?>
 <script>self.location='index.php?keyword=<?=$key?>';</script>";  
<?php 
}
?>
 <form name="frmSearch" method="POST" action="index.php?keyword=<?php echo $_POST['keyword']; ?>">
		 
		<div class="input">
			<label for="SearchKeyword">Search Results:</label>
			<input name="keyword" id="SearchKeyword" type="text" value="<?php echo $_POST['keyword']; ?>">
		</div>
		<div class="submit">
			<input value="Search" type="submit" name="Submit">
		</div>
	</form>
 <?php
//echo "GKEY".$_REQUEST['keyword'].$getKey;
if ($_GET['keyword'])
{   
	$key=$_GET["keyword"]; 
//taking data from database and displaying data

}

?>

Hi,

I am htaccess expert.

I think htaccess not working bcoz you are again redirecting url after form submit.

Plz tell me then how to rewrite Url?

Hey.

Your RewriteRule looks like it should work. Can't see a reason why it shouldn't at least. If you print the $_GET['keyword'] in the index.php file, you get the right value, right?

The code, however, doesn't look quite right.
Try removing the trailing "; from line #5. See if that helps.

And, just out of interest, what is line #5 meant to be doing anyways? I can't really see a reason why you would want to put a JavaScript redirect at that point in the code. (Or anywhere in the code, for that matter.)

After printing $_GET, i got the right value,als i have removed the "; from the javascript,eventhough my htaccess is not working properly,please help me in solving the problem. Thank you

I have modified the code and used post method for searching keyword,but it works when i click search button twice,

my code is

<form name="frmSearch" id="frmSearch" method="POST" action="http://localhost/CSV/<?php echo $_POST['keyword']; ?>">
		 
		<div class="input">
			<label for="SearchKeyword">Search Results:</label>
			<input name="keyword" id="SearchKeyword" type="text" value="<?php echo $_POST['keyword']; ?>">
		</div>
		<div class="submit">
			<input value="Search" type="submit" name="Submit">
		</div>
	</form>
 
<?php
  
if ($_GET['keyword'])
 
{
 
$key=$_GET["keyword"];
 
//taking data from database and displaying data
  
}
 ?>

and htaccess code is

RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?keyword=$1

please help me in solving this problem.

How to avoid hitting search button twice and please suggest any alternative way.

You are currently sending your form using the POST method, and adding the value to the URL of the next form submission, which is why you have to submit twice.

If you use the POST method on your form, you need to use the $_POST variable in your code. If you want to use $_GET you need to use the GET method on your form.

So, either change the <form ... method="POST"> of your form to <form ... method="GET"> , or change all $_GET['keyword'] to $_POST['keyword'] .

I have modified all $_GET to $_POST but even then i have to hit search button twice,plz suggest any ways
my code

<form name="frmSearch" id="frmSearch" method="POST" action="http://localhost/CSV/<?php echo $_POST['keyword']; ?>">
 
       
 
      <div class="input">
 
      <label for="SearchKeyword">Search Results:</label>
 
      <input name="keyword" id="SearchKeyword" type="text" value="<?php echo $_POST['keyword']; ?>">
 
      </div>
 
      <div class="submit">
 
      <input value="Search" type="submit" name="Submit">
 
      </div>
 
      </form>
 
       
 
      <?php
 
       
 
      if ($_POST['keyword'])
 
       
 
      {
 
       
 
      $key=$_POST["keyword"];
   
       
 
      //taking data from database and displaying data
   
       
  
      }
 
      ?>

plz suggest any solution .

There is nothing essentially wrong with your code. It shouldn't require more than one submission.
Unless, of course, there is some other part of your code you aren't showing?

But...
Seeing as you are just trying to change:
www.example.com/search/index.php?keyword=string
to:
www.example.com/search/string

I would do this using JavaScript. You can have JavaScript catch the submission of the form, cancel it, and manually redirect the page to the proper URL. Then you would just use a simple RewriteRule to allow the "pretty" version of the URL.
- Note, that if JavaScript is disabled in the client's browser, it doesn't matter. The code would still work. It would just show the "ugly" URL instead of the "pretty " one.

If you had a form like so:

<form id="frmSearch" method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input name="keyword" type="text" value="<?php echo @$_GET['keyword']; ?>">
    <input type="submit" value="Search" >
</form>

You could use this JavaScript to intercept the form submission, and manually redirect to the proper URL.

window.onload = function() {
    document.getElementById('frmSearch').onsubmit = function() {
        var value = this.keyword.value;
        if(value.length > 0) {
            var dir = this.action.replace(/\\/g,'/').replace(/\/[^\/]*\/?$/, '');
            location.href = dir + "/" + encodeURIComponent(value);
        }
        return false;
    }
}

Then all you would need is a simple RewriteRule to catch the keyword.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /search/
    RewriteCond ${REQUEST_URI} !index\.php
    RewriteRule ^(.*)$ index.php?keyword=$1 [QSA]
</IfModule>

The first time yo see the form you $_POST doesn't exist
so

<form name="frmSearch" id="frmSearch" method="POST" action="http://localhost/CSV/<?php echo $_POST['keyword']; ?>">

becomes

<form name="frmSearch" id="frmSearch" method="POST" action="http://localhost/CSV/">

not using you url rewrite but just going to index.php
the second time $_POST exits and

<form name="frmSearch" id="frmSearch" method="POST" action="http://localhost/CSV/<?php echo $_POST['keyword']; ?>">

will become

<form name="frmSearch" id="frmSearch" method="POST" action="http://localhost/CSV/foobar">

this whil triger your url rewrite

just use

<form name="frmSearch" id="frmSearch" method="POST" action="http://localhost/CSV/index.php">

and $_POST for the rest of your script

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.