请为初学者解释这个.htaccess文件



我在理解如何动态地将简单的一种语言网站放在一起时遇到了一些麻烦。如果有人能用婴儿语言向我解释以下代码的每个部分的含义,我将不胜感激:

RewriteEngine On
RewriteCond %{REQUEST_URI} !.(php|css|js|gif|png|jpe?g|pdf)$
RewriteRule (.*)$ templates/index.php [L]

提前谢谢你!

# Enable RewriteEngine to rewrite URL patterns
RewriteEngine On
# Every URI that not (! operator) ends with one of .php, .css, .js, .gif, .png, .jpg, .jpeg or .pdf
RewriteCond %{REQUEST_URI} !.(php|css|js|gif|png|jpe?g|pdf)$
# Will be redirected to templates/index.php
RewriteRule (.*)$ templates/index.php [L]
# Sample
# /foo/bar.php -> /foo/bar.php
# /foo/bar.html -> templates/index.php
RewriteEngine On

打开重写引擎

RewriteCond %{REQUEST_URI} !.(php|css|js|gif|png|jpe?g|pdf)$

匹配所有以 .php、.css 等结尾的请求。

  • ! = 否定以下表达式("不匹配")
  • . = 单个点(必须转义,以便从字面上理解。如果没有反斜杠,它将匹配每个字符)
  • (php|css|js|gif|png|jpe?g|pdf) = 这些选项之一。 jpe?g表示e是可选的,因此它匹配jpgjpeg
  • $ = 请求的结束。

RewriteRule (.*)$ templates/index.php [L]

将所有与正则表达式不匹配的请求重定向到 templates/index.php[L]意味着这是最后一条规则,因此不会应用此 .htaccess 中的其他规则。

  1. 启用重写引擎

  2. RewriteCond 定义了何时启动 RewriteRule,在您的原因中,它正在检查文件扩展名(如果不是在这种情况下,因为!

  3. 当 RewriteCond 为 true 时,请求被重定向到模板/索引.php

您的htaccess重写了您网站页面的URL。

RewriteEngine On

简单地说,你的 Web 服务器 Apache 现在将打开他的重写引擎。

然后是重写规则,它有一个条件。一、条件:

RewriteCond %{REQUEST_URI} !.(php|css|js|gif|png|jpe?g|pdf)$

条件位于客户端请求的 URL 上。这意味着,如果所述 URL 不以.php、.css、.js、.gif、.png、.pdf、.jpg或.jpeg结尾,则将应用以下规则。

RewriteRule (.*)$ templates/index.php [L]

此规则意味着 URL 的末尾(可能是".literally_anything")将替换为"模板/索引.php

[L] 表示这是最后的重写规则。

更多解释在这里 : http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

最新更新