URL rewriting - Beginners guide

余生长醉 提交于 2019-12-02 09:07:44

问题


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.



来源:https://stackoverflow.com/questions/5707129/url-rewriting-beginners-guide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!