.htaccess being one of the main file a SEO work on because of the ability to give proper naming to links, I had written an article about it a year ago. Might be useful for some of the beginners, don’t be shy to ask if you don’t understand something or it doesn’t work as I never took the time to review it (no feedback = no correction).
Subject : Using .htaccess to simplify your linkings!
Written By: Tom Rochette
Date: 14 Jul 2005
Level: 2 (Novice)
Any new webmaster who have took a bit of time to check out all the available resources for them have already heard about .htaccess. This file located in a directory can have many uses, and most of them are often forgotten or not used at his full potential. Discover how .htaccess will make your webmastering job an easy task.
Table of Contents
- How can it simplify it?
- Use it without any modifications (for redirections)
- How to get it working with already written scripts
How can it simplify it?
Often forgotten, the mod_rewrite made a big come back. People started using it to do redirection of virtual addresses (addresses who links to a physical directory or files but don’t exist by itself). The mod_rewrite can be really interesting to see in action since it can change large URLs to smaller one:
Example of a long URL : www.yourdomain.com/ index.php?SearchIndex=Photo&BrowseNodeName=Photo
Example of the same URL, but shorten using mod_rewrite : www.yourdomain.com/Photo/Photo/
As you can see, you’ve saved a lot of space and the URL is easier to remember. With the mod_rewrite, you’ll be able to redirect your member to pages who might have quite long URL, but they will never notice it. It saves you time when you are writing pages since you don’t have to type a long query sentence.
Another good aspect of using the mod_rewrite is you can remove the query string when you are using PHP which makes your pages search engines friendly because most of the time those engine won’t even handle anything beyond the .php?. Now with your new URLs, your pages will look like simple html pages generated on the go.
Use it without any modifications (for redirections)
If you don’t really have any time to change all your URLs in your pages, you can still use mod_rewrite to make simple redirection URL you might use on other websites or give to your friends.
To use mod_rewrite, you’ll need to have it installed on your server.
Then, in a .htaccess file, you’ll write the following content:
<IfModule mod_rewrite.c>
RewriteEngine on
<… Insert your RewriteRules here …>
</IfModule>
Note that the .htacess as been placed in the root folder.
The ifModule will first check if it is possible to use the mod_rewrite. Then, you turn on the RewriteEngine. You can now make your RewriteRule(s) between the IfModule and the /IfModule. Here is an example of one:
RewriteRule ^forum/thread-([0-9]+)$ forum/index.php?showtopic=$1
The ^ and $ are used the same way in PHP, they are string handler. The ^ indicate the beginning of a string and $ the end. Using ([0-9]+}, you’re telling your server to accept any incoming number. This number will be used in our old page and will be called $1. If you use more than one variable, they will be $2,$3,$4,$5,…
The RewriteRule work this way: First you input the part you’d like the server to make virtual (which is ^forum/thread-([0-9]+)$ here). This will make www.yourdomain.com/forum/thread-123 accessible from any browser. The second part indicate where it should go (which page it is suppose to be in fact). Since I’ve used $1, there will be a variable used. The server will load the page www.yourdomain.com/forum/index.php?showtopic=123. The great thing though is that it won’t show this in the address bar. It will show the “cloaked” URL, which is what you want!
How to get it working with already written scripts
If your scripts are already written and you’d like to use them with the mod_rewrite, here is a “workaround” you can use.
First, load the page you’d like to modify the urls to the new one. If it is in PHP format do the following:
- Open your PHP : <?PHP
- Create a variable you’ll use, I’ll call mine content.
- in it, write the following : <<<THECONTENT
- Put this above your PHP Content so THECONTENT is the sentence above all your PHP code.
- At the end of your PHP code, write THECONTENT;
- Then, using preg_replace("stringyourelookingfor","stringyourechangingto","stringinwhichthecontentis");
- Echo the page
- Close your PHP : ?>
You should end up with something like:
<?PHP
$CONTENT= <<<THECONTENT
all the stuff being echoed
THECONTENT;
$CONTENTx = preg_replace(“?page=”,””,$CONTENT);
echo $CONTENT;
?>
This will make any of your link using index.php?page= to be switched to just their name. Then, in your htaccess, write the following:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9]+)$ index.php?page=$1
</IfModule>
What can be useful if you want to add things after the $page variable, is to use the (\w+) in your preg_replace. This will allow you to take any content before or after and use it as a variable to replace text. This would look like
$CONTENTx = preg_replace("’\?page=(\w+)’","\\1/",$CONTENT);
This preg_replace function would now take any content listed after the $page= and replace it. So you’d get www.yourdomain.com/page/ instead of www.yourdomain.com/page. It can be usefull if you want to make your page look like they were indexes of directory.
This article has been written by Tom Rochette
http://www.ewealthdev.com/