简体   繁体   English

在.htaccess中包含子文件夹

[英]Include subfolder with .htaccess

I have a sub folder with php files. 我有一个包含php文件的子文件夹。

Like this: domain.com/subfolder/file.php 像这样:domain.com/subfolder/file.php

It would be very useful to call the php files as if they were on the root folder, without ignoring the existing root files. 调用php文件就像将它们放在根文件夹中一样,而不忽略现有的根文件,这将非常有用。

Is there a way to include a subfolder and all its contents through .htaccess? 有没有一种方法可以通过.htaccess包含子文件夹及其所有内容?

You can use the following rule in root/.htaccess : 您可以在root / .htaccess中使用以下规则:

 RewriteEngine on


 #1--If the request is not for an existent root directory--#
RewriteCond %{REQUEST_FILENAME} !-d
 #2--And the request is not for an existent root file--#
RewriteCond %{REQUEST_FILENAME} !-d
#3--Then, rewrite the request to "/subfolder"--#
RewriteRule ^([^/]+)/?$ /subfolder/$1 [NC,L]

The RewriteConditions above are important to avoid rewriting your root folder and files to /subfolder. 上面的RewriteConditions对于避免将根文件夹和文件重写到/ subfolder至关重要。

Or try this : 或者试试这个:

RewriteEngine on
#--if /document_root/subfolder/foo is an existent dir--#
RewriteCond %{DOCUMENT_ROOT}/subfolder/$1 -d [OR]
#--OR /document_root/subfolder/foo is an existent file--#
RewriteCond %{DOCUMENT_ROOT}/subfolder/$1 -f
#--rewrite "/foo" to "/subfolder/foo--#
RewriteRule ^(.+)$ /subfolder/$1 [NC,L]

How about something like this? 这样的事情怎么样?

RewriteRule ^subfolder/(.*)$ $1

It should help I think 我认为应该有帮助

PS please make sure that apache rewrite module is enabled and turned of in .htaccess file PS请确保在.htaccess文件中启用并重写了apache重写模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM