


Apache related mod_rewrite,RewriteCond,{HTTP_HOST}_PHP tutorial
1.给子域名加www标记
<span RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.<span com$ [NC] RewriteCond </span>%{HTTP_HOST} !^www\.<span [NC] RewriteRule </span>.? http:<span //</span><span www.%1example.com%{REQUEST_URI} [R=301,L] </span> 这个规则抓取二级域名的%1变量,如果不是以www开始,那么就加www,以前的域名以及{REQUEST_URI}会跟在其后。</span>
2.去掉域名中的www标记
<span RewriteCond %{HTTP_HOST} !^example\.<span com$ [NC] RewriteRule </span>.? http:<span //</span><span example.com%{REQUEST_URI} [R=301,L] </span></span>
3.去掉www标记,但是保存子域名
<span RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?example\.<span com)$ [NC] RewriteRule </span>.? http:<span //</span><span %1%{REQUEST_URI} [R=301,L] </span> 这里,当匹配到1%变量以后,子域名才会在%2(内部原子)中抓取到,而我们需要的正是这个%1变量。 </span>
4.防止图片盗链
<span <span 一些站长不择手段的将你的图片盗链在他们网站上,耗费你的带宽。你可以加一下代码阻止这种行为。 RewriteCond </span>%{HTTP_REFERER} !^<span $ RewriteCond </span>%{HTTP_REFERER} !^http:<span //</span><span (www\.)?example\.com/ [NC] </span> RewriteRule \.(gif|jpg|png)$ -<span [F] 如果{HTTP_REFERER}值不为空,或者不是来自你自己的域名,这个规则用[F]FLAG阻止以gif</span>|jpg|<span png 结尾的URL 如果对这种盗链你是坚决鄙视的,你还可以改变图片,让访问盗链网站的用户知道该网站正在盗用你的图片。 RewriteCond </span>%{HTTP_REFERER} !^<span $ RewriteCond </span>%{HTTP_REFERER} !^http:<span //</span><span (www\.)?example\.com/.*$ [NC] </span> RewriteRule \.(gif|jpg|png)$ 你的图片地址 [R=301,<span L] 除了阻止图片盗链链接,以上规则将其盗链的图片全部替换成了你设置的图片。 你还可以阻止特定域名盗链你的图片: RewriteCond </span>%{HTTP_REFERER} !^http:<span //</span><span (www\.)?leech_site\.com/ [NC] </span> RewriteRule \.(gif|jpg|png)$ - [F,<span L] 这个规则将阻止域名黑名单上所有的图片链接请求。 当然以上这些规则都是以{HTTP_REFERER}获取域名为基础的,如果你想改用成IP地址,用{REMOTE_ADDR}就可以了。</span></span>
5.如果文件不存在重定向到404页面
<span <span 如果你的主机没有提供404页面重定向服务,那么我们自己创建。 RewriteCond </span>%{REQUEST_FILENAME} !-<span f RewriteCond </span>%{REQUEST_FILENAME} !-<span d RewriteRule </span>.? /404.<span php [L] 这里</span>-f匹配的是存在的文件名,-d匹配的存在的路径名。这段代码在进行404重定向之前,会判断你的文件名以及路径名是否存在。你还可以在404页面上加一个?url=<span $1参数: RewriteRule </span>^/?(.*)$ /404.php?url=$1<span [L] 这样,你的404页面就可以做一些其他的事情,例如默认信心,发一个邮件提醒,加一个搜索,等等。</span></span>
6.重命名目录
<span <span 如果你想在网站上重命名目录,试试这个: RewriteRule </span>^/?old_directory/([a-z/.]+)$ new_directory/$1 [R=301,<span L] 在规则里我添加了一个“</span>.”(注意不是代表得所有字符,前面有转义符)来匹配文件的后缀名。 </span>
7.将.html后缀名转换成.php
<span 前提是.<span html文件能继续访问的情况下,更新你的网站链接。 RewriteRule </span>^/?([a-z/]+)\.html$ $1.<span php [L] 这不是一个网页重定向,所以访问者是不可见的。让他作为一个永久重定向(可见的),将FLAG修改[R</span>=301,L]。 </span>
8.创建无文件后缀名链接
<span 如果你想使你的PHP网站的链接更加简洁易记-或者隐藏文件的后缀名,试试这个:<span RewriteRule </span>^/?([a-z]+)$ $1.<span php [L] 如果网站混有PHP以及HTML文件,你可以用RewriteCond先判断该后缀的文件是否存在,然后进行替换: RewriteCond </span>%{REQUEST_FILENAME}.php -<span f RewriteRule </span>^/?([a-zA-Z0-9]+)$ $1.<span php [L] RewriteCond </span>%{REQUEST_FILENAME}.html -<span f RewriteRule </span>^/?([a-zA-Z0-9]+)$ $1.<span html [L] 如果文件是以</span>.php为后缀,这条规则将被执行。</span>
9.检查查询变量里的特定参数
<span <span 如果在URL里面有一个特殊的参数,你可用RewriteCond鉴别其是否存在: RewriteCond </span>%{QUERY_STRING} !uniquekey=<span RewriteRule </span>^/?script_that_requires_uniquekey\.php$ other_script.php [QSA,<span L] 以上规则将检查{QUERY_STRING}里面的uniquekey参数是否存在,如果{REQUEST_URI}值为script_that_requires_uniquekey,将会定向到新的URL。 </span></span>
10.删除查询变量
<span <span Apache的mod_rewrite模块会自动辨识查询变量,除非你做了以下改动: a)</span>.分配一个新的查询参数(你可以用[QSA,<span L]FLAG保存最初的查询变量) b)</span>.在文件名后面加一个“?”(比如index.php?)。符号“?”不会在浏览器的地址栏里显示。</span>
11.用新的格式展示当前URI
<span 如果这就是我们当前正在运行的URLs:/index.php?id=nnnn。我们非常希望将其更改成/nnnn并且让搜索引擎以新格式展现。首先,我们为了让搜索引擎更新成新的,得将旧的URLs重定向到新的格式,但是,我们还得保证以前的index.<span php照样能够运行。是不是被我搞迷糊了? 实现以上功能,诀窍就在于在查询变量中加了一个访问者看不到的标记符“marker”。我们只将查询变量中没有出现“marker”标记的链接进行重定向,然后将原有的链接替换成新的格式,并且通过[QSA]FLAG在已有的参数加一个“marker”标记。以下为实现的方式: RewriteCond </span>%{QUERY_STRING} !<span marker RewriteCond </span>%{QUERY_STRING} id=([-a-zA-Z0-9_+]+<span ) RewriteRule </span>^/?index\.php$ %1? [R=301,<span L] RewriteRule </span>^/?([-a-zA-Z0-9_+]+)$ index.php?marker &id=$1<span [L] 这里,原先的URL:http</span>:<span //</span><span www.example.com/index.php?id=nnnn,不包含marker,所以被第一个规则永久重定向到http://www.example.com/nnnn,第二个规则将http://www.example.com/nnnn反定向到http://www.example.com/index.php?marker&id=nnnn,并且加了marker以及id=nnnn两个变量,最后mod_rewrite就开始进行处理过程。</span> 第二次匹配,marker被匹配,所以忽略第一条规则,这里有一个“.”字符会出现在http:<span //</span><span www.example.com/index.php?marker &id=nnnn中,所以第二条规则也会被忽略,这样我们就完成了。</span> 注意,这个解决方案要求Apache的一些扩展功能,所以如果你的网站放于在共享主机中会遇到很多障碍。</span>
12.保证安全服务启用
<span <span Apache可以用两种方法辨别你是否开启了安全服务,分别引用{HTTPS}和{SERVER_PORT}变量: RewriteCond </span>%{REQUEST_URI} ^secure_page\.<span php$ RewriteCond </span>%{HTTPS} !<span on RewriteRule </span>^/?(secure_page\.php)$ https:<span //</span><span www.example.com/$1 [R=301,L] </span> 以上规则测试{REQUEST_URI}值是否等于我们的安全页代码,并且{HTTPS}不等于on。如果这两个条件同时满足,请求将被重定向到安全服务URI.<span 另外你可用{SERVER_PORT}做同样的测试,443是常用的安全服务端口 RewriteCond </span>%{REQUEST_URI} ^secure_page\.<span php$ RewriteCond </span>%{SERVER_PORT} !^443<span $ RewriteRule </span>^/?(secure_page\.php)$ https:<span //</span><span www.example.com/$1 [R=301,L]</span></span>
13.在特定的页面上强制执行安全服务
<span <span 遇到同一个服务器根目录下分别有一个安全服务域名和一个非安全服务域名,所以你就需要用RewriteCond 判断安全服务端口是否占用,并且只将以下列表的页面要求为安全服务: RewriteCond </span>%{SERVER_PORT} !^443<span $ RewriteRule </span>^/?(page1|page2|page3|page4|page5)$ https:<span //</span><span www.example.com/%1[R=301,L] </span> <span 以下是怎样将没有设置成安全服务的页面返回到80端口: RewriteCond </span>%{ SERVER_PORT } ^443<span $ RewriteRule </span>!^/?(page6|page7|page8|page9)<span $http</span>:<span //</span><span www.example.com%{REQUEST_URI} [R=301,L]<br /><br /> <br /></span></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

AI Hentai Generator
Generate AI Hentai for free.

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



One of the standout features ofCyberpunk 2077is path tracing, but it can put a heavy toll on performance. Even systems with reasonably capable graphics cards, such as the RTX 4080 (Gigabyte AERO OC curr. $949.99 on Amazon), struggle to offer a stable

Earlier this month, a YouTuber named HowToMen showcased the Rabbit R1 running Android. With this mod, the device that was originally meant to be less distracting than a phone got to function like one. That isn't a bad thing, though, as Rabbit didn't

The Asus ROG Ally features a 40 Wh battery, which typically results in about two hours of runtime. On the Ally X, however, Asus upgraded the battery to 80 Wh, pushing the battery life to around four hours, depending on the TDP. iFixit came up with a

Concurrency testing and debugging Concurrency testing and debugging in Java concurrent programming are crucial and the following techniques are available: Concurrency testing: Unit testing: Isolate and test a single concurrent task. Integration testing: testing the interaction between multiple concurrent tasks. Load testing: Evaluate an application's performance and scalability under heavy load. Concurrency Debugging: Breakpoints: Pause thread execution and inspect variables or execute code. Logging: Record thread events and status. Stack trace: Identify the source of the exception. Visualization tools: Monitor thread activity and resource usage.

1. Background of the Construction of 58 Portraits Platform First of all, I would like to share with you the background of the construction of the 58 Portrait Platform. 1. The traditional thinking of the traditional profiling platform is no longer enough. Building a user profiling platform relies on data warehouse modeling capabilities to integrate data from multiple business lines to build accurate user portraits; it also requires data mining to understand user behavior, interests and needs, and provide algorithms. side capabilities; finally, it also needs to have data platform capabilities to efficiently store, query and share user profile data and provide profile services. The main difference between a self-built business profiling platform and a middle-office profiling platform is that the self-built profiling platform serves a single business line and can be customized on demand; the mid-office platform serves multiple business lines, has complex modeling, and provides more general capabilities. 2.58 User portraits of the background of Zhongtai portrait construction

How to implement HTTP streaming in C++? Create an SSL stream socket using Boost.Asio and the asiohttps client library. Connect to the server and send an HTTP request. Receive HTTP response headers and print them. Receives the HTTP response body and prints it.

1. Choose an available domain name: The corporate email domain name should be related to the corporate brand or business. 2. Check whether the domain name has been registered: After selecting the corporate email domain name, you need to check whether the domain name has been registered by other companies. 3. Choose an email service provider: Enterprises can search for different email service providers through search engines and choose a provider based on their needs. 4. Create an account: The process of registering for an email service may be slightly different, but you usually need to fill in the basic information of the company and administrator and create an administrator account.

The top ten trading digital currency apps in the currency circle: Binance, OKX, Gate.io, Bitget, Huobi, Bybit, KuCoin, MEXC, Poloniex, BitMart. Among them, the four major trading apps in the currency circle are: Binance, OKX, Gate.io, and Bitget, which provide a wide range of cryptocurrency options, low transaction fees, a powerful trading platform and advanced trading functions.
