Tutorial Mod_rewrite

Chris S

Retired
Messages
2,055
Reaction score
1
Points
38
*Note - x10hosting has mod_rewrite enabled.

Q. What is mod_rewrite?

A. Mod_Rewite is an apache module for URL manipulation. It can be used to change the appearance of a path to which a file exists.

http://httpd.apache.org/docs/mod/mod_rewrite.html said:
This module uses a rule-based rewriting engine (based on a regular-expression parser) to rewrite requested URLs on the fly. It supports an unlimited number of rules and an unlimited number of attached rule conditions for each rule to provide a really flexible and powerful URL manipulation mechanism. The URL manipulations can depend on various tests, for instance server variables, environment variables, HTTP headers, time stamps and even external database lookups in various formats can be used to achieve a really granular URL matching.

Q. Why use Mod_Rewrite?

A. The most common use for Mod_ewrite is for SEO (Search Engine Optimization). It is used to trick the spider into thinking dynamically created pages are static.

For example, showthread.php?t=19864 can be displayed as thread19864.html

A common misunderstanding by people is that it isn't effective due to the fact that dynamically created pages are indexed by search engine spiders anyway. Allthough this is true there are problems concerned with dynamically created pages being indexed, the main problem is that spiders such as the google spider will move away from dynamically created page once their session has ran out to prevent being caught in an infinate loop inside a site.

Ok, now that those questions are answered. Lets get on to making the correct files.



Step 1 - Meh

Read this tutorial

Step 2 - Creating the rewrite rule
Getting the rule you want. This is up to you.
It could be turning /profile.php?user=123456 into 123456.html <-- will be our example for the tutorial.

Step 3 - Creating a .htaccess file
Create a .htaccess using notepad. Save the file as .htaccess
You have to change the save type to All Files

Step 4 - Turning on Mod_Rewrite
The first thing we need to do to that file is turn on mod_rewrite. Use this code

Code:
RewriteEngine on
Options +FollowSymLinks

Step 5 - Creating the rule
Next add this code to the .htaccess
*Note if the file you want is in a sub directoy you need to put that in the link.

Code:
RewriteRule ^([0-9]+).html$ profile.php?user=$1 [L]

Code with sub directoy
Code:
RewriteRule ^([0-9]+).html$ /forums/profile.php?user=$1 [L]

Step 6 - Save the file
Again save the file. Next go to your FTP program and uplod the file into the directory with profile.php



If all else fails here is a link that will create the code for the .htaccess file

Mod_Rewrite Generator
Behind the Secens with Apache's .htaccess

*if you see any errors tell me. Ether I or a mod will edit. (Mods you can edit this :tongue:
 

Chris S

Retired
Messages
2,055
Reaction score
1
Points
38
ya,

well i actually copied it from another site but i posed it on that site but at 1 point in time i did
 
B

Brandon

Guest
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^([0-9]+).html$ sig.php?name=$1 [L]

When I go to blah.html I dont get the image I have tried with php too.
 

Chris S

Retired
Messages
2,055
Reaction score
1
Points
38
did you try this

RewriteEngine on
Options +FollowSymLinks
RewriteRule ^([0-9]+).png$ sig.php?name=$1 [L]

also is the .htaccess in the folder you are calling the page from?
 
Top