Welcome to web 2.0 Blog

The Last news of the web Development from the world master's

The Definitive Guide to htaccess Techniques: Do’s and Don’ts

Posted by web2.0 Design resourse Sunday, August 30, 2009

Of all the elements of web design and coding, htaccess can be one of the most intimidating. After all, it’s an incredibly powerful tool and one that has the potential to completely break your site if you’re not careful.
Below are a dozen basic htaccess techniques and tips to get you started. They’re not nearly as intimidating as many people expect, and if you study the code for a few minutes, I’m sure you’ll quickly grasp exactly how they work and why.
After that are a few bewares and don’ts for working with htaccess to help keep you out of trouble, and some more resources for further working with htaccess.
htaccess Techniques
Create a custom error page.

.htaccess on a Linux Apache server makes it easy to create your own custom error pages. Just create your custom error page files and then add this code to your .htaccess file:
 HTML:
ErrorDocument 401 /401.php  
ErrorDocument 403 /403.php  
ErrorDocument 404 /404.php 
ErrorDocument 500 /500.php  
(Obviously you should replace the “/500.php” or whatever with your own file path and name.)

2. Prevent directory browsing.

If you don’t include an index file in a directory, visitors can browse the directory itself. But preventing that is as easy as adding a single line to your .htaccess file:

 HTML:
Options All -Indexes
3. Set the default page of each directory.

If you don’t want to use an index page in each directory, you can set the default page visited when someone reaches (like an about page or a page offering the newest content) that directory by adding this:
 HTML:
DirectoryIndex news.html
(And of course you’d replace the “news.html” bit with whatever you want to use as the default.)

4. Set up a 301 redirect.

If you move around the structure of your site and need to redirect some old URLs to their new locations, the following bit of code will do so for you:
كود بلغة HTML:
Redirect 301 /original/filename.html http://domain.com/updated/filename.html  
5. Compress file output with GZIP.

You can add the following code to your htaccess file to compress all of your JavaScript, CSS and HTML files using GZIP.
 HTML:
 

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.*  

  





6. Redirect to a secure https connection

If you want to redirect your entire site to a secure https connection, use the following:
 HTML:
RewriteEngine On
RewriteCond %{HTTPS} !on  
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
7. Block script execution.

You can stop scripts in certain languages from running with this:

 HTML:
Options -ExecCGI 
AddHandler cgi-script .pl .py .php .jsp. htm .shtml .sh .asp .cgi
Just replace the types of scripts you want to block

0 comments

Post a Comment

counter

free counters