我目前正在使用它来使我的网址看起来很漂亮:
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中,如下所示:ErrorDocument 403 /404.php ErrorDocument 404 /404.php Options -Indexes
<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
放在子目录中,并期望它重写的urlexample.com/sub
,对吗?
是。/subdir/.htaccess
默认情况下,文件中的mod_rewrite指令将完全覆盖父/根.htaccess
文件中的mod_rewrite指令。但是,您将需要重新定义ErrorDocument
指令以覆盖根。
RewriteCond %{REQUEST_FILENAME}/index.html !-f RewriteCond %{REQUEST_FILENAME}/index.php !-f
需要说明的是,在这两个条件下,您是否希望偶尔从请求的子目录中提供index.html
或index.php
(DirectoryIndex)文档,而不是将请求路由到/index.php
文档根目录中?在这种情况下,简单地检查该请求是否未映射到目录(即RewriteCond %{REQUEST_FILENAME} !-d
)会更常见(并且略有最优)。除非要/index.php
在文档根目录中路由到某些物理子目录?
<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>
这些指令可以稍作简化-如果您要将相同的指令应用于子站点/subdir
(并服务/subdir/index.php
),这将对您有所帮助。
RewriteBase
完全删除该指令。相对替代字符串,即。index.php
,则相对于.htaccess
文件所在(或继承)的目录。
您也不需要<IfModule mod_rewrite.c>
包装器。有关此的更多信息,请参见我对网站管理员堆栈中以下问题的回答:https : //webmasters.stackexchange.com/questions/112600/is-checking-for-mod-write-really-necessary
这样,上面可以写成:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
(并注意我上面关于用目录检查代替两个条件的评论)。
现在,你可以简单地精确复制相同的指令到/subdir/.htaccess
文件,这些现在将请求路由到/subdir/index.php
替代,覆盖父mod_rewrite的指令。
无需复制Options -Indexes
,除非您想更改此选项。
或者,如果您想应用完全相同的指令(但相对于/subdir
),则只需在/subdir/.htaccess
文件中启用mod_rewrite继承即可。例如:
# /subdir/.htaccess
ErrorDocument 403 /subdir/404.php
ErrorDocument 404 /subdir/404.php
RewriteEngine On
RewriteOptions Inherit
实际上的作用RewriteOptions Inherit
是从父配置(即)“复制” mod_rewrite指令。/.htaccess
在根目录中,位于当前/subdir/.htaccess
文件中指令的后面。重要的是要弄清楚伪指令是“复制的”,它们不能像在根/.htaccess
文件中一样就地运行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句