分享14个有用的.htaccess代码段和技巧
htaccess的 是一个文件,每一个网络管理员应该知道和了解。 ,它控制着访问到您的网站目录。 但仍然有很多,你应该知道的的,在这篇文章中的片段会告诉你。如何来做才能更好的让网站性能发挥到最佳 1。 控制对文件和目录的访问 密码保护是一回事,但有时你可
<span><span>htaccess的</span></span>
是一个文件,每一个网络管理员应该知道和了解。,它控制着访问到您的网站目录。但仍然有很多,你应该知道的的,在这篇文章中的片段会告诉你。如何来做才能更好的让网站性能发挥到最佳
1。控制对文件和目录的访问
密码保护是一回事,但有时你可能需要完全阻止用户访问一个特定的文件或目录的选项。这通常发生在系统文件夹,如<span><span>包括</span></span>
文件夹,应用程序需要访问,但没有用户会以往任何时候都需要的特权。
要做到这一点,粘贴此代码到<span><span>的。htaccess</span></span>
文件,并把它的目录中:
deny from all
然而,这将阻止访问到每个人,包括你自己。授予自己访问,你需要指定您的IP地址。下面是代码:
<span>order deny,allow deny from all allow from xxx.xxx.xxx.xxx</span>
<span><span>xxx.xxx.xxx.xxx</span></span>
是你的IP。如果您要更换的最后三位数字,例如<span><span>0/12</span></span>
,这将指定范围的IP地址在同一网络内,从而节省您的麻烦,分别列出所有允许的IP地址。
如果你想阻止用户访问一个特定的文件,包括.<span><span>htaccess文件</span></span>
本身,请使用下面的代码片段:
<span><span>Files </span><span>.htaccess</span><span>></span><span> order allow,deny deny from all </span><span></span><span>Files</span><span>></span></span>
同样,如果您要允许给定的IP地址,<span><span>允许从</span></span>
列出。
如果你想阻止访问特定的文件类型,用它来代替:
<span><span>FilesMatch </span><span>".(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$"</span><span>></span><span> Order Allow,Deny Deny from all </span><span></span><span>FilesMatch</span><span>></span></span>
2。禁用目录浏览
防止目录浏览,
Options All -Indexes
但是,如果由于某种原因,你要启用目录浏览,更改为以下内容:
Options All +Indexes
3。高速化的加载时间,压缩文件
你可以压缩任何类型的文件,不仅图像。例如,要压缩HTML文件,使用这样:
AddOutputFilterByType DEFLATE text/html
要压缩文本文件,使用这样的:
AddOutputFilterByType DEFLATE text/plain
您也可以压缩的Javascript,或压缩多个文件类型使用一个命令:
<span>AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/rss+xml</span>
另外,如果你想你的JavaScript,HTML和CSS文件GZIP压缩,可以使用:
<span><span>IfModule </span><span>mod_gzip.c</span><span>></span><span> 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.* </span><span></span><span>IfModule</span><span>></span></span>
4。防止盗链保护您的网站
如果你不希望你的图片盗链,使用下面的代码:
<span>RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC] RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]</span>
上面替换yourdomain.com为你自己网站。
5。阻断来自特定域的访问者
如果你有你不欢迎来自特定域的用户,你可以禁止他们。例如,如果您的网站被列在一个地方,你不想要的流量(即成人网站,黑帽网站等),你可以为一个403 Forbidden。您需要启用<span><span>mod_rewrite的,</span></span>
:
<span><span>IfModule </span><span>mod_rewrite.c</span><span>></span><span> RewriteEngine on RewriteCond %{HTTP_REFERER} bannedurl1.com [NC,OR] RewriteCond %{HTTP_REFERER} bannedurl2.com [NC,OR] RewriteRule .* - [F] </span><span></span><span>ifModule</span><span>></span></span>
您需要更换<span><span>bannedurl1.com</span></span>
,<span><span>bannedurl2.com</span></span>
等 你想加入黑名单的域名。您可能要使用<span><span>[NC]</span></span>
标志,因为它指定了您输入的域名不区分大小写。<span><span>[F]</span></span>
标志指定要采取的行动-在这种情况下,显示403 Forbidden错误。如果你想禁止多个站点,请使用<span><span>[NC,OR]</span></span>
标志为每一个域名,但最后,如果你想禁止单个域中使用<span><span>[NC]</span></span>
标志。
6。从特定的用户代理阻断请求
如果你的日志文件显示特定的用户代理(机器人或蜘蛛),你可以添加几行字在.<span><span>htaccess文件,</span></span>
并拒绝他们的访问到您的网站:
<span>RewriteEngine On RewriteBase / SetEnvIfNoCase Referer "^$" bad_user SetEnvIfNoCase User-Agent "^badbot1" bad_user SetEnvIfNoCase User-Agent "^badbot2" bad_user SetEnvIfNoCase User-Agent "^badbot3" bad_user Deny from env=bad_user</span>
更换<span><span>badbot1</span></span>
,<span><span>badbot1</span></span>
,从日志文件中的机器人的名字。
7。缓存文件
另一种方式是通过文件缓存到您的网站的加载时间加快。下面是你需要添加以缓存文件:
<span><span>FilesMatch </span><span>".(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$"</span><span>></span><span> Header set Cache-Control "max-age=2592000" </span><span></span><span>FilesMatch</span><span>></span></span>
您可以添加更多的文件类型(或删除其中的一些),您还可以使用<span><span>的max-age</span></span>
指定的时间量,
8。禁用缓存为特定的文件类型
如果你不想缓存特定的文件类型,很容易不包括缓存中的序列。但是,有时文件可能会缓存,即使你没有明确列出他们,在这种情况下,您可能需要禁用缓存,只保留适合自己的,大多数情况下,你需要禁用缓存的动态文件,如脚本。
<span><span>FilesMatch </span><span>".(pl|php|cgi|spl|scgi|fcgi)$"</span><span>></span><span> Header unset Cache-Control </span><span></span><span>FilesMatch</span><span>></span></span>
9。绕过下载对话框
默认情况下,当您尝试从Web服务器下载文件时,你会得到一个对话,询问您是否要保存文件或打开。这种对话是尤其是在大的媒体文件或PDF文件。如果你已经上传到服务器的文件进行下载,您可以节省用户的时间和进行直接下载。以下是你所需要的<span><span>。htaccess文件</span></span>
中设置:
<span>AddType application/octet-stream .pdf AddType application/octet-stream .zip AddType application/octet-stream .mp3</span>
10。重命名<span><span>的.htaccess</span></span>
文件
如果由于某种原因,主要是与安全相关的,你要重命名<span><span>的。htaccess</span></span>
文件,这是很容易做到这一点。
AccessFileName htac.cess
您还需要更新.<span><span>htaccess文件</span></span>
中的任何项目文件本身或被提及的地方,否则,你会得到很多的错误。
11。更改默认的索引页
如果你想你的索引页是从默认<span><span>的index.html</span></span>
,<span><span>index.php文件</span></span>
,<span><span>INDEX.HTM</span></span>
等不同的东西,这是很容易做到的。
DirectoryIndex mypage.html
12。重定向到一个安全的HTTPS连接
如果您使用的是HTTPS,您希望将用户重定向到您的网站的安全网页,使用这样的:
<span>RewriteEngine On RewriteCond %{HTTPS} !on RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}</span>
13。限制在PHP文件上传限制,最大尺寸的POST数据,最大脚本执行时间,等
<span><span>htaccess文件</span></span>
允许你设置一些值,直接影响到你的PHP应用程序。例如,如果你想要强加在PHP上传限制,:
php_value upload_max_filesize 15M
您也可以在PHP上传后的最大尺寸为限制,
php_value post_max_size 10M
如果你不想脚本执行永远,你可以限制以下的执行时间:
php_value max_execution_time 240
最后,如果你想限制的脚本解析输入数据的时间,使用这样的:
php_value max_input_time 180
适合你在几秒钟内设置任何值。
14。伪装的文件类型
有时你不希望用户在您的网站上知道的文件类型的文件。。例如,您可以使您的所有文件,看起来好像他们是HTML或PHP文件:
<span>ForceType application/x-httpd-php ForceType application/x-httpd-php</span>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

1. First, we enter NetEase Cloud Music, and then click on the software homepage interface to enter the song playback interface. 2. Then in the song playback interface, find the sharing function button in the upper right corner, as shown in the red box in the figure below, click to select the sharing channel; in the sharing channel, click the "Share to" option at the bottom, and then select the first "WeChat Moments" allows you to share content to WeChat Moments.

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

Recently, Baidu Netdisk Android client has ushered in a new version 8.0.0. This version not only brings many changes, but also adds many practical functions. Among them, the most eye-catching is the enhancement of the folder sharing function. Now, users can easily invite friends to join and share important files in work and life, achieving more convenient collaboration and sharing. So how do you share the files you need to share with your friends? Below, the editor of this site will give you a detailed introduction. I hope it can help you! 1) Open Baidu Cloud APP, first click to select the relevant folder on the homepage, and then click the [...] icon in the upper right corner of the interface; (as shown below) 2) Then click [+] in the "Shared Members" column 】, and finally check all

As a programmer, I get excited about tools that simplify the coding experience. With the help of artificial intelligence tools, we can generate demo code and make necessary modifications as per the requirement. The newly introduced Copilot tool in Visual Studio Code allows us to create AI-generated code with natural language chat interactions. By explaining functionality, we can better understand the meaning of existing code. How to use Copilot to generate code? To get started, we first need to get the latest PowerPlatformTools extension. To achieve this, you need to go to the extension page, search for "PowerPlatformTool" and click the Install button

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.

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

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

Working with files in the Linux operating system requires the use of various commands and techniques that enable developers to efficiently create and execute files, code, programs, scripts, and other things. In the Linux environment, files with the extension ".a" have great importance as static libraries. These libraries play an important role in software development, allowing developers to efficiently manage and share common functionality across multiple programs. For effective software development in a Linux environment, it is crucial to understand how to create and run ".a" files. This article will introduce how to comprehensively install and configure the Linux ".a" file. Let's explore the definition, purpose, structure, and methods of creating and executing the Linux ".a" file. What is L
