Easy htaccess tricks


htaccess tricks .htaccess (hypertext access) file is a directory-level configuration file supported by several web servers, that allows for decentralized management of web server configuration. They are placed inside the web tree, and are able to override a subset of the server’s global configuration for the directory that they are in, and all sub-directories.

The original purpose of .htaccess—reflected in its name—was to allow per-directory access control, by for example requiring a password to access the content. Nowadays however, the .htaccess files can override many other configuration settings including content type and character set, CGI handlers, etc.

Redirect whole site except one directory – 301 Redirect for all pages to new site Except 1 directory


RewriteEngine on
RewriteCond %{REQUEST_URI} !^/keepthis-directory/
RewriteRule (.*) http://www.youneeditall.com/$1 [R=301,L]

If you get 500 Internal Error then double-check that you have a space between } and ! on the second line.you can even to sub and sub-sub directory

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/keepthis-directory/andthis
RewriteRule (.*) http://www.youneeditall.com/$1 [R=301,L]


keepthis-directory - replace with name of your directory you would like to keep live without redirecting
andthis - name of sub-directory that also important to keep


Permanent – 301 and temporary redirect.

THIS WILL Permanently REDIRECT ONLY HOME PAGE to NEW site:
RedirectMatch 301 ^/$ http://www.youneeditall.com/

Below rules Will redirect All pages to new site
<IfModule mod_rewrite.c>
##RewriteEngine On
## RewriteCond %{HTTP_HOST} ^domainbuyitnow.com/$ [OR]
## RewriteCond %{HTTP_HOST} ^www.domainbuyitnow.com/$
##RewriteRule (.*)$ http://www.youneeditall.com/$1 [R=301,L]
<!–IfModule>

This allows you to redirect your entire website to any other domain

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

Speedup your site rules

Compress output using GZIP

Add following snippet into your htaccess file and compress all the css, js, html files with GZip compression.

<IfModule mod_gzip.c>
    mod_gzip_on       Yes
    mod_gzip_dechunk  Yes
    mod_gzip_item_include file      \.(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler   ^cgi-script$
    mod_gzip_item_include mime      ^text/.*
    mod_gzip_item_include mime      ^application/x-javascript.*
    mod_gzip_item_exclude mime      ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
code works only if mod_gzip module is enabled in your web-server.
ExpiresActive On
ExpiresByType image/jpg “access plus 1 year”
ExpiresByType image/jpeg “access plus 1 year”
ExpiresByType image/gif “access plus 1 year”
ExpiresByType image/png “access plus 1 year”
ExpiresByType text/css “access plus 1 month”
ExpiresByType application/pdf “access plus 1 month”
ExpiresByType text/x-javascript “access plus 1 month”
ExpiresByType application/x-shockwave-flash “access plus 1 month”
ExpiresByType image/x-icon “access plus 1 year”
ExpiresDefault “access plus 1 days”# Cache Headers
<ifmodule mod_headers.c>
# Cache specified files for 31 days
<filesmatch “\.(ico|flv|jpg|jpeg|png|gif|css|swf)$”>
Header set Cache-Control “max-age=2678400, public”
<!–filesmatch>
# Cache HTML files for a couple hours
<filesmatch “\.(html|htm)$”>
Header set Cache-Control “max-age=7200, private, must-revalidate”
<!–filesmatch>
# Cache PDFs for a day
<filesmatch “\.(<span=”” class=”hiddenSpellError” pre=””>pdf)$”>
Header set Cache-Control “max-age=86400, public”
</filesmatch>
# Cache Javascripts for 31 days
<filesmatch “\.(js)$”>
Header set Cache-Control “max-age=2678400, private”
</filesmatch>
</ifmodule>

You may want to add following snippet if your webserver provides mod_deflate support.
<Location>
    SetOutputFilter DEFLATE
      SetEnvIfNoCase Request_URI  \
        \.(?:gif|jpe?g|png)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI  \
        \.(?:exe|t?gz|zip|gz2|sit|rar)$ no-gzip dont-vary
</Location>
more to come later
check this out:

One thought on “Easy htaccess tricks

  1. Pingback: Fully back in business! | The Fiction of Universal Nexus

Comments are closed.