Pretty URLs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Laeronai

    Pretty URLs

    I'm making a blog cms and have been having trouble with making the URLs
    look good. Each post goes to a URL like "viewpost.php?i d=40" but I want
    the URL to look like "YYYY/MM/TITLE" so it would come out to be
    "/2006/03/hey-look-its-march." Does anyone know how to do this in PHP?

    And also, what is this process called? I'd like for the cms to do it
    automatically for each post entered.

  • Carl Vondrick

    #2
    Re: Pretty URLs

    Laeronai wrote:[color=blue]
    > I'm making a blog cms and have been having trouble with making the URLs
    > look good. Each post goes to a URL like "viewpost.php?i d=40" but I want
    > the URL to look like "YYYY/MM/TITLE" so it would come out to be
    > "/2006/03/hey-look-its-march." Does anyone know how to do this in PHP?
    >
    > And also, what is this process called? I'd like for the cms to do it
    > automatically for each post entered.
    >[/color]

    If you are running off Apache, you should look into mod_rewrite. Just
    do a Google search for more info.

    Good luck

    --
    Carl Vondrick
    Professor of Computer Science at Columbia University, researching computer vision, machine learning, and AI applications.

    usenet [at] carlsoft [dot] net

    Comment

    • Laeronai

      #3
      Re: Pretty URLs

      Thanks.

      Comment

      • Laeronai

        #4
        Re: Pretty URLs

        Wow that's a complex module....might take a while to understand.

        Comment

        • David Quinton

          #5
          Re: Pretty URLs

          On 4 Mar 2006 19:03:03 -0800, "Laeronai" <wizzlefish@gma il.com> wrote:
          [color=blue]
          >Wow that's a complex module....might take a while to understand.[/color]

          It will do you good!

          If you can't / won't use mod_rewrite, I've sometimes used a php
          method to achieve the same end result.

          Make directory / subdirectories to match the "user friendly" path that
          you want people to see / enter.

          Place an index.php in the lowest level directory which explodes the
          $PATH_INFO into an array.
          e.g. list (, $Var1, $Var2) = explode ('/',$PATH_INFO);

          Form the "real" url by appending the elements
          e.g. $realurl='http://www.mysite.com/myscript.php?fi rstget=' . $Var1 .
          '&amp;secondget =' . $Var2 etc.

          Then do a
          header("locatio n: $realurl");

          You'll also need an .htaccess to
          ForceType application/x-httpd-php
          DirectoryIndex index.php



          Now I've typed all this - you're probably better off with mod_rewrite!
          --
          Locate your Mobile phone: <http://www.bizorg.co.u k/news.html>
          Great gifts: <http://www.ThisBritain .com/ASOS_popup.html >

          Comment

          • Janwillem Borleffs

            #6
            Re: Pretty URLs

            Laeronai wrote:[color=blue]
            > I'm making a blog cms and have been having trouble with making the
            > URLs look good. Each post goes to a URL like "viewpost.php?i d=40" but
            > I want the URL to look like "YYYY/MM/TITLE" so it would come out to be
            > "/2006/03/hey-look-its-march." Does anyone know how to do this in PHP?
            >[/color]

            As an alternative to what others have suggested, you could also do the
            following:

            Say, you want to map http://host/path/33 to http://host/path/?id=33

            1. Have the ErrorDocument directive pointing to a file that can handle the
            request; let's call it "redirect.p hp":
            ErrorDocument 404 redirect.php

            2. In redirect.php, the requested URL can be retrieved from the
            $_SERVER['REQUEST_URI'] variable, which can be parsed as follows:

            if (preg_match("#^/path/(\d+)/*$#", $_SERVER['REQUEST_URI'], $match)) {
            header("Locatio n: http://{$_SERVER['HTTP_HOST']}/path/?id=$match[1]");
            exit;
            } else {
            // Display error message
            }


            JW



            Comment

            • Laeronai

              #7
              Re: Pretty URLs

              If I used your example, Janwillem, would I include "redirect.p hp" on
              every page? I'm kinda new to PHP, not really sure how to do this kinda
              fancy stuff. But I can figure it out.

              If I wanted to use /YYYY/MM/title as the format, then I guess I could
              retrieve it from the database where the id has to match the one in the
              URI, but once I had the date in DATETIME format like YYYY MM how could
              I separate the two into two different variables?

              Somehow I'd end up with $year and $month, and $title, which would
              transform the original title to something all lowercase and hyphens
              where all the spaces should be, so "Hello Bob" would become
              "hello-bob." Once I had that, I suppose I could use your code.

              What does the "#^" mean, and the "(\d+)" mean? I'm a bit confused.

              If I were to use mod_rewrite, and I have absolutely no experience in
              Apache, how would I use it?

              Comment

              • Chung Leong

                #8
                Re: Pretty URLs

                Hmmm..., but if you redirect to the new URL then it's pretty no longer.
                Might make more sense to include the intended script instead.

                Comment

                • Janwillem Borleffs

                  #9
                  Re: Pretty URLs

                  Laeronai wrote:[color=blue]
                  > If I used your example, Janwillem, would I include "redirect.p hp" on
                  > every page? I'm kinda new to PHP, not really sure how to do this kinda
                  > fancy stuff. But I can figure it out.
                  >[/color]

                  The redirect.php page should be part of the ErrorDocument directive shown in
                  my previous post. In short, when the page doesn't exist, send the request to
                  redirect.php (ErrorDocument 404 redirect.php)
                  [color=blue]
                  > What does the "#^" mean, and the "(\d+)" mean? I'm a bit confused.
                  >[/color]

                  This is part of the regular expression synatx. Never heard of it? Start
                  reading at: http://www.php.net/manual/nl/pcre.pattern.syntax.php
                  [color=blue]
                  > If I were to use mod_rewrite, and I have absolutely no experience in
                  > Apache, how would I use it?
                  >[/color]

                  Read about it. It sounds like pretty URL's will be the goal and you will
                  have to do some learning to reach it. Start to gain some basic knowledge of
                  regular expressions as you will also need them when applying the mod_rewrite
                  directives.


                  JW


                  Comment

                  • Andy Jeffries

                    #10
                    Re: Pretty URLs

                    On Sat, 04 Mar 2006 15:03:56 -0800, Laeronai wrote:[color=blue]
                    > I'm making a blog cms and have been having trouble with making the URLs
                    > look good. Each post goes to a URL like "viewpost.php?i d=40" but I want
                    > the URL to look like "YYYY/MM/TITLE" so it would come out to be
                    > "/2006/03/hey-look-its-march." Does anyone know how to do this in PHP?[/color]

                    As another example you could put the following at the top of your
                    viewpost.php script:

                    <?php
                    list($ignore, $year, $month, $title) = explode($_SERVE R["PATH_INFO"]);
                    ?>

                    And link to:

                    /viewpost.php/2006/03/hey-look-its-march

                    The server will use viewpost.php as the script and put anything after the
                    script name that looks like a path into $_SERVER["PATH_INFO"].

                    Cheers,


                    Andy


                    --
                    Andy Jeffries | gPHPEdit Lead Developer
                    http://www.gphpedit.org | PHP editor for Gnome 2
                    http://www.andyjeffries.co.uk | Personal site and photos

                    Comment

                    Working...