【发布时间】:2021-03-14 11:29:49
【问题描述】:
我目前正在使用它来使我的网址看起来更漂亮:
ErrorDocument 403 /404.php
ErrorDocument 404 /404.php
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
我终于让它工作了(我对此很陌生),但现在我需要它来为位于主站点子目录中的“子站点”工作。而不是重写https://example.com/index.php,它需要重写https://example.com/sub/index.php
我尝试了一些方法,例如更改 RewriteBase 和 RewriteRule,但没有成功。
为了确定,我应该可以将另一个 .htaccess 放在子目录中,并期望它重写 example.com/sub 的 url,对吧?
提前致谢!
编辑 我向主 .htaccess 添加了例外,如下所示: 错误文档 403 /404.php 错误文档 404 /404.php 选项-索引
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^(/controle|/controle/|/voting|/voting/.*)$
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
但是,我仍然不知道将什么放入子目录中的 .htaccess 中,同一个不起作用,我尝试更改类似的内容
RewriteBase / 到 RewriteBase /subdir1/
【问题讨论】:
-
“为了确定,我应该可以将另一个 .htaccess 放在子目录中,并期望它重写 example.com/sub 的 url,对吧?” - 不,只要你的根级别 .htaccess 已经捕获了这些请求 - 那么它们就会被那个请求重写。因此,您需要为这些重写添加一个例外,即他们只留下
sub/声明的任何内容。 -
我会添加例外情况,谢谢!在将异常添加到根 .htaccess 文件后,您是否还知道我应该更改哪些内容以使此 .htaccess 对 sub/ 起作用?
-
当您添加异常时,几乎相同的东西也应该放在子文件夹中的 .htaccess 中。当然,您也可以直接在根级别添加它(如果您愿意) - 首先匹配以
sub/开头的任何请求路径,然后将其重写为sub/index.php,然后为根级别执行这些操作。 -
请编辑您的问题并在其中添加格式正确的代码,以难以阅读的 cmets 格式。
-
@CBroe 默认情况下,
/subdirectory/.htaccess文件中的 mod_rewrite 指令将完全覆盖根.htaccess文件中的 mod_rewrite 指令。无需例外。