Jump to content

Member Profile Page Within Url


kensbeijing

Recommended Posts

I'v seen many websites with this feature but I can't find any tutorials.Sometimes it's easier to link to someones online profile for a website with the use of a URL e.g. www.mysite.com/tomhardy.How do you take that last directory "tomhardy" to link it to a specific profile?

Link to comment
Share on other sites

You should look into mod_rewrite, you can redirect (silently, the page URL for the user doesn't change and there is no page reload - as the name suggests it's more like URL mapping than a redirect but that gives you an idea of how it operates) any such page requests where mysite.com/[foo] doesn't match an existing directory (so it doesn't break existing links) using mod_rewrite in a .htaccess file and have it redirect to member_profile.php?user=$1 which takes the [foo] (which could be "tomhardy") and turn it into the contents of $_GET['user'].In essence the user sees mysite.com/[foo], and the server sees mysite.com/member_profile.php?user=[foo]EDIT: oh, and since the rewrite rules are regular expressions, you can restrict what "[foo]" will be accepted to something like ([a-zA-Z][a-zA-Z0-9]+) which would match any alphanumeric string starting with a letter (the kind of format you might have for a valid username) and would not match "mysite.com/page.php" or similar because they contain a "." character which doesn't match.

Link to comment
Share on other sites

You should look into mod_rewrite, you can redirect (silently, the page URL for the user doesn't change and there is no page reload - as the name suggests it's more like URL mapping than a redirect but that gives you an idea of how it operates) any such page requests where mysite.com/[foo] doesn't match an existing directory (so it doesn't break existing links) using mod_rewrite in a .htaccess file and have it redirect to member_profile.php?user=$1 which takes the [foo] (which could be "tomhardy") and turn it into the contents of $_GET['user'].In essence the user sees mysite.com/[foo], and the server sees mysite.com/member_profile.php?user=[foo]EDIT: oh, and since the rewrite rules are regular expressions, you can restrict what "[foo]" will be accepted to something like ([a-zA-Z][a-zA-Z0-9]+) which would match any alphanumeric string starting with a letter (the kind of format you might have for a valid username) and would not match "mysite.com/page.php" or similar because they contain a "." character which doesn't match.
Alright thanks, but it all seems a bit too much for my novice brain. Does anyone know of any tutorials for this? I know nothing about htaccess files and how they work. Mean while I'll have to do a bit of trial and error.
Link to comment
Share on other sites

I don't think that's possible, as that is a completely different domain. However, the work needed to make that into the URL your scripts need is the same as doing it the original way you asked about. It's not hard. You need to find out if your server is Apache or otherwise, by running a phpinfo script. If it is Apache, you can create a file named .htaccess and in it you will have something like:RewriteEngine On#this turns www.mysite.com/tomhardy into www.mysite.com/profile.php?user=tomhardyRewriteRule ^(.*).com/(.*)$ /$1.com/profile.php?user=$2That is a very basic command that will do it, but it will cause problems for any other pages on your site. Best to have something more unique, like have www.mysite.com/user/tomhardy, which you can rewrite like this:RewriteEngine On#translate mysite.com/user/tomhardyRewriteRule ^(.*)user/(.*)$ /$1profile.php?user=$2Don't try to use a fake directory that has a name similar to the real script, i.e. don't try rewriting mysite.com/profile/tomhardy to mysite.com/profile.php?user=tomhardy because I find that browsers try to resolve that to profile.php/tomhardy which likely doesn't exist, and it gives you a 500 error. If you're not on apache, I don't know how to do it without writing extra php scripts that just perform redirects.

Link to comment
Share on other sites

That's awesome...doesn't go into a lot of detail about implementation. Do you need full access to the server config files to do it? That would look pretty awesome - way more awesome than forward slashes. Who wouldn't feel more important as a user with a whole subdomain apportioned them?

Link to comment
Share on other sites

I found this page, which gives actual mod_rewrite code:sub domain mod_rewritingExcellent!

Link to comment
Share on other sites

