![](/img/trans.png)
[英]How do I redirect non-existant subdirectory in my index.php file, keeping the URL
[英]can I put index.php in a subdirectory, but not display subdirectory in url?
我有一个网站,例如http://mysite.com 。 我想将index.php放在一个子目录public_html/mysubdir/index.php
。 我希望当用户转到http://mysite.com时执行public_html/mysubdir/index.php
。 我希望该网址继续阅读http://mysite.com
。 这可能吗?
如果您的Web服务器是Apache,则可以将URL重写与mod_rewrite一起使用 。
另一种选择是在根目录中创建index.php,并在子目录中包含index.php。
根据您的需要,重写规则对此可能会过大。 仅对于您的主索引页面,这将起作用...
只需将这一行添加到您的.htaccess
文件中:
DirectoryIndex mysubdir/index.php
它将显示位于mysubdir/index.php
的页面,同时仅在URL中显示http://mysite.com
。
我自己使用这种方法。 当我所有页面都位于同一个子目录中时,主页将单独显示我的域名( http://www.mysite.com
)。 其他所有页面均显示完整的URL。
如果您在更深的子目录中也有index
页,并且希望这些页在默认情况下出现在子目录中。
例:
如果您想要此页面: http://mysite.com/mysubdir/anothersub/index.php
: http://mysite.com/mysubdir/anothersub/index.php
提出这个网址: http://mysite.com/mysubdir/anothersub/
: http://mysite.com/mysubdir/anothersub/
然后像这样用另一个index.php
修改行...
DirectoryIndex mysubdir/index.php index.php
这样做是告诉服务器以相同的顺序查找具有这些名称的文件。 如果找不到第一个,则尝试第二个,依此类推。
当您位于/
的根目录下时,它会找到并显示mysubdir/index.php
。
当您位于另一个子目录(如/mysubdir/anothersub/
,它找不到名为mysubdir/index.php
任何内容,因此它将转到下一个项目并显示index.php
您可以使用.htaccess文件并定义Rewrite
规则。
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
确保启用了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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.