URL rewriting - Beginners guide

2019-03-02 13:29发布

问题:

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.

回答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.



回答2:

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.