I don't think that's possible, as that is a completely different domain. However, the work needed to make that into the URL your scripts need is the same as doing it the original way you asked about. It's not hard. You need to find out if your server is Apache or otherwise, by running a phpinfo script. If it is Apache, you can create a file named .htaccess and in it you will have something like:RewriteEngine On#this turns www.mysite.com/tomhardy into www.mysite.com/profile.php?user=tomhardyRewriteRule ^(.*).com/(.*)$ /$1.com/profile.php?user=$2That is a very basic command that will do it, but it will cause problems for any other pages on your site. Best to have something more unique, like have www.mysite.com/user/tomhardy, which you can rewrite like this:RewriteEngine On#translate mysite.com/user/tomhardyRewriteRule ^(.*)user/(.*)$ /$1profile.php?user=$2Don't try to use a fake directory that has a name similar to the real script, i.e. don't try rewriting mysite.com/profile/tomhardy to mysite.com/profile.php?user=tomhardy because I find that browsers try to resolve that to profile.php/tomhardy which likely doesn't exist, and it gives you a 500 error. If you're not on apache, I don't know how to do it without writing extra php scripts that just perform redirects.
Is it possible to use htaccess, firstly to search if the directory exists e.g. I don't want people using the username "images", because that subfolder exists, then go to the profile page if the subfolder doesn't exist.
Link to comment
Share on other sites

Well, if someone used the name images, you would end up trying to go to the page profiles.php?user=images, which would not conflict with the images folder unless you had an images folder in the 'users' directory. That's why I said not to name your virtual directory the same as another real file, because you will end up with conflicts. So as long as there isn't really a 'users' directory, you'll be fine. If there is, rename one of the two.

Link to comment
Share on other sites

I got it this to work:Options +FollowSymLinksRewriteEngine OnRewriteBase /RewriteRule ^page/([^/\.]+)/?$ portfolio/index.php?page=$1 [R]But the css style sheet doesn't work, so its just boring unorganised text on the page. What's causing the problem?Also is it possible to only change the url server side. It seems to change back to the long complicated url in the address bar.

Link to comment
Share on other sites

You need to prepend a forward slash to your files that use relative addresses, or use absolute addresses. It shouldn't change the URL in the address bar at all, that's the point. You will need to adjust links to meet the format you have described in the .htaccess file or it destroys the illusion. I'm not sure what's causing the old style URLs to appear. Perhaps caching problems?

Link to comment
Share on other sites

You need to prepend a forward slash to your files that use relative addresses, or use absolute addresses. It shouldn't change the URL in the address bar at all, that's the point. You will need to adjust links to meet the format you have described in the .htaccess file or it destroys the illusion. I'm not sure what's causing the old style URLs to appear. Perhaps caching problems?
Ok thanks the css works now, but its still changing to the real url for some reason.Also at the moment i'm using www.mysite.com/portfolio/tomhardy to get to the profile, but this is a bit long winded.What's the best way to accept www.mysite.com/tomhardy ? Is there a way to only redirect if the directory doesn't exist?
Link to comment
Share on other sites

Oh right the [R] made it redirect the whole name. This is my new one:Options +FollowSymLinksRewriteEngine OnRewriteBase /RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1It works except I need a few more things:it will accept www.mysite.com/tomhardy but not www.mysite.com/tomhardy/also how do I deny access to folders like www.mysite.com/images?

Link to comment
Share on other sites

I'm not sure, I haven't tried to do that sort of complex filtering before. It depends on the layout of the rest of your site. You can have a rewrite rule:RewriteRule ^.*mysite.com$ /mysite.com/index.php [L]And then for sites that survive that rule,RewriteRule ^.*mysite.com/([.]+)$ /mysite.com/profile.php?user=$1But, if you wanted to make more complex/non-user related URLs later, you would need to tighten that up so it didn't convert everything after mysite.com/ to profile.php?user=foo. Something like, but I'm not sure and no regex-pert:RewriteRule ^.?mysite.com/([^/]+)$ /mysite.com/profile.php?user=$1That should allow something like mysite.com/foo/bar through, because it doesn't match the pattern...but, that's something to check! It may capture everything except forward slashes.

Link to comment
Share on other sites

alright thanks, got it to work nicely. I tried denying access to my images folder by adding "Deny from all" to an .htaccess file in the images directory, but that meant images from it would not load if its called upon. What's the best way to prevent direct access to subfolders? I'v been thinking of putting an index.php in the folder to make it redirect, but is there a better way?

Link to comment
Share on other sites

Is there not an alternative to 'deny from all' that will deny only from requests that don't originate within the server or something? I really have no experience in this area, I'm afraid. If nobody else comes looking, might be worth a new thread.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...