Apache Rewrite and Redirection .htaccess Rules Cheatsheet - everything you need to know

Apache Rewrite and Redirection .htaccess Rules Cheatsheet

How to enable the rewrite engine in .htaccess?

Before you can use rewrite rules in .htaccess you must enable the rewrite engine.

To enable the rewrite engine in .htaccess you must add this to your .htaccess file:

  1. RewriteEngine On
View More Details (and 10 discussions about this topic) Here...

How to set environmental variables in your .htaccess file

Setting env vars is easy in your .htaccess file.

  1. SetEnv ENV_VAR_NAME 'env var value'

(you must enable mod_env in Apache for this to work)

You can access these environment variables in PHP with this code:

  1. var_dump(getenv('ENV_VAR_NAME'));
View More Details (and 6 discussions about this topic) Here...

How to Force www or non-www in htaccess

If you want to redirect non-www requests to the www URL of your website (i.e. redirect from http://example.com to http://www.example.com/) then you can do this with a few lines in your .htaccess file

  1. RewriteCond %{HTTP_HOST} !^$
  2. RewriteCond %{HTTP_HOST} !^www\. [NC]
  3. RewriteCond %{HTTPS}s ^on(s)|
  4. RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Don't forget to enable the rewrite engine!

Don't forget that you will probably need to enable the rewrite engine for these rules to work. Click the button below to find out how to do this.

View More Details (and 4 discussions about this topic) Here...

How to redirect your website to a new domain/location in .htaccess

If you need to move domains and want to 301 redirect all old URLs to the new domain/location, use the following .htaccess code

  1. Redirect 301 / http://your-new-domain.com/
View More Details (and 4 discussions about this topic) Here...

How to remove .php from the end of URLs with .htaccess

If you have files such as hello.php and you want users to be able to go to yoursite.com/hello, use the following code:

  1. RewriteCond %{SCRIPT_FILENAME} !-d
  2. RewriteRule ^([^.]+)$ $1.php [NC,L]

A request to http://yoursite/asdf will try and use the file /asdf.php.

Don't forget that you will probably need to enable the rewrite engine for these rules to work. Click the button below to find out how to do this.

View More Details (and 12 discussions about this topic) Here...

How to force non www in htaccess

If you want to automatically redirect from www to non-www of your website, in .htaccess then you can add this code (edit your domain as required) to your .htaccess

  1. #Force non-www:
  2. RewriteEngine on
  3. RewriteCond %{HTTP_HOST} ^www\.YOURSITE\.com [NC]
  4. RewriteRule ^(.*)$ http://YOURSITE.com/$1 [L,R=301]

Don't forget that you will probably need to enable the rewrite engine for these rules to work. Click the button below to find out how to do this.

View More Details (and 7 discussions about this topic) Here...

How to force SSL in Apache's .htaccess?

If you need to enable SSL (https) URLs in htaccess (redirect non SSL to SSL, or redirect http to https in .htaccess) then you should use this code:

  1. RewriteCond %{HTTPS} !=on
  2. RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

If you want to redirect to https AND force www then you can use the following code:

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

Don't forget that you will probably need to enable the rewrite engine for these rules to work. Click the button below to find out how to do this.

View More Details (and 7 discussions about this topic) Here...

Point all requests to one PHP file

If you want every request to be routed through a main file (similar to how WordPress and a lot of frameworks do it) then you can use this code, and every request that isn't an existing file or directory (so users can still access .css or images):

  1. RewriteCond %{REQUEST_FILENAME} !-f
  2. RewriteCond %{REQUEST_FILENAME} !-d
  3. RewriteRule ^([^?]*)$ /index.php [NC,L,QSA]

Remember to create the /index.php file too.

Don't forget that you will probably need to enable the rewrite engine for these rules to work. Click the button below to find out how to do this.

View More Details (and 12 discussions about this topic) Here...

How to set up an alias to map one directory to another destination

  1. RewriteRule ^from/(.*) to/$1

Don't forget that you will probably need to enable the rewrite engine for these rules to work. Click the button below to find out how to do this.

View More Details (and 6 discussions about this topic) Here...

301 Redirect a single URL in .htaccess to a new location

  1. Redirect 301 /old-url http://www.example.com/new-url
View More Details (and 11 discussions about this topic) Here...

Remove trailing slash (redirect to remove trailing slash) in .htaccess

If you want to redirect from http://example.com/test/ to http://example.com/test (i.e. remove the slash at the end of URLs) then you can use the following .htaccess code:

  1. RewriteCond %{REQUEST_FILENAME} !-d
  2. RewriteRule ^(.*)/$ /$1 [R=301,L]

This will 301 redirect to the non trailing slash version of the page

Don't forget that you will probably need to enable the rewrite engine for these rules to work. Click the button below to find out how to do this.

View More Details (and 7 discussions about this topic) Here...

Force trailing slash (redirect if no trailing slash exists) in .htaccess

If you want to redirect from http://example.com/test to http://example.com/test/ then you can use the following .htaccess code:

  1. RewriteCond %{REQUEST_URI} \/+[^\.]+$
  2. RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

Don't forget that you will probably need to enable the rewrite engine for these rules to work. Click the button below to find out how to do this.

View More Details (and 11 discussions about this topic) Here...

What is WordPress default Apache .htaccess rules?

Messed up your WordPress .htaccess file? Here is the default one for most configurations:

  1. # BEGIN WordPress
  2. <IfModule mod_rewrite.c>
  3. RewriteEngine On
  4. RewriteBase /
  5. RewriteRule ^index\.php$ - [L]
  6. RewriteCond %{REQUEST_FILENAME} !-f
  7. RewriteCond %{REQUEST_FILENAME} !-d
  8. RewriteRule . /index.php [L]
  9. <\/IfModule>
  10.  
  11. # END WordPress
View More Details (and 9 discussions about this topic) Here...