Htaccess 301 Redirect Rules

Choose what you are looking for below for a fast answer (copy & paste to your file). Make sure you edit the URLs.

How do you redirect a single page?

Redirect 301 /example.php http://www.yourdomain.com/example.html

How do you redirect an entire site or domain to a new one?

Redirect 301 / http://www.yourdomain.com/

How do you redirect an entire site to a sub folder?

Redirect 301 / http://www.yourdomain.com/subfolder/

How do you redirect a sub folder to different website?

Redirect 301 /subfolder http://www.yourdomain.com/

How do you redirect a file extension but retain the page name?

Example: If you want an .html extension to use the same filename but use the .php extension.

RedirectMatch 301 (.*)\.html$ http://www.yourdomain.com$1.php

How do you use rewriting to redirect from an old domain to a new domain?

RewriteEngine on
RewriteBase /
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

How do you use rewriting to redirect from a non-www to a www subdomain?

RewriteEngine on
RewriteBase /
rewritecond %{http_host} ^yourdomain.com [nc]
rewriterule ^(.*)$ http://www.yourdomain.com/$1 [r=301,nc]

How do you use rewriting to redirect a domain to a www location with a subdirectory?

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/directory/index.html [R=301,NC]

How do you use rewriting to redirect from an old domain to a new domain that includes the full path and query string?

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*) http://www.newdomain.com%{REQUEST_URI} [R=302,NC]

How do you use rewriting to redirect from an old domain with a subdirectory to new domain without the subdirectory but include the full path and query string?

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/subdirname/(.*)$
RewriteRule ^(.*) http://www.katcode.com/%1 [R=302,NC]

How do you rewrite and redirect URLs with query parameters with files placed in a root directory?

Example: The original URL being http://www.website.com/index.php?id=3 and the new URL being http://www.website.com/path-to-new-location/

RewriteEngine on
RewriteCond %{QUERY_STRING} id=3
RewriteRule ^index\.php$ /path-to-new-location/? [L,R=301]

How do you redirect URLs with query parameters and place files in a subdirectory?

Example: The original URL being http://www.website.com/sub-dir/index.php?id=3 and the new page being http://www.website.com/path-to-new-location/

RewriteEngine on
RewriteCond %{QUERY_STRING} id=3
RewriteRule ^sub-dir/index\.php$ /path-to-new-location/? [L,R=301]

How do you redirect a site to HTTPS from HTTP to eliminate duplicate content?

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

How do you redirect a site from HTTP to HTTPS to eliminate duplicate content?

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}

How do you remove an index.html or index.php and redirect to the root?

RewriteEngine On
RewriteCond %{THE_REQUEST} /index.php HTTP [NC]
RewriteRule (.*)index.php$ /$1 [R=301,L]
RewriteEngine On
RewriteCond %{THE_REQUEST} /index.html HTTP [NC]
RewriteRule (.*)index.html$ /$1 [R=301,L]

How do you rewrite and redirect URLs with query parameter to directory based structure while retaining the query string in the URL root level?

Example: The original URL being http://www.website.com/index.php?id=100 and new page being http://www.website.com/100/

RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [QSA]

How do you rewrite URLs with a query parameter to a directory based structure while retaining query string parameter in the URL subdirectory?

Example: The original URL is http://www.website.com/index.php?category=fish and new page being http://www.website.com/category/fish/

RewriteEngine On
RewriteRule ^/?category/([^/d]+)/?$ index.php?category=$1 [L,QSA]

How do you redirect an old website to a new domain and retain the URL path?

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example-old\.com$ [NC]
RewriteRule ^(.*)$ http://www.example-new.com/$1 [R=301,L]

If you do not want to pass the path to the new domain, change the last line to:
RewriteRule ^(.*)$ http://www.example-new.com/ [R=301,L]

How do you rewrite and add a trailing slash to URLs without one?

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} !(.*)/$ 
RewriteRule ^(.*)$ http://www.yourdomain.com/$1/ [R=301,L]

How do you redirect from a blog sub domain to to a blog folder?

Example: Redirect blog.oldsite.com to www.newsite.com/blog/

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI}/ blog
RewriteRule ^(.*) http://www.somewhere.com/%{REQUEST_URI} [R=302,NC]
RewriteRule ^(.*) http://www.somewhere.com/blog/%{REQUEST_URI} [R=302,NC]

How do you redirect one directory to another?

Options +FollowSymLinks RewriteEngine On RewriteRule ^(.*)/old-directory/(.*)$ $1/new-directory/$2 [R,L]

What is an .htaccess file?

.htaccess is a configuration file for use on web servers running the Apache Web Server software. When a .htaccess file is placed in a directory which is in turn ‘loaded via the Apache Web Server’, then the .htaccess file is detected and executed by the Apache Web Server software.

These .htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer.

These facilities include basic redirect functionality, for instance if a 404 file not found error occurs, or for more advanced functions such as content password protection or image hot link prevention.

2 thoughts on “Htaccess 301 Redirect Rules”

  1. It’s so frustrating when I make changes to the htaccess file and it doesn’t work! Thanks for this huge list of options.

Leave a Comment

Your email address will not be published. Required fields are marked *