Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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
我可以将index.php放在子目录中,但不能在url中显示子目录吗?_Php_.htaccess - Fatal编程技术网

我可以将index.php放在子目录中,但不能在url中显示子目录吗?

我可以将index.php放在子目录中,但不能在url中显示子目录吗?,php,.htaccess,Php,.htaccess,比如说,我有一个网站。我想把index.php放在一个子目录中,public\uhtml/mysubdir/index.php。我希望在用户转到时执行public\uhtml/mysubdir/index.php。我希望url继续阅读http://mysite.com。这可能吗?如果您的Web服务器是Apache,您可以使用 另一个选项是在根目录中创建index.php,并在子目录中包含index.php。您可以使用.htaccess文件并定义重写规则 确保启用了mod_rewrite,然后将

比如说,我有一个网站。我想把index.php放在一个子目录中,
public\uhtml/mysubdir/index.php
。我希望在用户转到时执行
public\uhtml/mysubdir/index.php
。我希望url继续阅读
http://mysite.com
。这可能吗?

如果您的Web服务器是Apache,您可以使用


另一个选项是在根目录中创建index.php,并在子目录中包含index.php。

您可以使用.htaccess文件并定义
重写规则

确保启用了
mod_rewrite
,然后将.htaccess文件放在根目录中,如下所示:

<IfModule mod_rewrite.c>
RewriteEngine On

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

重新启动发动机
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$your_subdir/index.php/$1[L]
根据您的需要,重写规则在这方面可能有些过火。对于您的主索引页,这将工作

只需将这一行添加到
.htaccess
文件中:

DirectoryIndex mysubdir/index.php
它将显示位于
mysubdir/index.php的页面,同时只显示
http://mysite.com
在URL中

我自己用这个方法。虽然我的所有页面都位于同一子目录中,但主页本身显示的是我的域名(
http://www.mysite.com
)。所有其他页面都显示完整的URL


如果您在更深的子目录中也有
索引
页面,并且希望这些页面在默认情况下在子目录中出现

例如:

如果需要此页面:
http://mysite.com/mysubdir/anothersub/index.php

要创建此URL:
http://mysite.com/mysubdir/anothersub/

然后用另一个
index.php
修改该行,如下所示

DirectoryIndex mysubdir/index.php index.php
这会告诉服务器以相同的顺序查找具有这些名称的文件。如果找不到第一个,它会尝试第二个,依此类推

当您位于根目录中的
/
时,它会找到并显示
mysubdir/index.php


当您在另一个子目录(如
/mysubdir/anothersub/
)中时,它找不到任何名为
mysubdir/index.php
的内容,因此它会转到下一项并显示
index.php

是的,在这里搜索URL重写。您使用的是什么http服务器?@Ivan:我使用的是Apache 2.2.22。这就是你的意思吗?这很有效,谢谢。然而,现在我有另一个问题。php链接到各种css和js文件,使用相对链接。例如:。如果用户转到,则相关链接可以正常工作。但如果用户转到,它们就不起作用了。有什么想法吗?我只是使用“绝对”链接
/mysubdir/js/jquery-1.2.1.js
,等等。关于
/
文档根目录的所有内容。太棒了!我很高兴它有用。