"Pseudo directory" urls

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I've got a very basic newbie question, I hope you can understand me.
Sorry for asking it, first of all.

I see a lot of sites (e.g. communities) with a url-structure like
this: example.com/users/paul (displaying the user profile of "paul")
or example.com/pictures/landscapes/13 (displaying pictures from the
category "landscapes", page 13). These are just examples, I hope I
didn't confuse you.

Well, in my sites I call sites with GET parameters. For example:
myexample.com/index.php?site=users&user=paul or myexample.com/
index.php?site=pictures&cat=landscapes&page=13. In my index.php file I
check for those parameters and include the relevant php file.

For me, my method somehow looks unprofessional compared to those
"pseudo urls" described at the beginning. I'm calling them pseudo urls/
directories, because I don't think there is a physical directory for
every user, for example. If a website has over 10.000 users, I don't
think they have a directory for every single user, so I guess there's
another way to get those nice-looking structure.

How to do that?

Thanks in advance.

Re: "Pseudo directory" urls

Quoted text here. Click to load it

Google for 'apache mod_rewrite'.
In case of questions, look for a forum dedicated to mod_rewrite (there a=
re  =

lots of them), or ask assistance in alt.apache.configuration (as this ha=
s  =

nothing to do with PHP, but rather the webserver).

If you're not using Apache as a webserver, ask in a group dedicated to  =

your webserver how to accomplish this.
-- =

Rik Wasmus

Re: "Pseudo directory" urls


Quoted text here. Click to load it

The most common way to do that is called URL rewriting. On Apache
servers this can be done with the mod_rewrite module.

Apache Module mod_rewrite

URL Rewriting Guide

Another way would be to redirect all requests to the index.php and let
the script parse the request URL. This is what I do in my own framework,
because it makes it a lot easier for me to add new functions or modules
to a website without having to modify any rewrite rule in the .htaccess.


Re: "Pseudo directory" urls

Quoted text here. Click to load it

How do you do that ("redirect all requests to the index.php and let
Quoted text here. Click to load it

Re: "Pseudo directory" urls


Quoted text here. Click to load it

I also do it with mod_rewrite with some additional rules:

RewriteEngine On
RewriteBase /

# remove leading www. from hostname
RewriteCond % ^www\.(.*)$
RewriteRule (.*) http://%1/$1 [R=301,L]

# exclude static URIs from rewriting
RewriteRule ^(css|download|images|scripts)(/|$) - [L]

# redirect internally to application script
RewriteRule .* index.php [QSA]

The last rule will internally redirect all requests to my front
controller. Internally means that this only happens inside the web
server, it won't send a redirect back to the browser, so the browser's
address bar won't be affected.

The script then just takes a look at $_SERVER['REQUEST_URI']. If the
requested URI starts with "/user" for example, it would call a method to
handle such a user request, "/admin" would trigger another method for
handling an administration request and so on. It's more flexible than a
pure mod_rewrite solution, but also more complicated, though.


Re: "Pseudo directory" urls

Quoted text here. Click to load it

I was stuck on the same problem, but now that I've written a .htaccess
file with your example everything works fine, thanks :-)

Re: "Pseudo directory" urls

k3pp0 wrote:
Quoted text here. Click to load it

Actually, they may very well have separate directories for each user, at
least if they allow file uploads.  What happens if everything's in one
directory and two people upload "mybio.html", for instance?  One would
overlay the other.

Sure, you could use mod_rewrite and redirect the requests, but you still
have to handle the filename collisions.

But most sites won't have 10K users on one server.  If they only have
500, for instance, it's quite easy for the file system to handle 500
directories.  It's much cleaner and doesn't require constant updating of
the rewrite rules.

And if they do have 10K users, chances are those users are spread across
multiple servers.  In that case there might be some routing done just to
get to the correct server, but then separate directories for each user.
   Then they would have to update rewrite rules, but only when they get
a new user (or drop one).

As to how you can do it, redirecting via mod_rewrite allows you to use
your current code but have the desired effect with URL's.

The difference is that you're controlling your entire content with your
one URL.  Other sites which don't allow file uploads probably rewrite
the URL and pull everything (or almost everything) from a database.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Site Timeline