In this blog post I will be explaining how it is possible to re-write urls on an apache web server using your .htaccess file. This allows you to use ‘pretty’ links such as http://www.example.org/category/item.html in place of ‘ugly’ links such as http://www.example.org/catid=23&itemid=54
Aside from the fact that the pretty links obviously look better, they can help users navigate the site and are more search engine friendly. They provide keywords for the pages and can also provide a site hierarchy.
Some users navigate the site by removing sections of the URL, for example:
http://www.example.org/category/item.html -> http://www.example.org/category/
In a well designed site using URL re-writing this should not be a problem.
The Tutorial:
To get started, the below line should be added above the rewrite rules in your .htaccess file (if it is not there already) .
We will assume that the sites URL’s are arranged as so:
http://www.example.org/index.php?pageid=x
http://www.example.org/index.php?catid=x
http://www.example.org/index.php?catid=x&itemid=y
In the sites database a field should be added to hold a ‘slug’ – unique URL friendly title – for each page/category. The site linking needs to be changed so that the x/y above contains the slug e.g.
as well as the link
http://www.example.org/index.php?pageid=2
the following can be used:
http://www.example.org/index.php?slug=about-us
the argument in the url does not have to be slug, it could be anything e.g.
http://www.example.org/index.php?title=about-us
and so we have urls such as these:
http://www.example.org/index.php?slug=x
http://www.example.org/index.php?catslug=x
http://www.example.org/index.php?catslug=x&itemslug=y
Rewrite rules like these should then need to be added to the .htaccess file
RewriteRule ^([0-9A-Za-z-]+)/$ index.php?catslug =$1 [L,QSA]
RewriteRule ^([0-9A-Za-z-]+)/([0-9A-Za-z-]+).html$ index.php?catslug =$1&itemslug=$2 [L,QSA]
In the
regular expression, the brackets
select the regular expression between them.
allows all alphanumeric characters and hyphens
we could add underscores
like this
for example.
The plus after the square bracket expression means 1 or more instances.
So
looks for and selects a group of one or more alphanumeric characters/hyphens. The regular expression selects everything between the previous and next part of the link. So
Would select ‘abcdef’ from the url ‘http://www.example.org/abcdef.html’
This is just a very simple guide to regular expression, look out for an upcoming post explaining regular expressions in more detail.
The regular expression selection is referenced using
, for each subsequent regular expression the number is incremented.
i.e.
These above rules will then take a link such as
http://www.example.org/index.php?slug=about-us
and show
http://www.example.org/about-us.html
and
http://www.example.org/index.php?catslug=urls
and show
http://www.example.org/urls/
and
http://www.example.org/index.php?catslug=urls&itemslug=re-writing
and show
http://www.example.org/urls/re-write.html
The linking within the site should therefore be using the links of the format
http://www.example.org/urls/re-write.html
Each page should also automatically generate a canonical tag of the format
at the top, therefore if any search engine spiders visit the existing links of the format http://www.example.org/index.php?catslug=urls&itemslug=re-writing they will know about the new link i.e. http://www.example.org/urls/re-write.html
and change the cached url to the new one.
Another example of re-writing
http://www.example.org/index.php?pageslug=x
http://www.example.org/index.php?catslug=x
http://www.example.org/index.php?itemslug=x
RewriteRule ^category/([0-9A-Za-z-]+).html$ index.php?catslug =$1 [L,QSA]
RewriteRule ^item/([0-9A-Za-z-]+).html$ index.php?itemslug=$1 [L,QSA]
Will give urls such as
http://www.example.org/x.html
http://www.example.org/category/x.html
http://www.example.org/item/x.html
Discuss this blog in The Forum
Related posts:
- Search Engine Friendly URLs (Apache and PHP)
The concept is fairly simple. Make your website’s urls as readable as possible. If a human can read and remember the url, then you can... - Canonicalisation and SEO
In an SEO campaign you want as much authority to go to your homepage as possible. You submit your site to directories, write articles, post... - Dynamic content rules
If your site has any e-commerce functions, dynamic content management systems or user-specific presentation, you will be familiar with dynamic content. Dynamic content was once... - Are your URLs friendly?
The URLs for your pages may seem like a jumble of words, letters, numbers and symbols, but their construction can have a significant impact on... - URL Rewrites and Redirects
Rewriting your websites URLs is an important and proven method for successful SEO. There are four core reasons for this, they maybe staring you in...
Tags: canonical urls, Canonicalisation, Keywords, link rewriting, seo urls
Link to us
If you want to link to this blog, copy and paste the following HTML code to your website.

(31 votes, average: 4.87 out of 5)






