1

I need to learn how to do URL rewriting for a site to get pretty URLs...

I have a general idea of how it works through the 50 tutorials I read. But none of them really clicked and never managed to make it properly work.

Do you know of any tutorials that are good for me to start?

Here is an example of how I want to use it:

Here is the basic link http://www.dico2rue.com/dictionnaire.php?idW=675&word=Resto-basket

I want it to become like this: http://www.dico2rue.com/dictionnaire/675/Resto-basket

But when I did that, all my links (like for CSS sheets, images,...) didn't work. Do I need to full linking (which apparently slows your site down)?

Thanks for your help.

UDPATE There are 2 options for the links

  • PAGE = ?page=1
  • WORD = ?idW=45&word=google-friendly-url-part-here

The code in htaccess is

RewriteRule    ^dictionnaire.php/p([0-9]+)?$    dictionaire.php?page=$1    [NC,L]    
RewriteRule    ^dictionnaire.php/([0-9]+)/([a-z])?$    dictionaire.php?idW=$1&word=$2   [NC,L]

I hope this helps.

denislexic
  • 10,786
  • 23
  • 84
  • 128
  • possible duplicate of [How to: URL re-writing in PHP ?](http://stackoverflow.com/questions/1039725/how-to-url-re-writing-in-php) – Talljoe Apr 18 '11 at 18:22
  • @Talljoe - Thanks for the link. I checked what they said but I don't quite understand. But I'll take a look at that tutorial. – denislexic Apr 18 '11 at 18:42

2 Answers2

1

Have a play with something like CodeIgniter, Wordpress, or Drupal. You can see how the URLs are formed and how they map to the contents of .htaccess.

This mod_rewrite cheatsheet is very useful.

Given that your stylesheets are already referenced absolutely (they include http://www.dico2rue.com/) it's probably because you're telling all your images, stylesheets and JavaScript files to go through index.php. Instead you need to tell .htaccess to leave physical files alone, and only parse other URLs.

Post what you have in your .htaccess file here and I'm sure we can help fix it.

Dan Blows
  • 20,846
  • 10
  • 65
  • 96
  • I updated the .htaccess code in the question. Much appreciated. – denislexic Apr 18 '11 at 18:49
  • What do you mean by this? How can I do this? `Instead you need to tell .htaccess to leave physical files alone, and only parse other URLs.` – denislexic Apr 18 '11 at 18:51
  • @denislexic Sorry I didn't notice this question until now. Did you solve the problem, or can I help some more? – Dan Blows Apr 28 '11 at 22:49
  • Thanks for getting back to me. I ended up following your advice, but I had to put full paths to all my images and ecternal scripts...but it works now. Thanks. – denislexic Apr 30 '11 at 09:17
0

Check if you're requesting a existing file using a (negative)RewriteCond using flags -f and/or -d. Used in many tutorials, check the topmost results at any search engine.

Robert de W
  • 316
  • 8
  • 24