February 6, 2009 – 11:58 am by
Mark
They’re easy!
RateAFace, like alot of sites, offer easy to remember user urls.
http://www.rateaface.com/Mark is tidy, user friendly and search engine friendly. But how is it done?
If you’re thinking some sort of 404 redirect (like I did at first a couple of years ago), then you’re wrong. Apache (Apache web server version 1.2+) has a very sexy url rewrite rule - which comes as standard. All you need to do is set up a .htaccess file to tell your server what to do with certain URLs!
.htaccess
To set up your .htaccess file simply create a blank file (in notepad or similar) and save it as some temporary name. When you upload it rename it to .htaccess - the dot in front is important, as it makes it a hidden file on unix systems.
The content of your .htaccess file can vary based one what you want it to do.
FOR EXAMPLE, turn it into a 404 command by having it say
ErrorDocument 404 /location/of/404.html. But obviously this is not what we want - this is a redirect, not a rewrite.
RateAFace’s user "redirect" (it don’t actually redirect your browser, but y’kno) works from a htaccess file with the following data:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9_-]+)$
RewriteRule ^(.*)$ user.php [L]
</IfModule>
Let’s go though this.
<IfModule mod_rewrite.c>
RewriteEngine on
Tells Apache to load the mod_rewrite stuff.
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9_-]+)$
RewriteRule ^(.*)$ user.php [L]
The base/root of the rewrite.
Followed by condition of the rewrite - as in what triggers a url rewrite.
Then followed by the rule.
So when htaccess sees a url that fits the condition, it runs the rule.
%{REQUEST_URI} ^/([a-zA-Z0-9_-]+)$ refers to any url which contains letters, numbers and - or _ (allowed chars for user urls)
^(.*)$ user.php [L] tells it to go to user.php.
The [L] is a mod_rewite Flag. L for Last, meaning the data of the rule is not checked. - Basically, going to user.php will result in a "user not found" instead of loading the page! (We have profile.php to manually load a profile.) Pretty nifty, yo.
And that’s it! Well, of course there’s more - inside user.php, which takes the REQUEST_URI and works out what to do with it. Either load a user profile or throw back a 404, etc.
I found a good article about this on Sitepoint as I was writing this, http://www.sitepoint.com/article/guide-url-rewriting/ take a look if this is what you’re after!
Posted in System, Technical |