• Collection of useful Apache redirects, rewrites, directives and reminders

    These are a slew of rewrite and redirection commands for Apache that can be setup in .htaccess or other site configuration files. In some cases there are multiple ways of doing things that might work in one situation but not another. I have a love/hate relationship with mod_rewrite, as I think many do, but in the end it is a powerful tool that can do so much that it is indispensable.

    ** IMPORTANT – Anywhere you see RewriteXXXXX, make sure you have “RewriteEngine On” to enable it! **

    Custom error pages:

    ErrorDocument 400 /errors/400.html
    ErrorDocument 401 /errors/401.html
    ErrorDocument 403 /errors/403.html
    ErrorDocument 404 /errors/404.html
    ErrorDocument 500 /errors/500.html

    Enable compression output:

    AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4.0[678] no-gzip
    BrowserMatch bMSIE !no-gzip !gzip-only-text/html

    Redirect all requests to WWW:

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

    Redirect ALL subdomains to a new domain name:

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

    Wildcard domain rewrite for *.domain1.com to *.domain2.com:

    RewriteRule ^([A-Za-z0-9-]+)\.domain1\.com?$ https://%1.domain2.com$1

    Force HTTPS site wide:

    RewriteCond %{HTTPS} !=on
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    #RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,QSA,R=permanent]
    #RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

    Force HTTPS only on specific folder:

    RewriteCond %{HTTPS} !=on
    RewriteRule ^/?secure/(.*) https://%{SERVER_NAME}/secure/$1 [R,L]

    Remove the .php extension from files:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]

    Remove the .html extension from files:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.html [NC,L]

    Another rewrite rule for removing file extensions:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.*)$ $1.html

    Redirect all images to a new domain, useful if you moved everything to a CDN:

    RewriteRule ([^.]+\.(jpe?g|gif|bmp|png))$ http://www.newdomain.com/$1 [R=301,L,NC]

    Prevent hotlinking from ALL sites:

    RewriteCond %{HTTP_REFERER} !^http://(.+\.)?yourdomain\.com/ [NC]
    RewriteCond %{HTTP_REFERER} !^$
    RewriteRule .*\.(jpe?g|gif|bmp|png)$ /location/donothotlink.jpg [L]

    Prevent hotlinking from SPECIFIC sites:

    RewriteCond %{HTTP_REFERER} ^http://(.+\.)?blocked-site1\.com/ [NC,OR]
    RewriteCond %{HTTP_REFERER} ^http://(.+\.)?blocked-site2\.com/ [NC]
    RewriteRule .*\.(jpe?g|gif|bmp|png)$ /location/donothotlink.jpg [L]

    Add trailing slash (/) if missing:

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

    Remove trailing slash if desired:

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

    Using RedirectMatch to repoint some subfolder to the homepage (or another folder):

    RedirectMatch 301 /some/folder/ /

    Using a wildcard to remove a portion of a URL ie: domain.com/xyz/randomstring/filename to domain.com/xyz/filename

    RedirectMatch 301 /xyz/.*/filename/ /xyz/filename

    Using find and replace with RedirectMatch building on above, now you have multiple companies on top of xyz, so you need to redirect multiple companies separated by a random string – ie: you have domain.com/abc/randomstring/filename and domain.com/xyz/randomstring/filename:

    RedirectMatch 301 /(.*)/.*/filename/ /$1/filename


    Leave a reply