By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,454 Members | 1,803 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,454 IT Pros & Developers. It's quick & easy.

seeking mod_rewrite solution

P: n/a
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.com/my_profile.php?user_id=fdjkhfh2489298hf298h3s0dhfx j".
I want the users to be able to choose their own web address that would
look like "www.example.com/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.php?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.

Mar 1 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
I'm brand new to mod_rewrite, so any help would be appreciated.


Ask an Apache mailing list. You will get more help...
Mar 1 '06 #2

P: n/a
jo******@gmail.com wrote:
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.com/my_profile.php?user_id=fdjkhfh2489298hf298h3s0dhfx j".
I want the users to be able to choose their own web address that would
look like "www.example.com/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?
Yes, it is.
Reading a little bit about mod_rewrite, I believe the rule I want is
something like the following:

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


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.

Mar 2 '06 #3

P: n/a
jo******@gmail.com wrote:
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.com/my_profile.php?user_id=fdjkhfh2489298hf298h3s0dhfx j".
I want the users to be able to choose their own web address that would
look like "www.example.com/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?
Yes, it is.
Reading a little bit about mod_rewrite, I believe the rule I want is
something like the following:

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


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.

Mar 2 '06 #4

P: n/a
>The pattern ^/\w+\/?/$ probably makes more sense.

Yes, that is better, thank you.
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.


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.

Mar 2 '06 #5

P: n/a
jo******@gmail.com wrote:
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.


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.php, 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.

Mar 2 '06 #6

P: n/a
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).

Mar 2 '06 #7

P: n/a
jo******@gmail.com wrote:
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).


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.

Mar 2 '06 #8

P: n/a
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.

Mar 7 '06 #9

P: n/a
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.

Mar 7 '06 #10

P: n/a
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."?

Mar 7 '06 #11

P: n/a
jo******@gmail.com wrote:
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."?


Joel,

Try an Apache newsgroup, like alt.apache.configuration. 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.
js*******@attglobal.net
==================
Mar 7 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.