Is it better to handle friendly/clean/pretty URLs with mod_rewrite or a language like PHP?

前端 未结 5 841
挽巷
挽巷 2021-02-04 14:55

I\'m developing my first decent-sized PHP site, and I\'m a bit confused about what the \"right way\" (assuming there ever is such a thing) to handle clean/friendly/pretty URLs i

5条回答
  •  攒了一身酷
    2021-02-04 15:57

    Really this is the "are frameworks worth using?" question in disguise.

    Using mod_rewrite to define your URL routes is quick and easy (if you understand regular expressions...) but your application code is oblivious to the URLs unless you duplicate the information somewhere.

    Usually, people duplicate this information many times without thinking about it, by hard-coding URLs in the links in their views, or in redirects. This is really messy, and will one day cause pain when you decide to change the URL structure of your site halfway through development. You're bound to miss one and end up with a 404 somewhere.

    Using a routing component in your application (such as the one in Symfony) means you can attach names to your routes, allowing you to define your URLs once and re-use them many times:

    # apps/frontend/config/routing.yml
    homepage:
      url:   /
      param: { module: default, action: index }
    

    This makes it really easy to link to pages of your site without repeating yourself:

    
    

提交回复
热议问题