解释htaccess规则? [英] Explain htaccess rules?
问题描述
在过去的一个小时中,我已经通过搜索,复制和粘贴等方式制作了.htaccess文件.
I have made this .htaccess file over the last hour through searching, copy and pasting etc.
它确实可以按照我的意愿来工作.
It does work how I want it to.
但是我不明白.
有人可以一步一步把它放下来,对我来说,这是外行的说法.
Could someone please put it down step by step what is happening here in layman's terms for me.
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
如果有任何提示,请把它们扔在那里.
And if there are any tips please throw them in there.
推荐答案
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
^www\.example\.com$
锚点^$
表示这是HTTP_HOST
中的完整字符串,之前或之后都没有.因此,如果随请求传递的域名完全匹配www.example.com
,则整个URI (.*)
都将重定向到example.com
,从而从前部剥离www.
.
^www\.example\.com$
The anchors ^$
mean this is the complete string in HTTP_HOST
, nothing before or after. Therefore, if the domain name passed with the request matches www.example.com
exactly, the entire URI (.*)
is redirected to example.com
, thereby stripping off the www.
from the front.
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
RewriteCond
的-f
标志测试第一个参数是否是实际存在的文件.在这种情况下,它通过在测试的参数上添加.php
扩展名来测试REQUEST_FILENAME
的值,该值将是URI的最后一部分(file
),例如example.com/directory/file
作为PHP文件存在.
A -f
flag to RewriteCond
tests if the first argument is a file that actually exists. In this case, it tests for the value of REQUEST_FILENAME
, which would be the last part (file
) of a URI like example.com/directory/file
exists as a PHP file, by adding a .php
extension onto the tested argument.
因此,如果file.php
实际上存在,则对不存在的file
的请求在此处用$1.php
静默重写为其相应的PHP文件.因此,如果/directory/notexists
没有相应的directory/notexists.php
文件,则将不进行重写.
So, if file.php
actually exists, the request for the non-existent file
is here rewritten silently into its corresponding PHP file with $1.php
. So if /directory/notexists
did not have a corresponding directory/notexists.php
file, it would not be rewritten.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
THE_REQUEST
包含浏览器最初发送的完整GET/POST
请求,例如GET /index.php
.因此,此处匹配的内容与上一个块相似.
THE_REQUEST
contains the complete GET/POST
request the browser originally sent, like GET /index.php
. So what has been matched here is similar to the previous block.
-
^[A-Z]{3,9}
首先与动词GET
或POST
等匹配,但不会捕获它以供重用 -
/([^\ ]+)
然后捕获/
之后直至下一个空格的所有内容,例如GET /index.php
中的index
. -
\.php
确实匹配
^[A-Z]{3,9}
first matches the verbGET
orPOST
, etc. but doesn't capture it for reuse- the
/([^\ ]+)
then captures everything following/
and up to the next whitespace, like theindex
inGET /index.php
. - The
\.php
is literally matched
好,然后下面的RewriteRule
将具有上述条件的index
捕获到%1
中,然后实际上重定向浏览器以删除.php
扩展名,因此浏览器的结束URL看起来像/index
.
Ok, then the following RewriteRule
takes that index
captured into %1
with the above condition, and actually redirects the browser to remove the .php
extension so the browser's ending URL looks like /index
.
换句话说,如果浏览器请求带有.php
扩展名的/directory/file.php
,它将把用户重定向到/directory/file
以剥离.php
.
In other words, if the browser requests /directory/file.php
with the .php
extension, it redirects the user to /directory/file
to strip off the .php
.
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
并且此匹配原始请求中包含/index
的任何内容,但不必在URI的开头.换句话说,/directory/index
将匹配,/directory/subdir/index.php
也将匹配.无论匹配什么,它都将重定向到索引部分之前的内容.让我们分解一下:
And this one matches anything containing /index
in the original request, but it need not be at the start of the URI. In other words, /directory/index
would match, as would /directory/subdir/index.php
. No matter what it matches, it is redirected to whatever comes before the index part. Let's break it down:
-
^(.*)
匹配$1
开头的所有内容
-
index.php
..在上面匹配的内容之后出现
^(.*)
matches whatever comes at the start into$1
index.php
.. comes after whatever was matched above
然后将其仅重定向到$1
组件,因此,如果/directory/subdir/index.php
这样的URL如果浏览器直接请求,将被重定向为指向更干净的URL:/directory/subdir/
,而index.php
不会出现在地址栏.
That is then redirected just to the $1
component, so a URL like /directory/subdir/index.php
if directly requested by the browser, would be redirected to point to the cleaner URL: /directory/subdir/
without the index.php
appearing in the address bar.
这篇关于解释htaccess规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!