(Simple) mod_rewrite question

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

    (Simple) mod_rewrite question

    Hello.

    I'm trying to make use of "pretty URLs" with Apache's rewrite module.

    Here's what I want:



    should be replaced with



    My .htaccess looks like that:

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILEN AME} !-f
    RewriteCond %{REQUEST_FILEN AME} !-d
    RewriteRule (.*) index.php

    In the index.php I look at $_SERVER['REQUEST_URI'] and act
    accordingly:

    $requestedURI = empty($_SERVER['REQUEST_URI']) ? false :
    $_SERVER['REQUEST_URI'];

    switch($request edURI) {
    case "/download":
    include "download.p hp";
    break;
    default:
    // do something
    break;

    }

    This works if I go to http://foo.bar/download - it shows index.php
    with the included download.php file. But if I go to http://foo.bar/download/
    (with a slash at the end of the URL) it doesn't do anything (default
    case of switch($request edURI)), because "/download" != "/download/".

    To solve this I could just make two cases: case "/download" and case
    "/
    download/", but I don't really like this solution, if there's a
    better
    one, please tell me.

    Let's take it I use the method with the two cases ("/download" and "/
    download/"), there's another problem:
    In the index.php I've included a stylesheet: "<link rel="stylesheet "
    type="text/css" href="main.css" />". As you can see, I use a relative
    link to the css file. So if I call http://foo.bar/download, it loads
    the css file from http://foo.bar/main.css, but if I enter http://foo.bar/download/
    (with the slash) it can't find the css file, because it tries to
    locate it at http://foo.bar/download/main.css, but of course, there
    is
    no main.css, /download/ is not even a directory... How do I fix this?

    Thanks in advance for any help!
  • Guillaume

    #2
    Re: (Simple) mod_rewrite question

    k3pp0 a écrit :
    RewriteRule (.*) index.php
    Why don't you just do it the other way ?
    Something like that should just work, without needing to parse the URL:

    RewriteCond %{REQUEST_URI} !^index.php
    RewriteRule ^(.*)[/]?$ index.php?$1 [L]

    Then you can add more conditions, specific directories not to parse,
    sub-directories, etc... Depends on what your goal is.
    You should not need any PHP parsing when using URL rewriting imo.

    Regards,
    --
    Guillaume

    Comment

    • Jeremy

      #3
      Re: (Simple) mod_rewrite question

      k3pp0 wrote:
      >
      <snip>
      >
      This works if I go to http://foo.bar/download - it shows index.php
      with the included download.php file. But if I go to http://foo.bar/download/
      (with a slash at the end of the URL) it doesn't do anything (default
      case of switch($request edURI)), because "/download" != "/download/".
      You could always trim the trailing slash off the URI, if you wanted to.

      $requestedURI = preg_replace('/\/$/', '', $requestedURI);

      But you're probably going to run into problems down the road, unless you
      come up with a good way of mapping friendly URIs to the proper action.

      So if I call http://foo.bar/download, it loads
      the css file from http://foo.bar/main.css, but if I enter http://foo.bar/download/
      (with the slash) it can't find the css file, because it tries to
      locate it at http://foo.bar/download/main.css, but of course, there
      is
      no main.css, /download/ is not even a directory... How do I fix this?
      This is an easy one. Change your CSS reference to an absolute path:
      <link rel="stylesheet " type="text/css" href="/main.css" />
      (note the leading slash).

      Jeremy

      Comment

      • Rik Wasmus

        #4
        Re: (Simple) mod_rewrite question

        On Wed, 16 Apr 2008 16:54:58 +0200, k3pp0 <upthekhyber@gm ail.comwrote:
        Hello.
        >
        I'm trying to make use of "pretty URLs" with Apache's rewrite module.
        >
        Here's what I want:
        >

        >
        should be replaced with
        >

        >
        My .htaccess looks like that:
        >
        Options +FollowSymLinks
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILEN AME} !-f
        RewriteCond %{REQUEST_FILEN AME} !-d
        RewriteRule (.*) index.php
        >
        In the index.php I look at $_SERVER['REQUEST_URI'] and act
        accordingly:
        >
        $requestedURI = empty($_SERVER['REQUEST_URI']) ? false :
        $_SERVER['REQUEST_URI'];
        >
        switch($request edURI) {
        case "/download":
        include "download.p hp";
        break;
        default:
        // do something
        break;
        >
        }
        >
        This works if I go to http://foo.bar/download - it shows index.php
        with the included download.php file. But if I go to

        (with a slash at the end of the URL) it doesn't do anything (default
        case of switch($request edURI)), because "/download" != "/download/".
        >
        To solve this I could just make two cases: case "/download" and case
        "/
        download/", but I don't really like this solution, if there's a
        better
        one, please tell me.
        >
        Let's take it I use the method with the two cases ("/download" and "/
        download/"), there's another problem:
        In the index.php I've included a stylesheet: "<link rel="stylesheet "
        type="text/css" href="main.css" />". As you can see, I use a relative
        link to the css file. So if I call http://foo.bar/download, it loads
        the css file from http://foo.bar/main.css, but if I enter

        (with the slash) it can't find the css file, because it tries to
        locate it at http://foo.bar/download/main.css, but of course, there
        is
        no main.css, /download/ is not even a directory... How do I fix this?
        Several propabilities. The first: http://example.com/download is NOT the
        same as http://example.com/download/ for obvious reasons, so why not just
        fail with a Not Found header, as you would with 'example/com/nonsense'?

        Obviously that's not what you want, so you might:
        1)
        a) use $url = trim($url,'/'); or
        b) $paths = explode('/',$url); (for more levels in the future?)
        2) When playing with rewrites, you want all links from the root, so a
        simple '<link .. href="/main.css"solves the second problem.
        --
        Rik Wasmus

        Comment

        • k3pp0

          #5
          Re: (Simple) mod_rewrite question

          So I'm trying to only use the RewriteEngine to handle my url.

          I came across this problem:

          Let's assume I have a url like that:


          For example, this url would open a site that let's you add a new user.
          The "pretty" url should look like http://foo.bar/users/manage/addnew
          This works fine:
          RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?site= $1&mode=
          $2&action=$3 [L]

          But http://foo.bar/index.php?site=users&mode=manage (note that there's
          no action parameter) should also be accessible through the pretty url
          (/users/manage) and so should http://foo.bar/index.php?site=users
          (through /users). The RewriteRule I mentioned above does only work if
          you have a site, mode and action parameter, but not with just a site
          or just a site + mode parameter.

          Is there a way to solve this without making a RewriteRule for each
          case (or probably a better solution than mine)?


          Thanks in advance!

          Comment

          • Jerry Stuckle

            #6
            Re: (Simple) mod_rewrite question

            k3pp0 wrote:
            So I'm trying to only use the RewriteEngine to handle my url.
            >
            I came across this problem:
            >
            Let's assume I have a url like that:

            >
            For example, this url would open a site that let's you add a new user.
            The "pretty" url should look like http://foo.bar/users/manage/addnew
            This works fine:
            RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?site= $1&mode=
            $2&action=$3 [L]
            >
            But http://foo.bar/index.php?site=users&mode=manage (note that there's
            no action parameter) should also be accessible through the pretty url
            (/users/manage) and so should http://foo.bar/index.php?site=users
            (through /users). The RewriteRule I mentioned above does only work if
            you have a site, mode and action parameter, but not with just a site
            or just a site + mode parameter.
            >
            Is there a way to solve this without making a RewriteRule for each
            case (or probably a better solution than mine)?
            >
            >
            Thanks in advance!
            >
            mod_rewrite is an Apache module, not PHP code. You are asking in the
            wrong newsgroup.

            Try alt.apache.conf iguration, for instance.

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • k3pp0

              #7
              Re: (Simple) mod_rewrite question

              mod_rewrite is an Apache module, not PHP code.  You are asking in the
              wrong newsgroup.
              >
              Try alt.apache.conf iguration, for instance.
              >
              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstuck...@attgl obal.net
              =============== ===
              I know, but the apache newsgroup(s) are pretty inactive...

              I would be very thankful if someone would answer me anyway.

              Comment

              • Jerry Stuckle

                #8
                Re: (Simple) mod_rewrite question

                k3pp0 wrote:
                >mod_rewrite is an Apache module, not PHP code. You are asking in the
                >wrong newsgroup.
                >>
                >Try alt.apache.conf iguration, for instance.
                >>
                >--
                >============== ====
                >Remove the "x" from my email address
                >Jerry Stuckle
                >JDS Computer Training Corp.
                >jstuck...@attg lobal.net
                >============== ====
                >
                I know, but the apache newsgroup(s) are pretty inactive...
                >
                I would be very thankful if someone would answer me anyway.
                >
                Ask in the correct newsgroup and you'll get a good answer.

                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstucklex@attgl obal.net
                =============== ===

                Comment

                Working...