The Most Active and Friendliest
Affiliate Marketing Community Online!

“Propeller”/  Direct Affiliate

mod_rewrite simple tutorial

M

melkior_inactive

Guest
Ok, mod rewrite is very good function of Apache Web Server.
The potential is enormous. As you might or might not know, I'm developing a link selling app for UK WW.
And we want the app to be SEO friendly.
So this is a potential use of mod rewrite.
It's also good if you want to hide the structure of your website, and the arguments that you pass to the script.

So what does it look like?
Well this is the basic idea:
you have a site called www.domain.com
and you have a script which passes the arguments.
So your url look like this:
www.domain.com/index.php?query=xxx&id=yyy

mod rewrite enables you to change that into:
www.domain.com/xxx/yyy/

First of all make sure that mod_rewrite is available on your server and turned on.
You can check that out by creating a file (let's call it phpinfo.php) with the following content:
PHP:
<? phpinfo(); ?>

Go to that page and search for mod_rewrite. If you find it you're good to go.
If not, contact your host.

OK, now create a .htaccess file with this content:
Code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)/(.*)/$ index.php?query=$1&id=$2 [L]
Upload the file to your server into the folder that contains the script.
Now, what this does is: when someone requests bla/bla/ on your server, it automatically gets transcribed to index.php?query=bla&id=bla
Cool, isn't it?

But maybe you want your site to look like this:
www.domain.com/xxx-yyy.html
No problem!
This is the setup for the .htaccess file:
Code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)-(.*).html$ index.php?q=$1&id=$2 [L]
So if you need index.php?query=bla&id=bla you can now access it like bla-bla.html

Now, all you need to do is update the links in your site and your site is SEO friendly and harder to hack.
Here's a little help with that. If you have static links you have no problems, but chances are that your script is generating the urls based on various conditions.
So here's the old code:
PHP:
<html>
<body>
...
...
<?
echo '<a href="$siteurl/index.php?query=type&id=$id">Link</a>';
?>
You'd change it to:
PHP:
<html>
<body>
...
...
<?
echo '<a href="$siteurl/type/$id/">Link</a>';
?>
for the first type of url rewrite.
Or:
PHP:
<html>
<body>
...
...
<?
echo '<a href="$siteurl/type-$id.html">Link</a>';
?>
for the second type.
Hope it helps.
 
Thanks Temi!
Hope our members will find it useful.
If anyone has questions don't be afraid to ask. :D
 
Wow Melkior, I don't say that I would become a htaccess genius, but your tutorial do really helps a lot to understand this rewrite thing.
Rep added, thank you:D
 
Bagi, I am more than pleased if you get anything useful from these ramblings! :D
I'll add a few more examples and suggestions tomorrow.
 
That is very kind of you. I didn't understand this topic so far because i couldn't connect it with php, but you gave examples how to modify the php file also. :D
 
I promised to add a few more explanations but never got back to do them.
Now, when I see how many linkbacks this thread has I think I'll try to explain a few more things.

In the first part of the tutorial I gave examples how to change your dynamic link structure too look like a directory structure or an HTML pages structure.

These are the two most common examples, but truth is you'll probably want a combination of these two.

So you want your www.domain.com/index.php?query=xxx&id=yyy to look like this:
www.domain.com/xxx/yyy.html

How do you achieve this?
Your .htaccess file should look like this:
Code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)/(.*).html$ index.php?query=$1&id=$2 [L]

And your links update in the PHP script should be something like:
PHP:
<html>
<body>
...
...
<?
echo '<a href="$siteurl/type/$id.html">Link</a>';
?>
This makes your site not only SEO friendly but human friendly too.
 
Melkior,

Better later than never:
Thank you so much for this marvellous post:D Your knowledge on this is really impressive and I learned (again) a lot...

lala
 
Sorry for being a newb (I tried to figure it out myself before posting).
Where in your site might you find the php script which you enter the following code into?

I'm trying to implement this into this site... A2Z Sex Shop - His And Her Sex Toys, Dildos, Strapons, Dolls, Bondage kits

Thanx!

And your links update in the PHP script should be something like:
PHP:
<html>
<body>
...
...
<?
echo '<a href="$siteurl/type/$id.html">Link</a>';
?>
This makes your site not only SEO friendly but human friendly too.
 
Lala, you're too kind!

mrcrowley, if you're not familiar with PHP programming I highly advise that you hire some professional(s) to do this for you.
It looks like a rather complex script and integrating mod_rewrite would take a lot of work.
All I gave here are theoretical examples for those who are writing their own scripts and want to make them SEO friendly.
Modifying an existing script is of course possible but it can also be risky if you're not sure what you're doing.
The code modifications depend on the script itself and editing the source imposes that you are familiar with the way the script works in high detail.
 
Lala, you're too kind!

mrcrowley, if you're not familiar with PHP programming I highly advise that you hire some professional(s) to do this for you.
It looks like a rather complex script and integrating mod_rewrite would take a lot of work.
No problem i thought it was just editing a few files (adding a few lines) I will stop right there. I mean i always back things up before i mess with em anyway. I tend to fiddle with things and if they don't work put them back nothing to lose that way.But if theres complexities involved then i'm out lol
 
Thanks for the guide Melkior, I was keeping an eye out on how to do this.

I had some trouble getting this to work, I had to comment/remove the 1st line of the .htaccess file (#Options +FollowSymLinks) to do this but working fine now.

This may have been due to me messing around with the config file for apache.

Humous
 
for yourdomain.co.uk -> www.yourdomain.co.uk

RewriteEngine On

RewriteCond %{HTTP_HOST} ^yourdomain\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.yourdomain.co.uk/$1 [R=301,L]
 
Search engines Consider www & without www is two Different websites & ask to provide a Preferred domain for each website . mod_rewrite is great function to specify in .htaccess for 301 SEO Friendly Redirection also
 
MI
Back