Google Answers Logo
View Question
 
Q: mod_rewrite help ( No Answer,   3 Comments )
Question  
Subject: mod_rewrite help
Category: Computers > Internet
Asked by: webguy123-ga
List Price: $20.00
Posted: 30 Aug 2005 16:05 PDT
Expires: 29 Sep 2005 16:05 PDT
Question ID: 562386
I have a coldfusion application and I am running Apache 2.0. I need help
writing a rule:

I have the following URL:

http://localhost/profile.cfm?id=55614789

(the "localhost" will eventually be a domain name)

What this does is load a specific persons page depending on the URL.ID
variable passed in the browser (which I lookup from the database once
the profile.cfm page is ran.)

I need to allow a user's AccountName to load the page as well. For example:

If someone enters "http://localhost/smith88" in the browser, I need it
to lookup their unique AccountName and AccountID in the database and
then really load "http://localhost/profile.cfm?id=55614789"

Request for Question Clarification by palitoy-ga on 31 Aug 2005 01:05 PDT
Hello webguy123-ga

The only way I can think of doing this is to redirect the
http://localhost/smith88 request to another page that performs the
database lookup which then redirects the user again.

Would information on how to perform this operation be acceptable as an answer?

palitoy-ga

Clarification of Question by webguy123-ga on 01 Sep 2005 17:52 PDT
hello palitoy-ga,

Thanks for your reply. I know I could do it this way but I am trying
to avoid that. I am looking for a on-the-fly solution as much as
possible.

Thanks

Request for Question Clarification by palitoy-ga on 01 Sep 2005 23:32 PDT
No problem.  nozen-ga's suggestion is a good one and something I also
thought of but it involves the production of a separate file with all
the usernames in it.  I don't like duplicating the data unless I have
to!  The information nozen-ga explained can be read about in the
mod_rewrite manual:
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteMap

Would you like me to provide you with this as an answer?

palitoy-ga
Answer  
There is no answer at this time.

Comments  
Subject: Re: mod_rewrite help
From: nozen-ga on 31 Aug 2005 14:06 PDT
 
Step 1: create a file with your AccountName -> ID list in it.  E.g.:

smith88   55614789
webguy123 11276534
... etc

Ensure that permissions are such that your Apache process can read it.
I'll assume it's in /usr/local/apache2/conf/username.txt for the following.

Step 2: Configure rewriting in the httpd.conf file.  Here's a really
basic version; this would go at the end of your existing httpd.conf
file:

# Turn on rewriting
RewriteEngine  on
# Define the text database that maps usernames to IDs
RewriteMap     usernames txt:/usr/local/apache2/conf/usernames.txt
# Match /<something> - that's the /(.*)$ part
# Replace it with profile.cfm?id=
# Plus the result of looking up the <something> in the usernames.txt file
RewriteRule    ^/(.*)$       profile.cfm?id=${usernames:$1|NOTFOUND}
# The |NOTFOUND above is a hack; if we see that string in the URL we should
# probably load some sort of "no such user" page...
RewriteCond    ^.*NOTFOUND$  /notfound.html [L]

In the long run you will probably want to store the database in a DBM
file rather than as plaintext since the lookups will be faster - not
an issue for 10 names but definitely for 10,000!  You may well also
want some sort of RewriteCond above the first RewriteRule to identify
pages or directories that aren't supposed to be rewritten.

I would recommend against a redirect.  It will look "cleaner" and be
more search engine friendly if the user loads
http://example.com/username and the page loads directly (as this
example should do), rather than redirecting them to
http://example.com/profile.cfm?731892.

NoZen
Subject: Re: mod_rewrite help
From: webguy123-ga on 31 Aug 2005 18:33 PDT
 
Ok, so I can create some coldfusion code to append the username and userid 
to a text file everytime a new account is created. 

But, could your write out the mod_rewrite rule so I could try it? I
understand very little on how to construct one myself.

Thanks
Subject: Re: mod_rewrite help
From: idcdiscount-ga on 02 Sep 2005 06:06 PDT
 
It depends on what characters may contain urls at first level pages -
http://www.domain.tld/(1level)/
Because if user account may contain dots you will receive an endless
cycle with RewriteRule ^([^/]+)(/.*)? profile.cfm?id=$1
If your urls are like domain/file.ext this will work...

For examle, look at http://www.idc-maxico.ru/
If you try to access a page http://www.idc-maxico.ru/catalog/company-555/
and there is no such a company at mysql db, you will get a message.

So you should do something like this:
.htaccess
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ profile.cfm?id=$1

And a check in your script (it's PHP+MySQL):
mysql_query("SELECT * FROM users WHERE id='".mysql_escape_string($_GET['id'])."'");
if(!mysql_affected_rows())
	{
	header ("Location: /nouserfound.cfm");
	exit;
	}

About RewriteMap... It may take a really long time itself and plus map
generation from db...

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy