seeking mod_rewrite solution

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • joelbyrd@gmail.com

    seeking mod_rewrite solution

    I have a people-networking type site in which each user has their own
    profile page, with their user id encoded. So, for example, the web
    address of their page might look like
    "www.example.co m/my_profile.php? user_id=fdjkhfh 2489298hf298h3s 0dhfxj".
    I want the users to be able to choose their own web address that would
    look like "www.example.co m/Joel", and then when they type in that
    address, they are automatically redirected to their profile page with
    the more complex address.

    I believe mod_rewrite is the solution to this?

    Reading a little bit about mod_rewrite, I believe the rule I want is
    something like the following:

    RewriteRule ^/(.*) /redirect_user.p hp?user_name=$1

    So 2 simple questions:
    1) does this rule look like what I want?
    2) how do I install and hook up the module, and do I write this rule
    in the .htaccess file?

    I'm brand new to mod_rewrite, so any help would be appreciated.

  • F Laupretre

    #2
    Re: seeking mod_rewrite solution

    [color=blue]
    > I'm brand new to mod_rewrite, so any help would be appreciated.[/color]

    Ask an Apache mailing list. You will get more help...

    Comment

    • Chung Leong

      #3
      Re: seeking mod_rewrite solution

      joelbyrd@gmail. com wrote:[color=blue]
      > I have a people-networking type site in which each user has their own
      > profile page, with their user id encoded. So, for example, the web
      > address of their page might look like
      > "www.example.co m/my_profile.php? user_id=fdjkhfh 2489298hf298h3s 0dhfxj".
      > I want the users to be able to choose their own web address that would
      > look like "www.example.co m/Joel", and then when they type in that
      > address, they are automatically redirected to their profile page with
      > the more complex address.
      >
      > I believe mod_rewrite is the solution to this?[/color]

      Yes, it is.
      [color=blue]
      > Reading a little bit about mod_rewrite, I believe the rule I want is
      > something like the following:
      >
      > RewriteRule ^/(.*) /redirect_user.p hp?user_name=$1[/color]

      No, that wouldn't work too well, as it redirect every request to the
      script. In general you want your rewrite rules to be as narrow as you
      can. The pattern ^/\w+\/?/$ probably makes more sense.

      I would also modify my_profile.php so it finds the user based on the
      user name instead of doing a redirect to the page with the user id.
      Don't really see a reason to do it in two steps.

      Comment

      • Chung Leong

        #4
        Re: seeking mod_rewrite solution

        joelbyrd@gmail. com wrote:[color=blue]
        > I have a people-networking type site in which each user has their own
        > profile page, with their user id encoded. So, for example, the web
        > address of their page might look like
        > "www.example.co m/my_profile.php? user_id=fdjkhfh 2489298hf298h3s 0dhfxj".
        > I want the users to be able to choose their own web address that would
        > look like "www.example.co m/Joel", and then when they type in that
        > address, they are automatically redirected to their profile page with
        > the more complex address.
        >
        > I believe mod_rewrite is the solution to this?[/color]

        Yes, it is.
        [color=blue]
        > Reading a little bit about mod_rewrite, I believe the rule I want is
        > something like the following:
        >
        > RewriteRule ^/(.*) /redirect_user.p hp?user_name=$1[/color]

        No, that wouldn't work too well, as it redirect every request to the
        script. In general you want your rewrite rules to be as narrow as you
        can make them. The pattern ^/\w+\/?/$ probably makes more sense.

        I would also modify my_profile.php so it finds the user based on the
        user name instead of doing a redirect to the page with the user id.
        Don't really see a reason to do it in two steps.

        Comment

        • joelbyrd@gmail.com

          #5
          Re: seeking mod_rewrite solution

          >The pattern ^/\w+\/?/$ probably makes more sense.

          Yes, that is better, thank you.
          [color=blue]
          >I would also modify my_profile.php so it finds the user based on the
          >user name instead of doing a redirect to the page with the user id.
          >Don't really see a reason to do it in two steps.[/color]

          I think you might have misunderstood what the my_profile.php page is.
          It does not
          redirect, it _is_ the user profile page, but based on the user_id as
          given in the query
          string (which is encoded in the query string, so decoded on the page),
          it can get all
          the information about the user from the database - it's a simple
          database-driven page.

          Comment

          • Chung Leong

            #6
            Re: seeking mod_rewrite solution

            joelbyrd@gmail. com wrote:[color=blue]
            > I think you might have misunderstood what the my_profile.php page is.
            > It does not
            > redirect, it _is_ the user profile page, but based on the user_id as
            > given in the query
            > string (which is encoded in the query string, so decoded on the page),
            > it can get all
            > the information about the user from the database - it's a simple
            > database-driven page.[/color]

            I understood you just fine. What you are planning to do is to find the
            user id based on the given user name in redirect_user.p hp, then
            redirect to my_profile.php, which look up the user record using the id.
            What I suggested was to look up the user record using the user name in
            my_profile.php. There is no point in doing a redirect to the page when
            you can rewrite directly to it.

            Comment

            • joelbyrd@gmail.com

              #7
              Re: seeking mod_rewrite solution

              Oh! Duh! Yes, of course - that makes perfect sense. I guess the only
              reason I was thinking about using an intermediary redirect page was to
              do any other pre-processing that might be necessary. For example, if
              the "name" is "admin", then I would have the redirect page redirect the
              user to the actual admin folder since in this case, this is not a
              user's profile page, but an administrator trying to access the backend.
              But, I guess this should probably be done using a RewriteCond?
              Something like:

              RewriteCond %{REQUEST_URI} !^admin.*$

              Or something like that (I'm not too familiar with the syntax, although
              I am somewhat familiar with regular expressions).

              Comment

              • Chung Leong

                #8
                Re: seeking mod_rewrite solution

                joelbyrd@gmail. com wrote:[color=blue]
                > Oh! Duh! Yes, of course - that makes perfect sense. I guess the only
                > reason I was thinking about using an intermediary redirect page was to
                > do any other pre-processing that might be necessary. For example, if
                > the "name" is "admin", then I would have the redirect page redirect the
                > user to the actual admin folder since in this case, this is not a
                > user's profile page, but an administrator trying to access the backend.
                > But, I guess this should probably be done using a RewriteCond?
                > Something like:
                >
                > RewriteCond %{REQUEST_URI} !^admin.*$
                >
                > Or something like that (I'm not too familiar with the syntax, although
                > I am somewhat familiar with regular expressions).[/color]

                It's easier to handle this special case by putting another rewrite rule
                ahead of the one dealing with the profile page.

                RewriteRule ^/admin/?$ /admin_page.php [L]
                RewriteRule ...

                The [L] flag tells mod_rewrite to not apply the subsequent rules.

                Comment

                • joelbyrd@gmail.com

                  #9
                  Re: seeking mod_rewrite solution

                  Alright, I think I've got everything squared away, but now for the
                  potentially annoying part - how do I run the thing? Is it simply a
                  matter of including

                  RewriteEngine On
                  RewriteRule ^/admin\/?/$ /admin/index.php
                  RewriteRule ^/\w+\/?/$ /my_profile.php? user_name=$1

                  in the .htaccess file? Or do I need to include more in the .htaccess
                  file, or does this code have to go somewhere else besides the .htaccess
                  file? Is the mod_rewrite module already built in (I think it is)?
                  I've been trying to read about the mod_rewrite module on
                  http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html, but I'm still
                  confused as to how to get the thing actually working. Ideally, the
                  complexity of a task should somewhat reflect the complexity of
                  implementing that task, but I don't know if that's the case here.

                  Comment

                  • joelbyrd@gmail.com

                    #10
                    Re: seeking mod_rewrite solution

                    Alright, I think I've got everything squared away, but now for the
                    potentially annoying part - how do I run the thing? Is it simply a
                    matter of including

                    RewriteEngine On
                    RewriteRule ^/admin\/?/$ /admin/index.php [L]
                    RewriteRule ^/\w+\/?/$ /my_profile.php? user_name=$1


                    in the .htaccess file? (because I tried that and it didn't work) Or
                    do I need to include more in the .htaccess
                    file, or does this code have to go somewhere else besides the .htaccess

                    file? Is the mod_rewrite module already built in (I think it is)?
                    I've been trying to read about the mod_rewrite module on
                    http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html, but I'm still
                    confused as to how to get the thing actually working. Ideally, the
                    complexity of a task should somewhat reflect the complexity of
                    implementing that task, but I don't know if that's the case here.

                    Comment

                    • joelbyrd@gmail.com

                      #11
                      Re: seeking mod_rewrite solution

                      Ok, I'm beginning to understand the mod_rewrite module better. I
                      discovered the following important information. The RewriteEngine and
                      RewriteRule directives (which I will be using, possibly along with the
                      RewriteBase directive) are "Extensions ". And the Apache server
                      documentation says this about "Extensions ":

                      A directive with "Extension" status is provided by one of the modules
                      included with the Apache server kit, but the module isn't normally
                      compiled into the server. To enable the directive and its
                      functionality, you will need to change the server build configuration
                      files and re-compile Apache.

                      So, does anyone know how I would "change the server build configuration
                      files and re-compile Apache."?

                      Comment

                      • Jerry Stuckle

                        #12
                        Re: seeking mod_rewrite solution

                        joelbyrd@gmail. com wrote:[color=blue]
                        > Ok, I'm beginning to understand the mod_rewrite module better. I
                        > discovered the following important information. The RewriteEngine and
                        > RewriteRule directives (which I will be using, possibly along with the
                        > RewriteBase directive) are "Extensions ". And the Apache server
                        > documentation says this about "Extensions ":
                        >
                        > A directive with "Extension" status is provided by one of the modules
                        > included with the Apache server kit, but the module isn't normally
                        > compiled into the server. To enable the directive and its
                        > functionality, you will need to change the server build configuration
                        > files and re-compile Apache.
                        >
                        > So, does anyone know how I would "change the server build configuration
                        > files and re-compile Apache."?
                        >[/color]

                        Joel,

                        Try an Apache newsgroup, like alt.apache.conf iguration. You don't have
                        to actually recompile Apache - just configure it correctly.

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

                        Comment

                        Working...