.htacess redirect and rewrites

I am writing this blog for a very simple usage of htaccess for redirects and rewriting the url

The first things that needs to be understand for the htaccess REWRITE and REDIRECT modules are the synstax

 

marks the start of line. It means anything that starts with

$ marks the end of line.

(.*) means any character within the url. This gets replaced by $1 at first occurence and $2 as second occurence

L means last code and do not execute anything after this.

R means redirect the page, so the url will be changed. However if you do not add that, it will be same.

 

 

Now here are some sample code

 

RewriteRule ^styles/ - [L,NC]

Now the above code means that if any url starts with style, just let it go through it. for example

http://www.google.com/styles

 

RewriteRule ^resources/image/thumb/(.*)/?$ /download/file.php?id=$1&t=1 [NC,L] # Handle product requests

If any url starts with resources/image/thumb/(.*){THIS COULD BE ANY STRING}/

http://www.google.com/resources/image/thumb/65/

http://www.google.com/resources/image/thumb/was5/

http://www.google.com/resources/image/thumb/adhg5/

Go to looking for

http://www.google.com/download/file.php?id=$1&t=1

Here $1 is (.*){THIS COULD BE ANY STRING}/

http://www.google.com/download/file.php?id=was5&t=1

http://www.google.com/download/file.php?id=65&t=1

 

 

RewriteRule ^resources/image/(.*)/rokbox.jpg?$ /download/file.php?id=$1&mode=view/rokbox.jpg [NC,L] # Handle product requests

RewriteRule ^(.*)/$ $1.html [R,NC,L] # Permanent Move
RewriteRule ^(.*)/(.*)$ $2 [R,NC,L] # Permanent Move

 

 

To exclude a file from htaccess rule: follow this

RewriteCond %{REQUEST_URI} !^/google89b2a81fd1609a6e\.html$

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s