了解这个重写规则,以便我可以安全地更改它?

时间:2013-08-11 19:22:12

标签: .htaccess

# -FrontPage-

#<Limit GET POST>
#   order deny,allow
#   deny from all
#   allow from all
#</Limit>
<Limit PUT DELETE>
    order deny,allow
    deny from all
</Limit>

AuthUserFile /home1/tenleiye/public_html/_vti_pvt/service.pwd
AuthGroupFile /home1/tenleiye/public_html/_vti_pvt/service.grp

# Use PHP5 as default
AddHandler application/x-httpd-php5 .php
AuthName tenleiyen.com
#IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Force www
    RewriteCond %{HTTP_HOST} ^tenleiyen\.com
    RewriteRule ^(.*)$ http://www.tenleiyen.com/$1 [R=permanent,L]

    # Do not change or site will be f*ked. Restrict all except the list below, redirecting all traffic to the index.php
    RewriteCond $1 !^(index\.php|1\.ico|favicon\.png|apple-touch-icon(.*)|crossdomain\.xml|robots\.txt|css|js|images|uploads|videos|download|sitemap\.xml)
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

我有一个可爱的笔记,由以前的程序员表示我无法更改权限。如果我添加某些内容甚至从RewriteCond中移除代码段,那么它将会是404。

我不能:

  1. 使用根目录
  2. 获取sitemap.xml即可阅读,
  3. 1.jpg文件放在根目录中并查看,
  4. 将我的网站站长工具.html文件放入要读取的根目录。
  5. 所有内容都转到index.php文件,我所能做的就是使用指定的文件夹。

1 个答案:

答案 0 :(得分:1)

RewriteCond %{HTTP_HOST} ^tenleiyen\.com
RewriteRule ^(.*)$ http://www.tenleiyen.com/$1 [R=permanent,L]

根据此规则,条件会验证您的域名是否以tenleiyen.com开头,如果是,则会将请求重写为http://www.tenleiyen.com/$1

$ 1代表^(.*)$所持有的值,这基本上意味着域之后的任何内容,例如http://www.tenleiyen.com/index.php

最后的2个标志表示永久重定向,也就是301重定向,L代表最后一个规则。


这个很复杂,所以我会按部分去做:

RewriteCond $1 !^(index\.php|1\.ico|favicon\.png|apple-touch-icon(.*)|crossdomain\.xml|robots\.txt|css|js|images|uploads|videos|download|sitemap\.xml)
RewriteRule ^(.*)$ /index.php/$1 [L]

$1是您在下一个RewriteRule中首先捕获的内容^(.*)$,这是域名不计算初始/后面的任何内容(更多信息)特别是按照绿线):

back-reference

因此,如果左手以任何关键字开头,那么条件的右手基本上会比较:

  • index.php或
  • 1.ico OR
  • favicon.png或
  • apple-touch-icon(之后的任何其他内容)或
  • crossdomain.xml或
  • robots.txt或
  • css或
  • js OR
  • 图片或
  • 上传或
  • 视频或
  • 下载或
  • sitemap.xml的

如果它没有以任何一个开头,那么它会将其重定向到index.php

如果它与上述任何一个匹配,那么它将直接访问。

您将可以访问:

http://yourdomain.com/sitemap.xml

但无法访问

http://yourdomain.com/test/sitemap.xml

现在这就是我认为你想要的东西:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php/$1 [L]

上述规则将验证文件,目录或符号链接是否存在,如果存在,则它将不会重定向到index.php,如果不存在,则它将重定向到index.php

相关问题