4

I'm trying to write an .htaccess file that will make my URLs more attractive to search engines. I know basically how to do this, but I'm wondering how I could do this dynamically.

My URL generally looks like:

view.php?mode=prod&id=1234

What I'd like to do is take the id from the url, do a database query, then put the title returned from the DB into the url. something like:

/products/This-is-the-product-title

I know that some people have accomplished this with phpbb forum URLs and topics, and i've tried to track the code down to where it replaces the actual URL with the new title string URL, but no luck.

I know I can rewrite the URL with just the id like:

RewriteRule ^view\.php?mode=prod&id=([0-9]+) /products/$1/

Is there a way in PHP to overwrite the URL displayed?

chaoskreator
  • 889
  • 1
  • 17
  • 39

3 Answers3

6

At the moment you're wondering how to convert your ugly URL (e.g. /view.php?mode=prod&id=1234) into a pretty URL (e.g. /products/product-title). Start looking at this the other way around.

What you want is someone typing /products/product-title to actually take them to the page that can be accessed by /view.php?mode=prod&id=1234.

i.e. your rule could be as follows:

RewriteRule ^products/([A-Za-z0-9-])/?$ /view.php?mode=prod&title=$1

Then in view.php do a lookup based on the title to find the id. Then carry on as normal.

Matt
  • 9,068
  • 12
  • 64
  • 84
  • So does that mean I would need to go back through and edit my existing inter-site links? – chaoskreator May 01 '11 at 01:10
  • Yes, it does. I guess that makes this answer more of a future solution. To make the old links work you could do a lookup based on the `id` you have, find the title and then redirect to `/products/looked-up-title`. No changes to `.htaccess` for that, just your `view.php` – Matt May 01 '11 at 01:12
  • Well, I gave it a shot, using the rewrite rule you posted above, but i get a 404 error. – chaoskreator May 01 '11 at 02:03
  • just out of curiosity, what am i doing wrong with this: `RewriteRule ^/shipping/$ /index.php?mode=shipping`? because i get a 404 with this simple one, too – chaoskreator May 01 '11 at 02:15
  • I'd made a mistake with the `RewriteRule`. It did start `^/products/...` but that first slash will make it not work, it should be `^products/...` (I've updated my answer) – Matt May 01 '11 at 08:36
  • Also, my earlier comment saying **"Yes, it does. I guess that makes this answer more of a future solution"**, isn't true. The rest of the comment is right though. Bottom line being that you don't need to do lots of rewrites. – Matt May 01 '11 at 08:39
  • Thanks! I got things working fairly well on the SEO, except now my session id gets destroyed and the use logged out on page change. – chaoskreator May 02 '11 at 02:10
2

One way to do it, would be just like most mvc frameworks. You can redirect all your pages to the same index.php file, and you use your script to determine which page to load.

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>

and your php file will have a script like this one:

  // get the url
  $uri = (isset($_SERVER['REQUEST_URI']))?$_SERVER['REQUEST_URI']: false;
  $query = (isset($_SERVER['QUERY_STRING']))?$_SERVER['QUERY_STRING']: '';
  $url = str_replace($query,'',$uri); // you can edit this part to do something with the query
  $arr = explode('/',$url);
  array_shift($arr);

  // get the correct page to display
  $controller =!empty($arr[0])?$arr[0]:'home'; // $arr[0] could be product/ 
  $action = isset($arr[1]) && !empty($arr[1])?$arr[1]:'index'; // $arr[1] can be product-title
    }

of course you will have to work this code to fashion your application

I hope this helps

Ibu
  • 42,752
  • 13
  • 76
  • 103
0

One way would be to output a Location: header to force a redirect to the chosen URL.

jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107