Writing friendly URLs with .htaccess INFORMATION ONLY
5 posts by 4 authors in: Forums > CMS Builder
Last Post: February 26, 2013 (RSS)
By zaba - February 2, 2013 - edited: February 27, 2014
Hi Everyone, its a little off topic, but its something I have been meaning to get my head round. After 6 hours struggling with the concept yesterday and reading about a 1000 forum posts on the subject It all finally clicked when I discovered the easy way.
Your links have to be in a digestible format that .htaccess can break down. for example instead of using the default
which generates your links (using the fields you have specified in the editor (in the following example product_name) then -record number when it has been parsed)
for example if the _link url when parsed is this
http://www.yoursite.com/products.php?ActualProductName-1
and you want to re-write it to
http://www.yoursite.com/products/ActualProductName/1
firstly, you will have to expand the link into a usable form that includes variables that htaccess can easily rewrite, so the above _link would be
http://www.yoursite.com/products.php?product_name=xxx&num=y
Then you have to generate your .htaccess to make sure the urls are rewritten, heres the hard part, but fortunately after wasting hours and hours on this I found an online tool that will do this tricky bit for you and generate the code for your .htaccess file.
its excellent, and very easy to understand.
http://www.generateit.net/mod-rewrite/
Using the tool you will discover how easy it is to make this change.
you copy the generated code into a text file and call it .htaccess. With your FTP tool upload this to the root directory of your website. UPLOAD IN ASCII FORMAT! not binary, most FTP will auto detect. NOTE saving the file locally as .htaccess will cause the file to disappear on mac (it's not disappeared it is hidden as files starting with . are reserved for system. instead call it rewrite.htaccess and remove the prefix after uploading.
Finally you need to go through your code and changed the instances of _link to the new format.
/products/<?php echo ['product_name'] ?>/<?php echo ['num'] ?>
Don't get confused what htaccess is actually doing is rewriting the above format into the format that you converted it from, so that your application continues to work.
Also it may be as well to add a canonical link tag to your page in the This tells google that this is the file to be indexed and not the expanded versions with variables. Google hates duplicate content. Update your sitemap.xml.
If you are doing this to an old site that may contain hundreds of pages already indexed by google you may be as well adding 301 redirects to your htaccess file there's tools out there to help you with this, Greg has posted one below. Here's how to find out what google has indexed and what you need to redirect type site:www.yoursite.com into google search box.
i have severely edited this post to make it clearer. Hope you like it.
I really hope this has been useful to you and its written in an easy to follow fashion. Please post back if you have any comments or better ideas. And thanks to IT for the best cms on the planet by a huge margin.
I'll post back if i spot any errors.
--------- update /02/2014
A good and detailed explanation can be found here:
Hi,
Great tutorial!
This is another good htaccess rule generating site that I've used on projects:
http://www.webconfs.com/htaccess-redirect-generator.php
Thanks
Greg
PHP Programmer - interactivetools.com
By gversion - February 4, 2013
Hi Zaba,
This is a huge help! Thank you for taking the time to share this with the community.
Regards,
Greg
By zaba - February 26, 2013
Ok, the great minds at CMSB have released what looks to be a fantastic add-on (or plug-in) called PERMALINKS, this essentially takes the headache out of doing this task and does it directly from within cmsb. I can't wait to install it for my next project, and indeed I might go for the unlimited licence. I think its a must for web projects these days and will quickly become an essential add on. Please shout about this, as its another string to your web developer bow!
By Toledoh - February 26, 2013
I've got zero knowledge of all this stuff, and have previously brushed clients away to an "SEO expert"... but I've purchased and played with the plugin on a couple of sites now and it's brilliant!
Tim (toledoh.com.au)