A Collection of .htaccess Rules to do Cool Things on Your Website

Here's how to redirect web pages using htaccess redirect rules. Make sure to enter the code AFTER "RewriteEngine On" directive. DO NOT REPEAT the "RewriteEngine On" directive either. Be extremely careful as any simple syntax error may cause your site not to display properly or at all.

1. Redirect an old domain to a new domain (replace https://new.site with your url).

This rule will map all the urls from the old site to the urls in the new site (unless you manually changed them, in which case you will need to do 301 redirect for every affected url)

RewriteRule ^(.*)$ https://new.site/$1 [R=301,L]

2. Redirect an old domain to a new domain but exclude a directory (folder must reference an actual directory name)

RewriteCond %{REQUEST_URI}!^/folder1/
RewriteCond %{REQUEST_URI}!^/folder2/
RewriteRule (.*) https://www.newexampledomain.com/$1 [R=301,L]

3. Redirect an old url to a new url for pages located on the same website (301 is a permanent redirect while 302 is a temporary redirect)

Redirect 301 /old-page /new-page

4. Redirect an old url to a new url on a different website

Redirect 301 /old-page https://different.site/new-page

5. Redirect a whole directory

RewriteRule ^old/(.*)$ /new/$1 [R=301,NC,L]

6. Force https on all website traffic

RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

7. Force https on a particular domain when you have multiple domains pointing to the same website.

RewriteCond %{HTTP_HOST} ^domain1.com [NC] 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

8. Force https on a particular folder within your website. Folders must be actual directory names.

RewriteCond %{HTTPS} off
RewriteRule ^(folder1|folder2) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

9. Show the content in site.com/this-folder but the url appears as site.com/that-folder

RewriteRule ^this-folder/?$ /that-folder/

6. Redirect all error 404 pages to home page

ErrorDocument 404 https://example.com

8. Rewrite all non-existing urls to the index.php file (all files and folders that do not exist will load normally and display the index page instead of an error page).

Again, if your site's index page isn't .php, just change the extension to match yours.

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

9. Prevent browsing of directory index files

Options -Indexes

10. Redirect to a specific index file (replace index.html with your preferred default handler)

DirectoryIndex index.html

11. Force www in the url (paste the code after "RewriteEngine On" directive)

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

12. Remove www in the url (paste the code after "RewriteEngine On" directive)

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

13. Rewrite a url such that https://example/1.html is shown as https://example.com/clean-seo-friendly-page.html

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9]+).html /clean-seo-friendly-page.html [QSA,L]

Interpreting #13

[0-9]+) - means any digit (and only digits) are allowed one or more times

([a-z]*) - means any lowercase letter and - for word separation allowed, 0 or more times. Use ([a-zA-Z]*) to allow uppercase letters.

[QSA,L] - appends this to your internal scripting query string, and makes it the Last rewrite rule executed.

14. Enable Gzip compression for website

  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml

  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent

# END Enable compression

15. Add expires header / cache policy

ExpiresActive on

# Perhaps better to whitelist expires rules? Perhaps.
  ExpiresDefault                          "access plus 1 month"

# Data
  ExpiresByType text/xml                  "access plus 0 seconds"
  ExpiresByType application/xml           "access plus 0 seconds"
  ExpiresByType application/json          "access plus 0 seconds"

# Favicon (cannot be renamed)
  ExpiresByType image/x-icon              "access plus 1 week"

# Media: images, video, audio
  ExpiresByType image/gif                 "access plus 1 month"
  ExpiresByType image/png                 "access plus 1 month"
  ExpiresByType image/jpeg                "access plus 1 month"
  ExpiresByType video/ogg                 "access plus 1 month"
  ExpiresByType audio/ogg                 "access plus 1 month"
  ExpiresByType video/mp4                 "access plus 1 month"
  ExpiresByType video/webm                "access plus 1 month"

# Webfonts
  ExpiresByType application/x-font-ttf    "access plus 1 month"
  ExpiresByType font/opentype             "access plus 1 month"
  ExpiresByType application/x-font-woff   "access plus 1 month"
  ExpiresByType image/svg+xml             "access plus 1 month"
  ExpiresByType application/vnd.ms-fontobject "access plus 1 month"

# CSS and JavaScript
  ExpiresByType text/css                  "access plus 1 year"
  ExpiresByType application/javascript    "access plus 1 year"

16. Generic .htaccess redirect www to non-www

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

17. If you want to separate http and https redirect rules from www to non-www

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

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

18. How to redirect and entire website without losing url structure

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newsite.com/$1 [R=301,L]

 

Enjoyed this article? Stay informed by joining our newsletter!

Comments

You must be logged in to post a comment.

Related Articles
Author

Leveraging the power of tech to influence human behavior through positive vibes. Get paid to write about tech, science and more.