• Enter your email address to subscribe to this blog and receive notifications of new posts by email.

  • Categories


  • Follow us On

7 Useful tips for URL Re writing with htaccess

Problem one 


I want it to look like this instead:


Solution One

Use mod_rewrite in the .htaccess file.

RewriteEngine On

RewriteRule ^/cars/(.*) cars.php?$1 [PT]

Problem Two 

I have a URL that look like this:


I want it to look like this instead:


Solution Two

RewriteEngine On

RewriteRule ^/product/([^/]*)/([^/]*) /cgi-bin/product.cgi?bottle=$1&topic=$2 [PT]

Why This Works

It requires that the requested URL look exactly like the URL that is

described in the pattern. This allows for a cleaner URL for Google

to spider more easily.

Problem Three 

I used an HTML editor and it named all of my files .htm but I want to go

to .html as a standard on my site. I need the old URLs to work because

my site is heavily bookmarked, but I don’t want to get hit with the

duplicate content penalty.

Solution Three

RewriteEngine On

RewriteRule (.*).htm $1.html [PT,L]

Why This Works

The “$1” passes the entire URL back and only the extension is

changed. Any query string from a dynamically generated page will

be passed as well.

Problem Four 

I want all non-www URLs to go to their www version to protect against

duplicate content issues with Google.

Solution Four

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteCond %{HTTP_HOST}!

^www\.domain\.com [NC]

RewriteRule ^(.*)$ http://www.domain.com/$1



If you have subdomains, you want the following code. You only want to

use one instance of this code, so use either the one above, or the one


RewriteEngine On

RewriteBase /

RewriteCond %{HTTP_HOST} ^domain.com [NC]

RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

Why This Works

Regardless of what the user types or what another site has linked,

your home page and any subpage will appear with the “www”.


What if you want the exact opposite? You want the www URLs to

redirect to the non-www version? Use the code below:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www.domin.com

RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

Problem Five 

I just checked my log files and I am getting a ton of websites that are

hijacking my images and referring to my site for theirs, thus using my

bandwidth for their theft. What can I do?

Solution Five

Block the use of your image files directly on the server.

RewriteEngine on

RewriteCond %{HTTP_REFERER} !^$

RewriteCond %{HTTP_REFERER} !^http://domain.*$ [NC]

RewriteCond %{HTTP_REFERER} !^http://www.domain.com.*$ [NC]

RewriteRule .(gif|jpg|png)$ – [F]

Why This Works

Any image file (gif, jpg, png) is rewritten to a forbidden status

from any outside request, thus protecting your bandwidth. Of

course, the dirty way to solve this is to change out the images to

something that is a huge file and in turn the thief’s website will

take forever to load. Jake Baillie was made famous for swapping

the images to porn images, but that got him into trouble with

some attorneys, so be careful out there.

Problem Six: I want to be able to redirect visitors to the root of the 

folder, not to the index file. So instead of seeing http://www.xyz.com/

index.html, they would see www.xyz.com.

Solution Six:

Options +FollowSymLinks

RewriteEngine on

RewriteCond %{THE_REQUEST} ^.*/index.html

RewriteRule ^(.*)index.html$ http://www.domain.com [R=301,L]

Why This Works

This takes the “index.html” file and rewrites it to the root. If your

main file was “index.htm” or “index.php” you would put that in


Problem Seven 

Google is indexing both our http and https versions of our site. Will this

cause duplicate content issues?

Solution Seven

The problem with allowing GoogleBot to index your secured pages is

that it is more taxing for GoogleBot and on your server. It’s never a

good idea. There is no reason to let Googlebot index those pages

anyway. While you could do exclusion in the robots.txt file, I wouldn’t

trust it. Instead, use your .htaccess file to redirect anything from

useragent googlebot to “http://&#8221; link instead of “https://&#8221;.

RewriteEngine on

RewriteBase /

RewriteCond %{HTTP_USER_AGENT} ^.*googlebot.*$

RewriteCond %{SERVER_PORT}443

RewriteRule ^(.*)$ http://www.domain.com/$1 [R,L]

The first rewrite condition checks to see if the useragent is Googlebot.

The second rewrite condition checks to see if the port is 443 (the SSL

port), to which Google is trying to get. If both of these match it will

redirect to http://www.domain.com/etc/etc

So, if Google keeps screwing up, in the future this redirect won’t allow

them to jump to your secured area anymore.


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

%d bloggers like this: