Static Urls from Dynamic Urls using Mod Rewrite

Static Urls from Dynamic Urls using Mod Rewrite (htaccess).

There are three stages to go through to complete and be successful in this venture

  • 1 The .htaccess file it self.
  • 2 The Code used in the htacces file.
  • 3 Modifcation to your code which will allow it to work.

The .htaccess File

This file must either be placed in the root directory of your website or in a folder where you wish to control the code to your webpages.

 

NOTE: The file name must have a period in front of it to work.

NOTE: Once this file is uploaded most FTP Programs will not allow you to see it.

NOTE: File Managers in your control panel will allow you to see it and edit the file.

 

Content for the .htaccess file

Lets convert http://www.domain.com/Content/photo.php?prod=21&cat=12
to http://www.domain.com/Content/photo/21/12.html

  • Options +FollowSymLinks
  • RewriteEngine On
  • RewriteBase /Content/
  • RewriteRule ^photo/([A-Za-z0-9-]+)/([0-9]+).html$ photo.php?prod=$1&cat=$2 [L]

NOTE: The above code is placed into your .htaccess file stored in a folder called "Content"

 

Lets convert http://www.domain.com/photo.php?prod=21
to http://www.domain.com/photo/21.html

  • Options +FollowSymLinks
  • RewriteEngine On
  • RewriteRule ^photo/([A-Za-z0-9-]+).html$ photo.php?prod=$1 [L]

NOTE: The above code is placed into your .htaccess file stored in root of your website

 

Modify the code in your program

This is the code before modification href=\"photo.php?prod=$row[productcode]&cat=$cat

This is the code after modification href=\"photo/$row[productcode]/$cat.html

 

Please NOTE: the above covers a small amount of the mod rewrite problems which webmasters face today, it is aimed at the beginners not the experanced programmers.