Using with .htaccess we can do following:
How to deny IP specific addresses?
# vi /home/user/public_html/.htaccess
Order allow, deny
Deny from 192.168.0.10
Deny from 212.155.
Deny from 1.2.3.4 5.6.7.8 127.0.0.1
Allow from all
How to prevent or allow directory listing?
The following line enables Directory listing.
Options +Indexes
The following disables directory listing for your web site.
Options –Indexes
With .htaccess file you can control which files to be ignored when creating a directory list.
For example:
IndexIgnore *.gif *.zip *.txt
Will make the apache server to skip all gif, zip and txt files from the directory list.
IndexIngnore *
Will just create an empty directory list.
You can use custom error pages for any error as long as you know its number (like 404 for page not found) by adding the following to your .htaccess file:
ErrorDocument errornumber /file.html
For example if I had the file notfound.html in the root direct
ory of my site and I wanted to use it for a 404 error I would use:
ErrorDocument 404 /notfound.html
Protecting a folder:
cd /home/user/public_html/
htpasswd -c .htpasswd username
the above command will create .htpasswdfile
To password protect a folder on your site, you need to put the following code in your .htaccess file:
AuthUserFile /full/path/to/.htpasswd
AuthType Basic
AuthName "My Secret Folder"
Require valid-user
Hotlink Protectection:
You can prevent the hot-linking of your images by creating a .htaccess file with the following content:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?your-domain.com/.*$ [NC]
RewriteRule \.(gif|jpe?g|png)$ - [F]
To block other type of files, just add their extension to the list above. For example to block movie files:
RewriteRule \.(mov|avi|wmv|mpe?g)$ - [F]
The Hot-Linking prevention is based on an Apache module called ModRewrite.
Redirection:
The Apache web server provides several way for setting up redirects.
The most simple one is using the “Redirect” directive:
Redirect /folder
http://www.example.com/newfolder
With such a line in your .htaccess if a visitor tries to load
http://www.example.com/folder
he will be redirected to
http://www.example.com/newfolder
You can add a status code to the Redirect directive. For example for Permanent 301 redirect you can use:
Redirect permanent /folder
http://www.example.com/newfolder
Another useful directive is the RedirectMatch. With it you can use regular expressions in the redirect condition. For example
RedirectMatch "\.html$"
http://www.example.com/index.php
This will redirect all requests to files that end with .html to the index.php file.
Introduction to mod_rewrite and some basic examples:
Create easy to remember URLs or also known as COOL URIs, other call them Search Engine Friendly URLs. For example you have some complicated site driven by some server-side scripting language: PHP, Perl, etc. In general its URLs will look like
http://www.example.com/view.php?cat=art ... 20&lang=en
It is not easy to remember such URL.
Using ModRewrite you can make the URL look like:
http://www.example.com/en/articles/10-2
Here is the code:
RewriteEngine On
RewriteRule ^([a-z]*)/([a-z]*)/([1-9]+)(-[1-9]+)? $
No comments:
Post a Comment