.htaccess RewriteRules and Cheat Sheet

Hints for using .htaccess to redirect file, folders and queries

If you’d like some background on .htaccess read this first: Overview of .htaccess

OK, let’s dive in…

Ensure once (and only once) you have this in your .htaccess file at the top:

RewriteEngine on
RewriteBase /

Simple  URL Redirect

Simple redirect from one url (file) to another: In this case from faqs.html to faq.html

RewriteRule ^faqs\.html$ http://yoursitename.com/faq.html? [R=301,L]

Unpacking:

  • ^ start of regex (regular expression – the search string)
  • \. you need to “escape” the . with a \ because a . has a different meaning in a regex
  • $ end of regex
  • ? at the end of the .html prevents any query strings (like “&doLogin=1”) from being added to the end of the web address. Particularly important in the query string example a bit lower in this article.
  • [R=301,L] R is for redirect, 301 is a permanent redirect (so Google will update their index). L is for last rule. This is generally a good idea, as this way the changed address will have all the rules run again. So if you are redirecting from faqs.html to faq.html and then changing that to pages.php?pagename=faq, then you need to do it this way, with the L, and not with a simple “Redirect 301”

Folder

Redirect a folder to a real filename. In this case yoursitename.com/faq to yoursitename/faq.html

RewriteRule ^faq/?$ http://yoursitename.com/faq.html? [R=301,L]

Differences:

  • /? after the folder name allows there to be a slash, or not. So yoursitename.com/faq and yoursitename/faq/ will both redirect to yoursitename.com/faq.html

 

Query String to URL

This is not the normal way of doing things. Most of the time you want to change a normal page into a query. But this is useful for old joomla or wordpress links

Change a query into a page:

RewriteCond %{QUERY_STRING} ^pages_id=24$
RewriteRule ^info_pages\.php$ http://yoursitename.com/faq.html? [R=301,L]

First of all, need to point out that RewriteCond only applies to the next RewriteRule, and not all subsequent rules.

So what is happening here is it is looking for a page that has ?pages_id=24 at in the query. So index.php?pages_id=24 would match this, so would pages.php?pages_id=24.

Also note the ? at the end of the redirected page. Without this you would be redirected to http://yoursitename/faq.html?pages_id=24 which is ugly.

 

Pretty Links (fake html pages)

Used if you want to have nice page names like faq.html instead of infopage.php?pageName=faq

This rule will change faq.html into infopage.php?pageName=faq, but the browser (and Google) will still see faq.html

This makes it easy for people to pass on the address and they don’t see any mention of PHP.

RewriteRule ^(.*)\.html infopage.php?pageName=$1 [QSA,L]

Unpacking:

  • ^ Start of regex
  • (.*) any characters, and as many of them as you like. The brackets makes this bit the variable
  • \. the dot is escaped so it can be seen as a dot
  • html – this is the “fake” extension. There are no .html pages on this site 🙂
  • $1 this is the variable. So it takes whatever is in the brackets, and puts it here. Hence faq.html  becomes infopage.php?pageName=faq
  • [QSA,L] Query String Append – adds whatever query string was passed through along with the original address to the new address – this is particularly useful if redirecting one php page (with queries attached) to another. The L tells the page to apply the rule, apply no other rules, and reload the page and then go through the rules again. You need this if you want to apply more than one rule – like the ones below after this one.

 

Remove www from the Address

Some people want to keep it, others want to remove it. Personally I remove it. Makes the address shorter. If you want to keep it, then force all the pages to keep it. This is how to force all pages to drop the www:

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

 

Force to https (SSL)

Forces use of https:

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

This rule is nice in that you don’t need to edit it for it to work on your site.

 

Example .htaccess File

You can (and should) use # to write comments – anything after a # on the same line is ignored and treated as a comment.

The order is important. The rules are run top to bottom, so if you are changing one .html url to another, and then you want to change that to a php page, you need to do the change from one .html to another first.

Putting it all together, you may end up with a .htaccess file like this:

# -- Rewrite Section --
RewriteEngine on
RewriteBase /

# -- Redirect Page --
RewriteRule ^faqs\.html$ http://yoursitename.com/faq.html? [R=301,L]

# -- Redirect Folder --
RewriteRule ^faq/?$ http://yoursitename.com/faq.html? [R=301,L]

# -- Redirect old php URL that had a query string --
RewriteCond %{QUERY_STRING} ^pages_id=24$
RewriteRule ^info_pages\.php$ http://yoursitename.com/faq.html? [R=301,L]

# -- Nice Links (fake .html pages redirected to php page as query) --
# make sure all the page and folder redirects to .html are above this
RewriteRule ^(.*)\.html infopage.php?pageName=$1 [QSA,L]

# -- Remove www --
RewriteCond %{HTTP_HOST} ^www\.yoursitename\.com$ [NC]
RewriteRule ^(.*)$ http://yoursitename.com/$1 [R=301,L]

# -- Force https: (SSL) --
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

 

I’m a programmer, and I use lines like this in the .htaccess file of cloud apps that I produce. However this page may contain errors, or may not work correctly in your environment. Test and check before deploying.

Cheat Sheet

Here’s a great little cheat sheet for mod rewrite in those .htaccess files by AddedBytes.com:

mod-rewrite-cheat-sheet

mod_rewrite-cheat-sheet-v2

4 thoughts on “.htaccess RewriteRules and Cheat Sheet”

    • Google translate (Turkish): “Thank you everything I am looking for I find your content”

      Rica ederim (You’re welcome).

      Reply
    • Google translates this as “Thank you very much, I find your content a lot about my search”.

      Rica ederim (You’re welcome).

      Reply

Leave a comment