Welcome to WebmasterWorld Guest from 3.81.29.226

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

mod rewite / query string

     
1:13 pm on Feb 18, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:June 8, 2008
posts:158
votes: 0


Hope someone can help, I'm at my wits end! (which doesn't take much!)

I pull some basic data from a mysql database and list it on a page, on the page I've added the links with the variable user_id:

<a href=\"http://localhost/TESTING/template.php?user_id=$user_id\">click</a>

On the template.php file I'll get the $user_id and display the relevant full data.

The thing is I don't want user_id=249 [localhost...] to show in the url I'd like something like "http://localhost/TESTING/template.php/249".

I've been looking into this for a few days now with no success, is this possible?

Or does the template file need the user_id=$user_id to be displayed in order to get the variable?

Thanks!
5:05 pm on Feb 18, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


On your pages, link to the URL you want the visitors and search engines to see. Links on pages define URLs.

Then use Apache mod_rewrite to 'map' requests for that URL to the template.php filepath inside your server, and to move the "249" from the URL into your query string. This requires only one or two lines of code.

See the thread "Changing Dynamic URLs to Static URLs [webmasterworld.com]" in our Apache Forum Library. Some very good references are cited in our Apache Forum Charter.

Please post specific questions back here.

Jim
9:10 pm on Feb 18, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:June 8, 2008
posts:158
votes: 0


Thanks for the link.

If it helps anyone, I got what i needed to work by doing the following:
RewriteEngine On
RewriteBase /TESTING/ ##my root testing folder on localhost
RewriteRule ^template.php/([0-9]+)(/)?$ template.php?user_id=$1 [L]

Changed the link to:
href="localhost/TESTING/template.php/{$user_id}"

which passes the $user_id to my template.php file for processing without having the variable name show up in the url.
11:28 pm on Feb 18, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


The only problem I see is subtle, but it's important: Allow no variation in the URL to be rewritten to your script. If someone tacks a trailing slash on the request, then detect that first and 301-redirect to the URL without the trailing slash.

If you do that, then it won't matter, that the parentheses around that slash-pattern are not needed... :)

Otherwise, by allowing your script to be accessed by two different URLs (with trailing slash, and sans), you create duplicate-content vulnerability which can be exploited...

Finally, once you've changed all of your old dynamic links to static links, you'll want to redirect any clients requesting the old dynamic URLs (which are now used only as internal filepaths).

# Externally redirect to remove trailing slash
RewriteRule ^template.php/([0-9]+)/$ http://localhost/template.php/$1 [R=301,L]
#
# Externally redirect direct client requests for old dynamic URLs to static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /template\.php\?user_id=([0-9]+)\ HTTP/
RewriteRule ^template\.php$ http://localhost/template.php/%1 [R=301,L]
#
# Internally rewrite template.php/<user-id> requests to template.php?user_id=<user-id>
RewriteRule ^template.php/([0-9]+)$ template.php?user_id=$1 [L]

I should also point out that there's no need to link to "/template.php/<user-id>". You could just as well link to "/user/<user-id>" or "/sponsor/<user-id>" or anything else that's shorter, more memorable, more attractive (in your opinion), or all of these. No-one but the bad guys care that the script filetype is probably .php, and there's no reason to publish the fact...

Jim
9:17 am on Feb 19, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:June 8, 2008
posts:158
votes: 0


Cheers Jim, that's simple awesome!

I would have completely missed the duplicate content thing with that trailing slash.

And yes, I'll be changing the template.php in the link to something more descriptive.
I can barely get my head around this mod_rewite stuff but it's starting to become clearer to me now.

I feel like I've joined the magic circle or the freemasons by learning these dark arts.

Thanks again!
4:22 pm on Feb 19, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


If you make a firm distinction between "URLs and links used out on the Web" and "Files and filepaths used inside this server," then all becomes much simpler and clearer; the magic seems less intense, and the art not quite so dark.

This distinction is absolutely the key to understanding URL-to-filepath rewriting and URL-to-URL redirection, the two main (and quite distinct) functions of mod_rewrite. Sadly, it is missed by most.

Jim