解释一下这个 .htaccess 片段

发布于 2024-10-22 02:21:48 字数 915 浏览 2 评论 0原文

有人可以解释以下 htacess 行,我理解部分内容,但想要更深入的知识。作为注释,我假设它按预期工作,这目前还没有上线,我只是在阅读一些工作簿,这是印刷的。

// Don't understand this line 
Options -Multiviews 

// Don't understand this line
Options +FollowSymLinks

// Understand this line
RewriteEngine On

// Don't ~fully~ understand this line, esp. in context
RewriteBase /portfolio

// Don't ~fully~ understand this line
// I understand that its asking if the filename is a valid file or dir
// but is it overall saying if valid file or valid dir perform rewrite?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

// Don't understand, $1 is the STRING, and the rest the condition, but
// how do you read this condition?
RewriteCond $1 !^(index\.php|images|robots\.txt|css)

// Don't understand (but do understand the RewriteRule PATTERN REPLACE, but is it
// saying replace 'all' with index.php/all ?
RewriteRule ^(.*)$ index.php?/$1

Can someone explain the following htacess lines, I understand parts, but would like a deeper knowledge. As a note I assumes it works as intended, this is not currently live, I am just reading through some workbooks and this was printed.

// Don't understand this line 
Options -Multiviews 

// Don't understand this line
Options +FollowSymLinks

// Understand this line
RewriteEngine On

// Don't ~fully~ understand this line, esp. in context
RewriteBase /portfolio

// Don't ~fully~ understand this line
// I understand that its asking if the filename is a valid file or dir
// but is it overall saying if valid file or valid dir perform rewrite?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

// Don't understand, $1 is the STRING, and the rest the condition, but
// how do you read this condition?
RewriteCond $1 !^(index\.php|images|robots\.txt|css)

// Don't understand (but do understand the RewriteRule PATTERN REPLACE, but is it
// saying replace 'all' with index.php/all ?
RewriteRule ^(.*)$ index.php?/$1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2024-10-29 02:21:48
Options -Multiviews 

这会禁用多视图 Apache 选项。基本上,该选项允许服务器根据客户端接受的内容类型和语言使用不同的文件名在同一目录中查找内容。在这种情况下,该指令只是被禁用,以确保 Apache 不会提供任何意外的文件。

多视图支持内容协商,对此进行了解释:http://httpd.apache.org/docs/current/ content-negotiation.html

Options +FollowSymLinks

这可确保启用 FollowSymLinks 选项。此设置允许 Apache 跟踪目录中存在的符号文件链接(如果存在)。如果存在符号文件链接,使文件实际存在于服务器上的其他位置而不是所请求的位置,则存在此设置。

更长的解释位于: http://www.maxi-pedia.com/FollowSymLinks

RewriteBase /portfolio

此设置用于定义基本路径重写引擎使用的 url。当重写引擎重写 .htaccess 中的 url 时,它会删除当前目录的路径。 url重写完成后,会根据当前文件目录添加回来。但是,有时请求的 url 与服务器本身的目录结构的路径不同。 RewriteBase 告诉重写引擎当前目录的 URL 路径是什么。例如,在这种情况下,文件可能存储在 /foo/bar 中,但可以通过浏览器作为 www.example.com/portfolio 访问它们。 RewriteBase 告诉引擎将 /portfolio 添加到 url,而不是 /foo/bar

有关完整说明,请参阅:http://httpd.apache.org/docs/current/mod/ mod_rewrite.html#rewritebase(该 URL 还包含对 .htaccess 的其他重写部分的说明)。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

这些行确保任何实际存在的文件或目录的 url 都不会被重写。条件之前的 ! 为否定。因此,这两个条件应理解为 ifNotFile AND ifNotDirectory

RewriteCond $1 !^(index\.php|images|robots\.txt|css)

这里的$1指的是实际重写的子模式捕获1。换句话说,就是RewriteRule中(.*)捕获的部分。基本上,此规则只是检查 RewriteRule 不会重写任何以“index.php”、“images”、“robots.txt”或“css”开头的 url。

RewriteRule ^(.*)$ index.php?/$1

这只是告诉重写引擎任何请求(当然,不会被重写条件阻止)都应该重写为 index.php? ,并在其后跟随实际请求。就像你说的,请求foo/bar将被转发到index.php?foo/bar。重点是允许 index.php 处理文件请求(可以通过 $_SERVER['QUERY_STRING'] 访问它们),这是 CMS 系统和框架中非常常见的做法。

我希望这些解释能有所帮助。我对所有这些指令没有丰富的经验,因此可能存在轻微的不准确之处,如果有,请发表评论。

Options -Multiviews 

This disables the Multiviews Apache option. Basically, the option allows the server to look for content in the same directory using different file names based on the content types and languages accepted by the client. The directive is just disabled in this case to make sure Apache doesn't serve any unexpected files.

Multiviews enables content negotiation, which is explained at: http://httpd.apache.org/docs/current/content-negotiation.html

Options +FollowSymLinks

This makes sure the FollowSymLinks option is enabled. This setting allows Apache to follow symbolic file links in the directory if they exist. This setting exists in case there are symbolic file links to make files physically exist elsewhere on the server than what is requested.

Longer explanation at: http://www.maxi-pedia.com/FollowSymLinks

RewriteBase /portfolio

This setting is for defining the base path for the url used by the rewrite engine. When the rewrite engine rewrites the url in .htaccess, it strips away the path to the current directory. Once the url rewriting is complete, it will add it back based on the current file directory. However, sometimes the url that is requested does not have the same path as the directory structure on the server itself. The RewriteBase tells the rewritengine what the URL path is to the current directory. In this case, for example, the files may be stored in /foo/bar, but they are accessed via the browser as www.example.com/portfolio. The RewriteBase tells the engine to add /portfolio to the url, instead of /foo/bar.

For complete explanation, see: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase (the url also contains explanations to the other Rewrite parts of the .htaccess).

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

These lines make sure that any url that is an actual existing file or directory wont get rewritten. The ! before the condition is negation. So these two conditions should be read as ifNotFile AND ifNotDirectory.

RewriteCond $1 !^(index\.php|images|robots\.txt|css)

The $1 here refers to the sub pattern capture 1 of the actual rewrite. In other words, it means the part captured by (.*) in the RewriteRule. Basically, this rule simply checks that the RewriteRule wont rewrite any url that starts with "index.php", "images", "robots.txt" or "css".

RewriteRule ^(.*)$ index.php?/$1

This simply tells the rewrite engine that any request (that isn't prevented by the rewrite conditions, of course) should be rewritten to index.php? with the actual request following it. Just like you said, a request foo/bar will be forwarded to index.php?foo/bar. The point is to allow index.php to handle the file requests (which can access them via $_SERVER['QUERY_STRING']), which is very common practice in CMS systems and frameworks.

I hope these explanations will help. I don't have extensive experience on all these directives, so slight inaccuracies may exist, please comment if so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文