SitePoint Sponsor

User Tag List

Results 1 to 22 of 22

Thread: URI handling

  1. #1
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    URI handling

    Parsing and building based on a mask/format.

    This is how I would guess WordPress does it to allow totally customized URI's?

    Anyone care to share ideas, experiences, thoughts on the whole process of making configurable SEF URI's?

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2008
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use .htaccess with mod_rewrite engine
    for example, .htaccess below will rune index.php for all url entered, uri is passed via get parameter "uri"

    RewriteEngine On
    RewriteRule ^(.*)$ index.php?uri=$1

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or just:
    Code:
    RewriteRule ^.*$	index.php [L]
    And then access the uri through $_SERVER['REQUEST_URI']

    By the way .. You would probably want to extend that rule a bit to:
    Code:
    DirectoryIndex index.php
    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME}  -d
    RewriteRule  ^.*$  -  [L]
    
    RewriteCond %{REQUEST_FILENAME}  -f
    RewriteRule  ^.*$  -  [L]
    
    RewriteRule ^.*$	index.php [L]
    to allow static files to be served.

  4. #4
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    Or just:
    Code:
    RewriteRule ^.*$	index.php [L]
    And then access the uri through $_SERVER['REQUEST_URI']

    By the way .. You would probably want to extend that rule a bit to:
    Code:
    DirectoryIndex index.php
    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME}  -d
    RewriteRule  ^.*$  -  [L]
    
    RewriteCond %{REQUEST_FILENAME}  -f
    RewriteRule  ^.*$  -  [L]
    
    RewriteRule ^.*$	index.php [L]
    to allow static files to be served.
    That could be condensed quite a bit into this:

    Code:
    DirectoryIndex index.php
    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME}  !-d
    RewriteCond %{REQUEST_FILENAME}  !-f
    RewriteRule ^.*$	index.php [L]

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice .. I didn't realise that you could negate those flags.

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Another way to write it, I found out from Dklynn
    Code:
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .? index.php [QSA,L]
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Zealot
    Join Date
    Oct 2007
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    RewriteRule .? index.php [QSA,L]

    should surely be:

    RewriteRule (.+)? index.php [QSA,L]

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Nope it is ".?" no need for anything else. Everything should be contained in "$_SERVER['REQUEST_URI']"
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Zealot
    Join Date
    Oct 2007
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah, yes I had overlooked the PHP role involved here

  10. #10
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you all for th replies, but...while WordPress does use mod_rewrite I don't believe mod_rewrite is responsible for handling the permalinks.

    Basically what I was looking for was implementation ideas on how to parse/build URI's like how WordPress does internally.

    You can configure the way WP renders and parses URI's in the config and I don't believe .htaccess is modified directly.

    Instead I assume WP basically has a simple .htaccess which passes all requests to index.php and WP uses an internal URI API to parse the API according to a mask/format.

    Cheers,
    Alex

  11. #11
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can browse yourself rewrite.php in wp-includes folder.

  12. #12
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have actuall looked at that file but it's lengthly and complex.

    I was hoping more for a disucssion on algorithm/design rather than getting implementation specific.

    For example, given a mask in a form:

    %post_id%/%post_title%/%post_cat%

    Assuming there are several other placeholders, like date, author, etc.

    How would I use a high level mask/format like above to parse a URI into known parts, without resorting to complex regex -- which WP users will definetely not understand, right?

    Cheers,
    Alex

  13. #13
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It depends on rules you set for routes. Each part of route can be static or dynamic. For example:
    /%module%/%item_name%
    is route with two dynamic part, so you must check both parts.
    /search/%parameters%
    is route with one static and one dynamic part.

    So you must create rules and then check step by step each part of REQUEST_URI. (To check not only conformance with pattern, but also existence of given part.) Patterns for routes should be in some config file, maybe you could have a look at Symfony:
    http://www.symfony-project.org/book/...Routing-System

    By the way, your mask %post_id%/%post_title%/%post_cat% is IMHO bad, because post_title is dependent on id (so one part suffices) and category has higher level of importance so it should be on the beginning.

  14. #14
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By the way, your mask %post_id%/%post_title%/%post_cat% is IMHO bad, because post_title is dependent on id (so one part suffices) and category has higher level of importance so it should be on the beginning.
    I've wondered the same of some examples I've seen of WordPress when a date is used. Obviously you cannot uniquely return a single blog entry when you select it based on date, so I can only assume that when date is in the URI that it's being used for front end organization (user friendliness or showing entries according to certain dates, RSS, etc).

    My example I gave was only that. As I understand only title/keywords and PKID would actually be useful in returning a single page.

    I will look into Sympphony, thank you.

  15. #15
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    No harm in having the id and title in there if it is for SEO / user friendliness purposes, but normally I would make sure the title was unique and get rid of the id from the url.

  16. #16
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been playing around with rewrites a bit using what was mentioned in this thread. I'm trying to make a default rewrite that will apply for each folder that contains a file router.php. Is this the best way to do it?

    Code:
    RewriteEngine on
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^/([^/]*).*/
    RewriteCond %{DOCUMENT_ROOT}/%1/router.php -f
    RewriteRule ^/[^/]* /%1/router.php [L]
    Also, I'm wondering how I can disable /%1/admin/ from being rewritten. Nothing I've tried seems to work.

  17. #17
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by allspiritseve View Post
    I've been playing around with rewrites a bit using what was mentioned in this thread. I'm trying to make a default rewrite that will apply for each folder that contains a file router.php. Is this the best way to do it?
    No. It's better to have just 1 .htaccess file and I don't see the need for multiple routing files, 1 should suffice.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  18. #18
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    No. It's better to have just 1 .htaccess file and I don't see the need for multiple routing files, 1 should suffice.
    It's actually the virtualhost file for my local server, so no .htaccess files are needed.

  19. #19
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by allspiritseve View Post
    It's actually the virtualhost file for my local server, so no .htaccess files are needed.
    You still shouldn't need multiple router.php files.

    To prevent admins from being rewritten, what about something like:
    Code:
    RewriteCond %{REQUEST_URI} !admin/?$
    RewriteRule ...

  20. #20
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Young Twig View Post
    You still shouldn't need multiple router.php files
    One for each site. Also, that means if a site doesn't have router.php, it won't use clean urls.

  21. #21
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The Akelos framework has an interesting routing/mapping system

    http://akelos.org/wiki/routes

    I used a similar syntax for our routing system, with the option to compile to mod_rewrite syntax for performance.

    The beauty of this system is that the code to turn route connections into regular expressions that can be run against an incoming REQUEST_URI is quite long (50-60 lines) but the actual matching function is only 10 lines and consists of looping through the regexps until you get one that matches (or you run out, in which case throw a 404).

  22. #22
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Code:
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .? index.php [QSA,L]
    this is very cool i reckon. i've used it to go between normal serving of files and the not found page. and in the php file the above routes to i've got php redirects from any filename missing or too many hyphens to the correct hyphenated file name. so say in the root folder i've got a file called where-we-are.php. (extensionless urls are being done by apache's MultiViews). and someone accesses:
    myserver.com/whereweare
    or
    myserver.com/where-weare
    or even
    myserver.com/-w---herewe--a---re
    (which is silly but anyway) they'll be redirected, not rewritten, to
    myserver.com/where-we-are
    so it allows people to be lazy/sloppy with hyphens (in the filename -- i havne't done this the the folders part) but still end up with the hyphenated version.

    it allows you to output urls for humans (e.g. verbally, in print) in a simpler, the more standard format (no hyphens), but still have the more readable versions with hyphens in the end. pretty nifty i reckon.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •