Jim Sundqvist
Jim Sundqvist

Reputation: 160

Getting clean urls with .htaccess

I've been going nuts at this with clean urls for some time now and i was hoping to get some assistance here with some tips and hopefully a solution.

Update

Here's my current navigation @anubhava

$select_category = mysql_query("SELECT * FROM menu WHERE hidden = 0 ORDER BY menu ASC");
while ($ln = mysql_fetch_array($select_category)) {
  $idcat = $ln['nmr'];
  $catname = $ln['menu'];
  $catsname = str_replace(' ', '-', $ln['menu']);

  echo '<li>';
  if($catname == "a certain category"){
  echo '<a href="http://mywebsite.aurl.net/" title="'.$catname.'"><span><strong>'.$catname.'</strong></span></a>'. PHP_EOL;
     }else{
  echo '<a href="http://www.mywebsite.com/cats/'.$idcat.'/'.$catsname.'/" title="'.$catname.'"><span><strong>'.$catname.'</strong></span></a>'. PHP_EOL; // if the user press on a main category it will stay open using GET[] on $idcat in categorymain.php
     }
  echo '<ul';


  if(isset($_GET['cats']) && $_GET['cats'] == $idcat){
    echo ' style="display:block;">'. PHP_EOL;
  }else{
    echo '>'. PHP_EOL;
  }

  $select_sub = mysql_query("SELECT * FROM submenu WHERE nmrmenu = '$idcat' AND hidden = 0");
  while ($lsub = mysql_fetch_array($select_sub)) {
      $subname = $lsub['submenu'];
    $subsname = str_replace(' ', '-', $lsub['submenu']);
    $pil = '&raquo;';
    $brnr = $lsub['nmr'];

    if(isset($_GET['cat'])){
    if ($_GET['cat'] == $brnr){
        $subname = '<u>'.$lsub['submenu'].'</u>'; // here the subcategory gets underlined if the user press on a sub category in category.php

    }else{
        $subname = $lsub['submenu'];
    }
    }

    echo '<li><a href="http://www.mywebsite.com/cat/'.$lsub['nmr'].'/'.$idcat.'/'.$subsname.'/"><span><strong> '.$pil.' '.$subname.' </strong></span></a></li>'. PHP_EOL; // here i send both main and sub category id so i can keep the main category opened and the underlined sub category when a user has pressed it.
  }

  echo '</ul>'. PHP_EOL;
  echo '</li>'. PHP_EOL;
   }

/update

Currently my code in htaccess look like this:

RewriteCond %{REQUEST_URI} cats/(.*)/
RewriteRule ^cats/([a-zA-Z0-9]+)/(.*)/$ /categorymain.php?cats=$1&mcn=$2

Which displays like this: www.xxxxx.com/cats/x/the_main_category/ and it's not very nice.

My goal is: www.xxxxx.com/the_main_category/

And i even got this:

RewriteCond %{REQUEST_URI} cat/(.*)/
RewriteRule ^cat/([a-zA-Z0-9]+)/(.*)/(.*)/$ /category.php?cat=$1&cats=$2&cn=$3

Which displays like this: www.xxxxx.com/cat/x/x/the_sub_category/ and it's really not nice.

My goal is: www.xxxxx.com/the_sub_category/

Lastly i've got this really horrible line:

RewriteCond %{REQUEST_URI} id/(.*)/
RewriteRule ^id/([a-zA-Z0-9]+)/(.*)/(.*)/(.*)/$ /product.php?id=$1&cat=$2&cats=$3&pn=$4

Which displays like this: www.xxxxxx.com/id/xxxx/x/x/the_product/ horrid :O

My goal is: www.xxxxx.com/the_product/

I would be incredibly happy if you could help me solve this issue

The reason for so many variables is because i use them in the navigation so the users easily can see where they are in the e-store if they want to go back, also a "internal" navigation above the categories and products so they don't have to use the side navigation more than necessary.

Upvotes: 1

Views: 721

Answers (3)

anubhava
anubhava

Reputation: 785128

Based on your comments, try this rule:

RewriteEngine On

RewriteRule ^([\w-]+)/([0-9]+)/?$ /product.php?id=$2&pn=$1 [L,NC,QSA]

This will support URI structure as: /productname/123 to be internally rewritten as /product.php?id=123&pn=productname

Upvotes: 1

Dave
Dave

Reputation: 3658

It sounds like what you really need is to simply pass everything to a single location:

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

You would then parse the requested URL in PHP and determine whether a main category, sub category, or product was requested (by comparing to a database or the like).

I understand you want "clean" URLs, but restricting your URLs to just text will only work if each main category, sub category, and product name is unique, which most certainly is not realistic. Your original design /some_id/some_name/ is perfectly fine. Having a number in the URL doesn't make it "unclean."

Upvotes: 0

jrthib
jrthib

Reputation: 1319

Essentially what you're going to need is a "slug" which is a unique identifier string for each category. If the category name is "Food Stuff", your unique slug would need to be "food-stuff" for the URL. Once you've established these unique slugs, change your script to look up by slug &cat=food-stuff rather than by the ID in the database.

From here its rather trivial, you can just use your rewrite rules and accomplish your desired result.

RewriteCond %{REQUEST_URI} /(.*)/
RewriteRule ^/([a-zA-Z0-9]+)/(.*)/$ /categorymain.php?cats=$1&mcn=$2

Upvotes: 0

Related Questions