FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home  »  Htaccess  »  Commonly Used htaccess Code

by 5 comments

[hide]

A lot of commonly used htaccess code snippets for use with the Apache Web Server.


Note 1: You may not need Options FollowSymlinks as it's commonly set by serverwide config file, already.

Note 2: You need RewriteEngine On only once in the .htaccess file.

The fix for Missing trailing slash problems

If you have 'www' dropping from the URL or asked twice for the password, this is the fix.It happens when you enter the URL of a directory without trailing slash and your domain name contains 'www'.ex. http://www.example.com/subdir <== No slash at the end.Generally, you should ALWAYS put a slash after a directory name.But some robots and links don't follow this practice, and you may want to use this fix.In short, you only need this code if you use 'www.' for your domain name.I prefer URL without 'www', these days, as it's shorter and easier.
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^/*(.+/)?([^.]*[^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
 
#If you want to cover both http and https:
Options +FollowSymlinks
RewriteEngine On
RewriteCond s%{HTTPS} ^((s)on|s.*)$ [NC]
RewriteRule ^/*(.+/)?([^.]*[^/])$ http%2://%{HTTP_HOST}/$1$2/ [L,R=301]
#Note: These codes are very efficient compared to the code
#with '-d' check,  but they won't work with directories that have a dot (period) in it.
# (ex. /a_directory.name/)

Forcing www for your domain name

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
 
#If you want to cover both http and https:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST}//s%{HTTPS} ^([^.]{4,}|[^w.]?[^.][^w.]?[^.]?[^w.]?)..*//((s)on|s.*) [NC]
RewriteRule ^ http%3://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Combined code for Missing trailing slash problems and Force www

It may look complicated, but this code reduce the wasteful redirect to your site.

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI}/%{HTTP_HOST}/www. ^/+(.+/)?[^.]*[^/](/)(([^.]{4,}|[^w.]?[^.][^w.]?[^.]?[^w.]?)..+/(www.)|.*)$ [OR,NC]
RewriteCond %{HTTP_HOST}/www. ^(/)?(#)?(/)?(([^.]{4,}|[^w.]?[^.][^w.]?[^.]?[^w.]?)..+/(www.))$ [NC]
RewriteRule ^ http://%6%{HTTP_HOST}%{REQUEST_URI}%2 [L,R=301]
 
#If you want to cover both http and https:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI}/%{HTTP_HOST}/www.//s%{HTTPS}
^/+(.+/)?[^.]*[^/](/)(([^.]{4,}|[^w.]?[^.][^w.]?[^.]?[^w.]?)..+/(www.)|.*)//((s)on|s.*)$ [OR,NC]
RewriteCond %{HTTP_HOST}/www.//s%{HTTPS}
^(/)?(/)?(([^.]{4,}|[^w.]?[^.][^w.]?[^.]?[^w.]?)..+/(www.))//((s)on|s.*)$ [NC]
RewriteRule ^ http%7://%5%{HTTP_HOST}%{REQUEST_URI}%2 [L,R=301]

Force to remove www from your domain name

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC] RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
 
#If you want to cover both http and https:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST}//s%{HTTPS} ^www.(.*)//((s)on|s.*)$ [NC]
RewriteRule ^ http%3://%1%{REQUEST_URI} [L,R=301]

November 7th, 2006

Comments Welcome

  • Colin

    This is great info, but why can I not find on the internet nor make it work myself to REMOVE trailing slashes? I have a .htaccess rewrite that makes nice short URLs, removes the www, and I want it to remove trailing slashes too.

  • Alan

    The "Combined code for Missing trailing slash problems and Force www" does not work. The seperate options do.

    Is it much worse just joining the two code together?

    Please can you fix the combined code!

    Cheers.

  • http://eapen.in Eapen

    Removing trailing slashes is easier IMO

    RewriteRule ^(.+)/$ /$1 [R=301,L]

  • Steven

    I'm a relative regex noob, so hope this doesn't sound too stupid:
    Under "Forcing www for your domain name" is this pattern:

    RewriteCond %{HTTP_HOST} !^(www.|$) [NC]

    AFAIK, the pipe | represents OR, ^ is the beginning of the string, and $ is the end of the string. So

    ^(www.|$)

    means "begins with 'www.' or begins with the end".

    How could a request string that reaches my server ever "begin with the end" (i.e. be empty)? Why test for this?

  • http://www.infinite-eye.com luke

    the expression for forcing www (where http AND https are wanted) does not work - it redirects a www.example.com to www.www.example.com

    any ideas on why that's broken?

My Online Tools
My Picks
Twitter

  • : Love that Tesla Model S
  • : Ratpoison, a lean mean WM for Linux (urvxt), I love it
  • : You understand, what I need to know is exactly what happens to the passengers in an elevator when it falls into emptiness - Einstein
  • : It's not process, its content. That's what makes good products. -jobs


Related Articles
Newest Posts

WordPress Development
Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two. See my article, On Hacking.
-- Richard M. Stallman






It's very simple - you read the protocol and write the code. -Bill Joy

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

| Google+

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service

↑ TOPMain