关于此 htaccess 文件的四个具体问题

发布于 2024-10-21 05:36:41 字数 1723 浏览 2 评论 0原文

#+FollowSymLinks must be enabled for any rules to work, this is a security 
#requirement of the rewrite engine. Normally it's enabled in the root and you 
#shouldn't have to add it, but it doesn't hurt to do so.
Options +FollowSymlinks
#Apache scans all incoming URL requests, checks for matches in our .htaccess file 
#and rewrites those matching URLs to whatever we specify.
#to enable runtime rewriting engine
RewriteEngine On

#this tells mod_rewrite to leave the URL unchanged (the dash part -) and quit 
#other rewwrite processing rules if the requested segment has one of those 
#prefixes (that's what we asking when we use the ^ sign), on the list. If the 
#prefix is in the list, all subsequent Rewrite Rules are skipped.
#So to avoid some files OR directories to be redirected we use:
RewriteRule ^(somefile|somedir|someotherfile) – [L]

1) 在这些之前的 RewriteRule 之前我们不需要一个条件吗?

#this will allow direct linkage to this extensions, regardless the case sensitive.
RewriteCond %{REQUEST_FILENAME} !\.(js|ico|zip|rar|mov|mpeg4|mp4|gif|jpg|png|css|doc|pdf|docx|wmv|mpeg|avi|mpg|flv|ppt|pptx|txt)$ - [NC]

2) [NC] 标志会处理这里的所有大小写变化吗?

#if the request uri has not public...
RewriteCond %{REQUEST_URI} !^/public/.*$
#rewrite to public something...
RewriteRule ^(.*)$ /public/$1

3) 为什么我们的 RewriteRule 上有 $1 而不是 /public/index.php 吗?

#rewrite all requests that start with public, to public/index.php and if that's 
#the case, don't run any other rule.
RewriteRule ^public/.*$ /public/index.php [NC,L]

4) 因为这是最后一条规则,我们可以删除 L 标志吗?

#+FollowSymLinks must be enabled for any rules to work, this is a security 
#requirement of the rewrite engine. Normally it's enabled in the root and you 
#shouldn't have to add it, but it doesn't hurt to do so.
Options +FollowSymlinks
#Apache scans all incoming URL requests, checks for matches in our .htaccess file 
#and rewrites those matching URLs to whatever we specify.
#to enable runtime rewriting engine
RewriteEngine On

#this tells mod_rewrite to leave the URL unchanged (the dash part -) and quit 
#other rewwrite processing rules if the requested segment has one of those 
#prefixes (that's what we asking when we use the ^ sign), on the list. If the 
#prefix is in the list, all subsequent Rewrite Rules are skipped.
#So to avoid some files OR directories to be redirected we use:
RewriteRule ^(somefile|somedir|someotherfile) – [L]

1) Don't we need a condition here before these previous RewriteRule ?

#this will allow direct linkage to this extensions, regardless the case sensitive.
RewriteCond %{REQUEST_FILENAME} !\.(js|ico|zip|rar|mov|mpeg4|mp4|gif|jpg|png|css|doc|pdf|docx|wmv|mpeg|avi|mpg|flv|ppt|pptx|txt)$ - [NC]

2) Will the [NC] flag deal with all case variations here?

#if the request uri has not public...
RewriteCond %{REQUEST_URI} !^/public/.*$
#rewrite to public something...
RewriteRule ^(.*)$ /public/$1

3) Why do we have $1 and not /public/index.php on our RewriteRule?

#rewrite all requests that start with public, to public/index.php and if that's 
#the case, don't run any other rule.
RewriteRule ^public/.*$ /public/index.php [NC,L]

4) since this is the last rule, can we remove the L flag ?

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

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

发布评论

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

评论(1

允世 2024-10-28 05:36:41
  1. 不,任何以 somefilesomedirsomeotherfile 开头的内容都不会被重写 (-)。 [L] 确保不会处理更多规则(包括此规则)。
  2. 是的。
  3. 因为 RewriteCond 也可以匹配 /public/otherthing,而不仅仅是 /public/index.php
  4. 不行,没有[L],会出现死循环,因为重写的URL /public/index.php 匹配 `^public.*$
  1. No, anything that starts with somefile, somedir or someotherfile will not be rewritten (-). [L] ensures that further rules (including this one) will not be processed.
  2. Yes.
  3. Because the RewriteCond can match /public/otherthing as well, not just /public/index.php.
  4. No, without [L], an infinite loop will occur because the rewritten URL /public/index.php matches `^public.*$.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文