【发布时间】:2016-01-18 02:48:21
【问题描述】:
所以我一直在研究一些无法按预期工作的重写规则。这些是我尝试重写的一些示例请求:
/ -> /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。
到目前为止,我一直将这个 sn-p 用于 /Home 和 /Teaching 的东西:
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]
我并不完全确定所有这些指令,我注意到它目前存在一些缺陷。
- 试图访问不存在的文件,例如
/Files/Bad/Path.pdf,将用户转发到/?page=Home,这应该重定向到/,/Home或显示/index.php?page=FileNotFound的内容,而不改变URL或重定向到/FileNotFound,这取决于@的规则987654333@。我不确定哪种解决方案可能最适合这种情况。 - 尝试访问某些确实存在的文件夹会导致无限重定向循环,而不存在的文件夹显然会重定向到
/。在这两种情况下,我想重定向到/FileNotFound感觉不错?
您能否制定一套在这种情况下可能适合我需要的规则?
【问题讨论】:
标签: regex apache .htaccess mod-rewrite redirect