Easy (???) question...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Panzram
Forum Newbie
Posts: 3
Joined: Thu Mar 06, 2008 9:04 am

Easy (???) question...

Post by Panzram »

I want users to be able to access their own web-pages with URLs http://www.mysite.com/usersite1, http://www.mysite.com/usersite2 etc, without having folders usersite1, usersite2 etc. Is this possible? And, if so, how?

All of their content and design is stored in a database, so there's no real need for folders. It will only create problems for me as admin.

Thanks.
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: Easy (???) question...

Post by Sekka »

This is very doable, but a bit of work is required. I refer to this technique as 'virtual' URLs. F**k if I know what the real name is though.

Anyway, in the root of your website, you need to add to the (or create a) .htaccess file the following code,

Code: Select all

RewriteEngine on
 
# Fix requests without last slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule (.*) /$1/ [R=301,L]
 
# 'Virtual' URL
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]
What does this do? Any request that is not a file, e.g. /folder/file.jpg, or existing folder, will be forwarded internally to index.php in the root.

When I said internally, it means just that. The user will still see /my/request/ in their address bar, but the server is now working on /index.php.

You need to then figure out what URL the user was trying to access. You can do this using a server variable. $_SERVER['REQUEST_URI'] gives the URL the user requested (aka what is in their address bar). You can use this to determine what URL they wanted, and as such, display the relevant page.

All that make sense?
Panzram
Forum Newbie
Posts: 3
Joined: Thu Mar 06, 2008 9:04 am

Re: Easy (???) question...

Post by Panzram »

Excellent answer and so to the point! Thanks a bunch.
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: Easy (???) question...

Post by kryles »

I'm sure you'll get this from a Moderator eventually, but you have to make your titles a little more specific to the problem.
Panzram
Forum Newbie
Posts: 3
Joined: Thu Mar 06, 2008 9:04 am

Re: Easy (???) question...

Post by Panzram »

I am aware of it, but it was hard to make my question into specific one-liner. How would you have formulated the title?
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: Easy (???) question...

Post by kryles »

how to enable users to view http://...uni1 and http://uni2 without having folders for them

Horrible description, but yah lol.
Post Reply