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](http://noupe.com/img/1092550_15746034.jpg)
.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:
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:
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:
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.
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.*
![](http://noupe.com/img/1160560_58688732.jpg)
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}
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
0 comments