htaccess 10 tips to prevent hotlinking and directory browsing_PHP tutorial

WBOY
Release: 2016-07-13 17:49:55
Original
781 people have browsed it

1. Anti-hotlinking
Websites that steal your content and aren't willing to store the images themselves are shameless. You can use the following configuration to prevent others from stealing your pictures:

1 RewriteBase /
2 RewriteCond %{HTTP_REFERER} !^$
3 RewriteCond %{HTTP_REFERER} !^http://(www.)?yoursite.com/.*$ [NC]
4 RewriteRule .(gif|jpg|swf|flv|png)$ /feed/ [R=302,L]
2. Prevent directory browsing
Sometimes directory browsing is useful, but in most cases there are security issues. To make your website more secure, you can disable this feature via htaccess file:

1 Options All -Indexes


3. SEO Friendly 301 Permanent Redirect
This trick is what I often use. Every time I change the URL structure of my website, I do a 301 redirect:

1 Redirect 301 http://www.yoursite.com/article.html http://www.yoursite.com/archives/article
4. Display personalized 404 error page
When a user accesses a page that does not exist, the web server will display a "404 file not found" error. There are many CMS that let you set up custom error pages, but the easiest way is to change htaccess:

1 ErrorDocument 404 /404.html


5. Set the default page of the directory
If you need to set different default pages for different directories, you can easily achieve this through .htaccess:

1 DirectoryIndex about.html
6. Restrict website access based on referer
Webmasters usually do not restrict website access, but when you find that some websites are bringing you junk traffic, you should block them:

1
2 RewriteEngine on RewriteCond %{HTTP_REFERER} spamteam.com [NC,OR]
3 RewriteCond %{HTTP_REFERER} trollteam.com [NC,OR]
4 RewriteRule .* – [F]
5

7. Limit PHP upload file size
This trick is useful on a shared space server to allow my users to upload larger files. The first is to set the maximum upload file size, the second is to set the maximum POST request size, the third is the longest execution time of the PHP script, and the last one is the longest time for the script to parse the uploaded file:

1 php_value upload_max_filesize 20M
2 php_value post_max_size 20M
3 php_value max_execution_time 200
4 php_value max_input_time 200


8. Compressed files
You can reduce network traffic and page load time by compressing files:

1 AddOutputFilterByType DEFLATE text/plain
2 AddOutputFilterByType DEFLATE text/html
3 AddOutputFilterByType DEFLATE text/xml
4 AddOutputFilterByType DEFLATE text/css
5 AddOutputFilterByType DEFLATE application/xml
6 AddOutputFilterByType DEFLATE application/xhtml+xml
7 AddOutputFilterByType DEFLATE application/rss+xml
8 AddOutputFilterByType DEFLATE application/javascript
9 AddOutputFilterByType DEFLATE application/x-javascript
9. Cache files
Does this need any explanation?

1
2 Header set Cache-Control “max-age=2592000″
3

10. Add trailing backslash
I'm not sure, but a lot of articles, a lot of people say adding a trailing backslash is good for SEO:

1
2 RewriteCond %{REQUEST_URI} /+[^.]+$
3 RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
4

Excerpted from PainsOnline’s column

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478306.htmlTechArticle1. Anti-hotlinking Websites that steal your content and are unwilling to store the pictures themselves are shameless. You can use the following configuration to prevent others from stealing your pictures: 1RewriteBase / 2Rew...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!