使用自定义.htaccess重写规则的意外行为 [英] Unexpected behavior with custom .htaccess rewrite rules

查看:61
本文介绍了使用自定义.htaccess重写规则的意外行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在研究一些重写规则,这些规则没有按预期工作.这些是我尝试重写的一些示例请求:

So I have been working on some rewrite rules which aren't working as expected. These are some sample requests I am trying to rewrite:

/             -> /index.php?page=Home
/Home         -> /index.php?page=Home
/Teaching/Foo -> /index.php?page=Teaching&id=Foo
/Teaching/Bar -> /index.php?page=Teaching&id=Bar
/Download/ue8 -> /index.php?action=Download&id=ue8
/Download/24a -> /index.php?action=Download&id=24a
(default)     -> /index.php?page=Home
** OR ALTERNATIVELY **
(default)     -> /index.php?page=FileNotFound
(and maybe rewrite the visible URL to /FileNotFound)

我主要想隐藏尽可能多的url,并防止目录列表和直接访问位于特定文件夹中的文件,并且仅允许通过/Download/FileId访问可下载文件,同时允许访问我的平常页面以进行不同的演讲通过/Teaching/SomeLecture.

I mainly want to hide as much of the urls as possible and prevent both directory listing and direct access to my files located in specific folders and only allow access to downloadable files via /Download/FileId while having my usual pages for different lectures accesible via /Teaching/SomeLecture.

到目前为止,我一直在将此片段用于/Home/Teaching内容:

So far I have been using this snippet for the /Home and /Teaching stuff:

RewriteEngine On
RewriteBase /

# Redirect Trailing Slashes
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Teaching/([A-Za-z0-9]*)$ index.php?page=Teaching&id=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Home$ index.php?page=Home [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L,R=301]

我对所有这些指令不是很确定,并且我注意到,它目前存在一些缺陷.

I am not completely sure about all of these directives and I noticed, that there are currently some flaws to it.

  1. 尝试访问不存在的文件,例如/Files/Bad/Path.pdf,将用户转发到/?page=Home,该用户应重定向到//Home或显示/index.php?page=FileNotFound的内容,而不必完全更改URL或重定向到/FileNotFound,具体取决于(default).我不太确定哪种解决方案最适合这种情况.
  2. 尝试访问某些确实存在的文件夹会导致无限重定向循环,而显然不存在的文件夹显然会重定向到/.在这两种情况下,重定向到我想的/FileNotFound都感觉正确吗?
  1. Trying to access a non-existant file, e.g. /Files/Bad/Path.pdf, forwards the user to /?page=Home which should be redirected either to /, /Home or display the contents of /index.php?page=FileNotFound without changing the URL at all or redirecting to /FileNotFound depending on the rule of (default). I am not really sure which solution might be the most suitable in this scenario.
  2. Trying to access some folders which do exist results in an infinite redirection loop while folders which do not exist apparently redirect to /. In both cases it feels right to redirect to /FileNotFound I suppose?

在这种情况下,您能制定出一套适合我需要的规则吗?

Could you please work out a set of rules that might suit my needs in this case?

推荐答案

.htaccess中有许多冗余指令.替换所有您的.htaccess与此:

You have many redundant directives in your .htaccess. Replace all of your .htaccess with this:

# Turn off mod_spelling
<IfModule mod_speling.c>
   CheckSpelling off
   CheckCaseOnly off
</IfModule>

Options -MultiViews
RewriteEngine On
RewriteBase /

# block direct access to file and directories in these directories
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(Templates|Files) - [NC,F]

# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]

# Redirect Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(Download|Teaching)/([\w-]+)/?$ index.php?page=$1&id=$2 [L,QSA,NC]

RewriteRule ^(Home)?/?$ index.php?page=Home [L,NC,QSA]

在进行测试之前,请确保清除浏览器缓存.

Make sure to clear your browser cache before testing this.

这篇关于使用自定义.htaccess重写规则的意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