Chirpsuite Community

Find answers, ask questions, and connect with our
community around the world.

Home Community Websites & Blogs A collection of htaccess rules to do cool things on your website


  • A collection of htaccess rules to do cool things on your website

     Chirpsuite updated 2 months ago 1 Member · 1 Post
  • Chirpsuite

    October 1, 2021 at 11:48 am

    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 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 ^(.*)$$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 (.*)$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 /

    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} ^ [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 but the url appears as

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

    6. Redirect all error 404 pages to home page

    ErrorDocument 404

    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} ^
    RewriteRule (.*)$1 [R=301,L]

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

    RewriteCond %{HTTP_HOST} ^
    RewriteRule (.*)$1 [R=301,L]

    13. Rewrite a url such that https://example/1.html is shown as

    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/
      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/ "access plus 1 month"
    # CSS and JavaScript
      ExpiresByType text/css                  "access plus 1 year"
      ExpiresByType application/javascript    "access plus 1 year"

    Generic .htaccess redirect www to non-www

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

    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]

Viewing 1 of 1 replies

Original Post
0 of 0 posts June 2018