简体   繁体   English

.htaccess - 将所有根文件夹或子文件夹重定向到 index.php

[英].htaccess - Redirect all to index.php for root folder or subfolder

I need an .htaccess file that will work whether it is put in the root folder or a subfolder without modification.我需要一个 .htaccess 文件,无论它是放在根文件夹还是子文件夹中都可以正常工作而无需修改。 The script below is the normal one that I've been trying to adapt without success.下面的脚本是我一直试图适应但没有成功的正常脚本。 I tried the solution on htaccess rewrite index.php on root and subfolders and couldn't get it to work.在根文件夹子文件夹上尝试了htaccess rewrite index.php 上的解决方案,但无法使其正常工作。

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Layout布局

.htaccess
index.php
subfolder1
- .htaccess
- index.php

The route /blah should go to /index.php and /subfolder1/whatever should go to /subfolder1/index.php .路由/blah应该去/index.php/subfolder1/whatever应该去/subfolder1/index.php Currently, the above script will send /subfolder1/whatever to /index.php .目前,上述脚本会将/subfolder1/whatever发送到/index.php

[Update] [更新]

This should also work for any path under subfolder1, like /subfolder1/1/2/3/idunno .这也适用于 subfolder1 下的任何路径,例如/subfolder1/1/2/3/idunno

If you are using Apache 2.2.16 and later, you can just stop using mod_rewrite, which although extremely useful and powerful, can get messy as hell.如果您使用的是 Apache 2.2.16 及更高版本,您可以停止使用 mod_rewrite,尽管它非常有用和强大,但可能会变得一团糟。

A new directive in mod_dir was introduced, FallbackResource which does just that, redirecting to the uri of your choice if there is no hit on the file system.引入了 mod_dir 中的一个新指令FallbackResource ,它就是这样做的,如果文件系统没有命中,则重定向到您选择的 uri。 It is available in .htaccess files as long as AllowOverride Indexes is specified for the directories in the configuration.只要为配置中的目录指定了AllowOverride Indexes它就可以在 .htaccess 文件中使用。

As .htaccess files are evaluated depth-first, you just have to have each .htaccess file describe your fallback resource in the current directory, and the one in the subdirectory subfolder1 will take precedence:由于 .htaccess 文件是深度优先评估的,您只需让每个 .htaccess 文件描述当前目录中的回退资源,并且子目录subfolder1将优先:

subfolder1/.htaccess:子文件夹1/.htaccess:

FallbackResource index.php

.htaccess: .htaccess:

FallbackResource index.php

They're both the same, and work just right.它们都是一样的,而且工作得恰到好处。

It seems this directive is not well known yet even though it's been around for a few years, and its goal is precisely to solve that problem in an elegant way.尽管这个指令已经存在了几年,但它似乎还不为人所知,它的目标正是以优雅的方式解决该问题。

There is only one limitation with that setup.该设置只有一个限制。 Calling urls in non-existing sub-directories of the root dir or subfolder1 will yield subrequest recursion and subsequently an error 500, because the fallback resource is local to the given directory.在根目录或子文件夹 1 的不存在的子目录中调用 url 将产生子请求递归和随后的错误 500,因为回退资源是给定目录的本地资源。
The best approach is to have absolute uris (beginning with '/') as parameter to FallbackResource, which is why it is true that the requirement in itself is kind of odd, and is probably not playing too well with the inner workings of Apache.最好的方法是将绝对 uri(以“/”开头)作为 FallbackResource 的参数,这就是为什么要求本身确实有点奇怪的原因,并且可能与 Apache 的内部工作关系不太好。

Must say this is little strange requirement but here it is: 必须说这有点奇怪,但这里是:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/index\.php [NC]
RewriteRule ^(.+?)(/[^/]*|)$ $1/index.php$2 [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM