Thursday, April 8, 2010

Simple htaccess script that will make your website's url look good

1. How to redirect your website to non-www to www.

RewriteEngine On
RewriteBase /RewriteCond %{HTTP_HOST} ^yoursite\.com [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [R=301,L]

Copy the code into your htaccess file and don't forget to change"yoursite" with your website name

2. How to create an htaccess script that would allow you to customize your website url

here is a sample:

RewriteEngine On
RewriteBase /RewriteCond %{HTTP_HOST} ^yoursite\.com [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [R=301,L]
RewriteRule ^news/([^/]+)/([^/]+)/([^/]+)/([^/]+).html permapages.php?year=$1&month=$2&day=$3&title=$4 [NC]

for example, you have this url http://www.yoursite.com/news/2010/2/2/testing.html.
This url will point to "permapages.php" based on what we declared in our htaccess and the
"([^/]+)/([^/]+)/([^/]+)/([^/]+)" are the parameters that we passed along with the script.
So if you are going to print the $_GET values inside permapages.php you will get something like this

array( year=>2010, month=>2, day=>2, title=>testing)

You can now add codes in your permapages.php to display contents that you want to put in http://www.yoursite.com/news/2010/2/2/testing.html.

note: put permapages.php in root folder of your site
This is it. Hope it helps.

No comments: