Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 如何使用.htaccess文件重写URL';是子目录吗?_Apache_.htaccess - Fatal编程技术网

Apache 如何使用.htaccess文件重写URL';是子目录吗?

Apache 如何使用.htaccess文件重写URL';是子目录吗?,apache,.htaccess,Apache,.htaccess,我目前正在使用此选项使我的URL看起来漂亮: 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 RewriteCon

我目前正在使用此选项使我的URL看起来漂亮:

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>
errordocument403/404.php
ErrorDocument 404/404.php
选项-索引
重新启动发动机
重写基/
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}/index.html-F
重写cond%{REQUEST_FILENAME}/index.php-F
重写规则。index.php[L]
我终于让它工作了(我对这个很陌生),但现在我需要它来为位于主站点子目录中的“子网站”工作。它需要重写,而不是重写

我尝试了一些事情,比如更改RewriteBase和RewriteRule,但没有成功

为了确保,我应该能够在子目录中放置另一个.htaccess,并期望它重写URL,例如.com/sub,对吗

提前谢谢

编辑 我向main.htaccess添加了异常,如下所示: ErrorDocument 403/404.php ErrorDocument 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>

重新启动发动机
重写基/
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_URI}^(/controle |/controle/|/voting |/voting/*)$
重写cond%{REQUEST_FILENAME}/index.html-F
重写cond%{REQUEST_FILENAME}/index.php-F
重写规则。index.php[L]
但是,我仍然不知道要在subdir中的.htaccess中放入什么,同一个不起作用,我尝试过改变一些东西,比如
RewriteBase/
RewriteBase/subdir1/

为了确保,我应该能够在子目录中放置另一个
.htaccess
,并期望它重写
example.com/sub
的URL,对吗

对。默认情况下,
/subdir/.htaccess
文件中的mod_rewrite指令将完全覆盖父/root
.htaccess
文件中的mod_rewrite指令。但是,您需要重新定义
ErrorDocument
指令以覆盖根目录

为了澄清这两个条件,您是否希望偶尔从请求的子目录提供
index.html
index.php
(DirectoryIndex)文档,而不是将请求路由到文档根目录中的
/index.php
?在这种情况下,只需检查请求是否没有映射到目录(即,
RewriteCond%{request\u FILENAME}!-d
)就更为常见(而且更为理想)。除非在文档根目录中有一些物理子目录要路由到
/index.php

(请注意我上面关于用目录检查替换这两个条件的评论。)

现在,您只需将完全相同的指令复制到
/subdir/.htaccess
文件中,这些指令现在将请求路由到
/subdir/index.php
,从而覆盖父级中的mod_rewrite指令

无需复制
选项-索引
,除非您想更改此选项

或者,如果您想要应用完全相同的指令(但与
/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
文件中那样运行。

“为了确保,我应该能够在子目录中放置另一个.htaccess,并期望它重写URL,例如.com/sub,对吗?”-不,只要您是根级别的。htaccess已经捕获了这些请求,那么它们就会被该请求重写。因此,您需要为这些重写添加一个例外,即它们将任何声明都单独保留在
sub/
中。我将添加例外,谢谢!您还知道在我将异常添加到根.htaccess文件后,我应该更改什么以使此.htaccess适用于sub/吗?当您添加异常时,几乎相同的内容也应该放在子文件夹中的.htaccess中。当然,您也可以直接将其添加到根级别(如果您愿意的话)-首先匹配以
sub/
开头的任何请求路径,然后将其重写为
sub/index.php
,然后再对根级别进行处理。请编辑您的问题并在其中添加正确格式的代码,在难以阅读的注释中。@CBroe默认情况下,
/子目录/.htaccess
文件中的mod_rewrite指令将完全覆盖根
.htaccess
文件中的mod_rewrite指令。无需例外。
<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>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
# /subdir/.htaccess

ErrorDocument 403 /subdir/404.php
ErrorDocument 404 /subdir/404.php

RewriteEngine On
RewriteOptions Inherit