Helping ordinary people create extraordinary websites!

Go Back   Web Development Forum > Website Management > Search Engine Optimization
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-14-2008, 06:48 PM
Junior Member
 
Join Date: Jan 2008
Posts: 3
Default How do I make my URLs better for SEO?

I use PHP for my site, so my URLs look like this:
mysite.com/index.php?page=videos
mysite.com/index.php?page=music
and so on. I am working on making my site more SEO friendly, and want to have URLs like mysite.com/videos - I've read that this is possible without actually having the directories in that structure, but I'm not sure how to go about it. Anyone enlighten me? Thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-14-2008, 07:08 PM
BigAlReturns's Avatar
Moderator Extraordinaire!
 
Join Date: Dec 2007
Location: The Wirral, England
Posts: 291
Send a message via MSN to BigAlReturns
Default

Hi there, welcome to the forums! Good question (and err interesting name as well!!)
You are right, you can indeed change your URLs without physically changing the location of your pages - as you say this has some SEO benefit, by increasing keyword density in the URL, but also a massive usability benefit. Consider pages like profile.php?id=1243438729. I'm never going to remember that that's where my profile is, but on the other hand, site.com/profile/bigalreturns is no problem to remember. Also, it's logical how I would go to another user's profile.
In your case, I wouldn't expect massive SEO benefits, as you already have the page keyword in the URL, expect a bigger benefit when changing, for example, IDs to names. Even so, my policy on SEO is every little helps, and perhaps you can apply this technique to other pages/sections of your site.
The technique we're going to use is called mod_rewrite, and it's a feature of Apache servers - I assume that as you're using PHP then this is what your server runs on. Microsoft IIS server has an equivalent, but let's not worry about that - you're very unlikely to be on an IIS server unless you specifically looked to get on one.
So what do we do - we need to edit/create a file in the root directory of the site, called .htaccess - thats no actual filename, just the extension .htaccess. It probably already contains some information, leave this as it is and start a line or so below this with the code. Firstly, we need to turn on the mod_rewrite feature, which is done with these two lines:
Code:
Options +FollowSymLinks
RewriteEngine on
After this, what we are going to use is a regular expression, which Apache will match against the typed in URL, and if it matches, apply the rule, and show the content from the page we specify. Regex is a complex subject, and I can't explain it here, but do some reading around, and try and adapt something to your needs. You can always come and post back here if you're struggling!
So in this case, the URLs we want to match are of the following structure:
mysite.com/xxxxxxx
The xs are the part that's important, as that's what determines which pages is shown. The regular expression to match this is as follows, it starts with ^ and ends with $, these are just to show the start and end of the expression.
Code:
^/?([a-zA-Z0-9])/?$
A little explanation of what's going on. The [square brackets] represent character sets, in this case we want to include a-z, A-Z and 0-9, i.e. all upper and lower case letters, and all numbers. The (round brackets) group things together - in this case we're grouping the bit typed in by the user, so we can use it later. Question marks mean the preceding string may or may not be there. We use it on the leading slash because different Apache versions may or may not use this in the matching string. The trailing slash is optional, because the user may or may not decide to type it in.
The clever part is now - each set of brackets is then passed to the next part of the string. The first set of bracket's content is represented by $1, the second set by $2 and so on. The order is by opening brackets by the way - can get confusing. So we can now write the second part of our rule, which tells Apache where the page actually is:
Code:
index.php?page=$1
So if we type in page.com/music/, apache recognises the /music/ as conforming to our rule, sends "music" as $1 to the second part, and fetches the page index.php?page=music.
We have all we need to create our rule now, we just need to define it as a rule by starting the line with RewriteRule. The order of parts to the rule is shown in the complete code below:
Code:
Options +FollowSymLinks
RewriteEngine on

RewriteRule ^/?([a-zA-Z0-9])/?$ index.php?page=$1
So now we can (hopefully) type in mysite.com/music, and while that address stays in the address bar, the content shown is actually from mysite.com/index.php?page=music.
mod_rewrite is a very powerful tool, so do some more reading about it, experiment, and come back here with any problems! Good luck!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-14-2008, 08:23 PM
Junior Member
 
Join Date: Jan 2008
Posts: 3
Default

Thanks bigalreturns, I tried your code out and it seems to be working for me, but I have one small problem - some of my URLs are being redirected when I don't want them to be. For example, I type in mysite.com/aboutme.php - this is a static page, but it's being redirected to index.php?page=aboutme.php. While I'm learning I want to keep this page static for the moment, so is there any way to exclude it from being redirected?

PS The name is because I'm a vet student, so I'm always getting asked if I put my hand up cows backsides!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-14-2008, 08:28 PM
BigAlReturns's Avatar
Moderator Extraordinaire!
 
Join Date: Dec 2007
Location: The Wirral, England
Posts: 291
Send a message via MSN to BigAlReturns
Default

Glad you got it at least partially working! We can exclude certain names from being matched, but what might be better is if we limited the matching to certain names that we do want to match. This is better coding all round, as it means there won't be any conflicts like the one you described. I'll show you the code as to how I'd go about it, then give you some explanation.
Code:
Options +FollowSymLinks
RewriteEngine on

RewriteRule ^/?(music|videos|whateverelse)/?$ index.php?page=$1
As you can see it's pretty similar to before - all that's changed is the regular expression we've used to match the typed in URL. The key operator here is the pipe | symbol. This simply means OR. So the part in the brackets has to exactly match "music" "videos" or "whateverelse". Only if it exactly matches one of these will the URL be rewritten according to the rules. As before, whatever is contained within the brackets will be passed into the second part of the rule.
So what you need to do is modify the bracketed part to contain only the words that you want to be matched, separating them with the |. Then it should work as planned, without rewriting pages that you want to remain static.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-14-2008, 08:33 PM
BigAlReturns's Avatar
Moderator Extraordinaire!
 
Join Date: Dec 2007
Location: The Wirral, England
Posts: 291
Send a message via MSN to BigAlReturns
Default

Hopefully I've sparked your interest enough in this that you might want to do some reading on. I just found a great site that lists all regex syntax - http://www.niederlandistik.fu-berlin...bin/regex.html - which is a handy resource. It doesn't explain the principles behind how to use it, but once you've got the jist it should come easily.
Also, regex isn't just for mod_rewrite, it has far reaching functions, and can be used in most programming languages. It really is a powerful tool worth learning how to use.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-15-2008, 11:32 AM
Junior Member
 
Join Date: Jan 2008
Posts: 3
Default

Thanks BigAl, that's working really nicely now!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 02:10 AM.


Website Design by Ducani Media Group
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.