-2

I want to make the clean url for my site. How do I change htaccess file. My original Link that

This is my original link

  **http://www.tangailbazar.com/adview_details.php?ID=9014&show=Hot%20and%20Cool%20Water%20Filter**

I want to make it like this

    http://www.tangailbazar.com/Hot-and-Cool-Water-Filter

My code is here.

   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule /^(.*) adview_details.php?show=$1 [PT,QSA]

My post link from where i get the value like this

    <a href="view-items-details.php?ID=<?php echo $row['sl'];?>&show=<?php echo $row['title'];?>">View Details</a>

In adview_details page I have written the code

    <?php


   $id=$_GET['ID'];
   $id1=$_GET['show'];

   ?>
   <?php 
   $SQL="select * from tb_classified where sl='$id' and title='$id1'";
   $obj->sql($SQL);

    while($row = mysql_fetch_array($obj->result))

    {

  echo $row['title'];
      echo $row['description'];
  }
  ?>
user3259990
  • 71
  • 1
  • 1
  • 5
  • You'll have to either embed 9014 into the URL or map Hot-and-Cool-Water-Filter back to it, if it's an important number. – user2864740 Feb 01 '14 at 08:35
  • The approach stack overflow uses is to include the id and the question title but only lookup the question by its ID (the title becomes useful in making a clean looking URL even though technically it's optional) – Jason Sperske Feb 01 '14 at 08:38
  • Your script is vulnerable to SQL injections; you should read [how to prevent them](http://stackoverflow.com/q/60174/53114). – Gumbo Feb 01 '14 at 09:11

3 Answers3

0

You are looking for an Apache mod called mod_rewrite. Specifically (from the documentation):

If, on the other hand, you wish to pass the requested URI as a query string argument to index.php, you can replace that RewriteRule with:

RewriteRule (.*) adview_details.php?show=$1 [PT,QSA]

Note that these rulesets can be used in a .htaccess file, as well as in a block.

Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
0

First what draws my attention is that your regular URL is not as clean to start with. I would advise you to remove the spaces and change them for hyphens—if possible. URL are also case insensitive so there is actually no use for uppercase characters, but using them is personal preference though it makes them less readable and when rewriting it's an extra process to change them.

When rewriting a URL, you should think of your URL as parts. There is the domain which is divided in: subdomain (www), domain-name (tangailbazar) and the top-level domain (com). After the domain begins the cleaning up (you can also do a lot of rewriting on the domain, but in your case you will not).

To keep your rewrite process clear and simple (because URL rewriting with mod_rewrite can give massive headaches) you want every part of a rewritten URL (between slashes) to translate to a parameter in the raw URL. So in your case: ID=9014&show=Hot and Cool Water Filter are two parameters that should be used in your clean URL. When rewriting the URL you would pick the value of key (parameter) ID and show and use them in your clean URL. In your case you need the ID parameter in the clean URL otherwise you can never not load your destination URL.

RewriteRule ^.+/(.+)/(.+)+$ adview_details.php?ID=$1&show=$2 [L]

This rule will change the request for:

www.tangailbazar.com/9014/Hot%20and%20Cool%20Water%20Filter

into:

www.tangailbazar.com/adview_details.php?ID=9014&show=Hot%20and%20Cool%20Water%20Filter

This is the best you can do in this case, otherwise you will have to change some things about the structure of the destination URL.

An important thing you need to get hold of is that the name URL rewriting is misleading, you actually don't rewrite anything, you translate the requested URL from the browser into the URL where you're page is located. So it's more of a 'URL translate' then a 'URL rewrite'. In creating user friendly URL's you most of the time are removing the parameter keys (show=) and file extension (.php) from the URL to make them more readable.

You will always need to use the dynamic parts in your URL and you can clean the static parts.

I hope this will help you solve your problem or at least make some things clear on rewriting URL's.

Good luck!

Community
  • 1
  • 1
sidneydobber
  • 2,790
  • 1
  • 23
  • 32
0
<?php 
$title = str_replace(' ', '-', $row['title']) 
?>
<a href="<?php echo "/".$title ;?>">View Details</a>

Output URL http://www.tangailbazar.com/Hot-and-Cool-Water-Filter but this will not work, why? because you can not make a url with a condition, while for the request to mysql need two conditions. maybe you should change your url into http://www.tangailbazar.com/1/Hot-and-Cool-Water-Filter If you follow my advice you should change the code. htaccess becomes

RewriteRule ([0-9]?)/([a-zA-Z_-]+) adview_details.php?ID=$1&show=$2 [PT,QSA]

and the URL that you should use

<a href="<?php echo "/".$row['sl']."/".$title ;?>">View Details</a>