Home php教程 php手册 htaccess 防止盗链,防止目录浏览等10大技巧

htaccess 防止盗链,防止目录浏览等10大技巧

Jun 13, 2016 am 10:48 AM
htaccess content picture big storage Skill Browse Table of contents Own chain prevent

1. 反盗链
那些盗用了你的内容,还不愿意自己存储图片的网站是无耻的。你可以通过以下配置来放置别人盗用你的图片:
 
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. 防止目录浏览
有时候目录浏览是有用的,但大部分情况会有安全问题。为了让你的网站更安全,你可以通过htaccess文件来禁用这个功能:
 
1 Options All -Indexes


3. SEO友好的301永久重定向
这一招是我常用的。每次我更改网站URL结构的时候,我都会做301重定向:
 
1 Redirect 301 http://www.yoursite.com/article.html http://www.yoursite.com/archives/article
4. 显示个性化的 404 错误页面
当用户访问了一个不存在的页面的时候,网页服务器会显示“404 file not found”错误。有很多CMS可以让你设置自定义的错误页面,但最简单的方法是更改htaccess:
 
1 ErrorDocument 404 /404.html


5. 设置目录的默认页面
假如你需要为不同的目录设置不同的默认页面,你可以很容易的通过 .htaccess 实现:
 
1 DirectoryIndex about.html
6. 基于referer来限制网站访问
站长通常不会限制网站访问,但是当你发现有一些网站尽给你带来垃圾流量的话,你就应该屏蔽他们:
 

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

7. 限制PHP上传文件大小
这招在共享空间的服务器上很有用,可以让我的用户上传更大的文件。第一个是设置最大的上传文件大小,第二个是设置最大的POST请求大小,第三个PHP脚本最长的执行时间,最后一个是脚本解析上传文件的最长时间:
 
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. 压缩文件
你可以通过压缩文件来减少网络流量,也页面装载时间:
 
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. 缓存文件
这一点还需要解释吗?
 

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

10. 添加尾部的反斜杠
我并不确定,但是很多文章,很多人都说添加尾部反斜杠有益于SEO:
 

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

 

摘自  PainsOnline的专栏 
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Win11 Tips Sharing: Skip Microsoft Account Login with One Trick Win11 Tips Sharing: Skip Microsoft Account Login with One Trick Mar 27, 2024 pm 02:57 PM

Win11 Tips Sharing: One trick to skip Microsoft account login Windows 11 is the latest operating system launched by Microsoft, with a new design style and many practical functions. However, for some users, having to log in to their Microsoft account every time they boot up the system can be a bit annoying. If you are one of them, you might as well try the following tips, which will allow you to skip logging in with a Microsoft account and enter the desktop interface directly. First, we need to create a local account in the system to log in instead of a Microsoft account. The advantage of doing this is

What are the tips for novices to create forms? What are the tips for novices to create forms? Mar 21, 2024 am 09:11 AM

We often create and edit tables in excel, but as a novice who has just come into contact with the software, how to use excel to create tables is not as easy as it is for us. Below, we will conduct some drills on some steps of table creation that novices, that is, beginners, need to master. We hope it will be helpful to those in need. A sample form for beginners is shown below: Let’s see how to complete it! 1. There are two methods to create a new excel document. You can right-click the mouse on a blank location on the [Desktop] - [New] - [xls] file. You can also [Start]-[All Programs]-[Microsoft Office]-[Microsoft Excel 20**] 2. Double-click our new ex

A must-have for veterans: Tips and precautions for * and & in C language A must-have for veterans: Tips and precautions for * and & in C language Apr 04, 2024 am 08:21 AM

In C language, it represents a pointer, which stores the address of other variables; & represents the address operator, which returns the memory address of a variable. Tips for using pointers include defining pointers, dereferencing pointers, and ensuring that pointers point to valid addresses; tips for using address operators & include obtaining variable addresses, and returning the address of the first element of the array when obtaining the address of an array element. A practical example demonstrating the use of pointer and address operators to reverse a string.

VSCode Getting Started Guide: A must-read for beginners to quickly master usage skills! VSCode Getting Started Guide: A must-read for beginners to quickly master usage skills! Mar 26, 2024 am 08:21 AM

VSCode (Visual Studio Code) is an open source code editor developed by Microsoft. It has powerful functions and rich plug-in support, making it one of the preferred tools for developers. This article will provide an introductory guide for beginners to help them quickly master the skills of using VSCode. In this article, we will introduce how to install VSCode, basic editing operations, shortcut keys, plug-in installation, etc., and provide readers with specific code examples. 1. Install VSCode first, we need

Git installation process on Ubuntu Git installation process on Ubuntu Mar 20, 2024 pm 04:51 PM

Git is a fast, reliable, and adaptable distributed version control system. It is designed to support distributed, non-linear workflows, making it ideal for software development teams of all sizes. Each Git working directory is an independent repository with a complete history of all changes and the ability to track versions even without network access or a central server. GitHub is a Git repository hosted on the cloud that provides all the features of distributed revision control. GitHub is a Git repository hosted on the cloud. Unlike Git which is a CLI tool, GitHub has a web-based graphical user interface. It is used for version control, which involves collaborating with other developers and tracking changes to scripts and

Win11 Tricks Revealed: How to Bypass Microsoft Account Login Win11 Tricks Revealed: How to Bypass Microsoft Account Login Mar 27, 2024 pm 07:57 PM

Win11 tricks revealed: How to bypass Microsoft account login Recently, Microsoft launched a new operating system Windows11, which has attracted widespread attention. Compared with previous versions, Windows 11 has made many new adjustments in terms of interface design and functional improvements, but it has also caused some controversy. The most eye-catching point is that it forces users to log in to the system with a Microsoft account. For some users, they may be more accustomed to logging in with a local account and are unwilling to bind their personal information to a Microsoft account.

PHP programming skills: How to jump to the web page within 3 seconds PHP programming skills: How to jump to the web page within 3 seconds Mar 24, 2024 am 09:18 AM

Title: PHP Programming Tips: How to Jump to a Web Page within 3 Seconds In web development, we often encounter situations where we need to automatically jump to another page within a certain period of time. This article will introduce how to use PHP to implement programming techniques to jump to a page within 3 seconds, and provide specific code examples. First of all, the basic principle of page jump is realized through the Location field in the HTTP response header. By setting this field, the browser can automatically jump to the specified page. Below is a simple example demonstrating how to use P

How to read the catalog when reading on WeChat How to view the catalog How to read the catalog when reading on WeChat How to view the catalog Mar 30, 2024 pm 05:56 PM

The mobile version of WeChat Reading App is a very good reading software. This software provides a lot of books. You can read them anytime, anywhere with just one click to search and read them online. All of them are officially authorized and different types of books are neatly arranged. Sort and enjoy a comfortable and relaxing reading atmosphere. Switch the reading modes of different scenarios, update the latest book chapters continuously every day, support online login from multiple devices, and batch download to the bookshelf. You can read it with or without the Internet, so that everyone can discover more knowledge from it. Now the editor details it online Promote the method of viewing the catalog for WeChat reading partners. 1. Open the book you want to view the catalog and click in the middle of the book. 2. Click the three lines icon in the lower left corner. 3. In the pop-up window, view the book catalog

See all articles