Php htaccess重写两个目录

Php htaccess重写两个目录,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,我试图使用重写规则访问两个目录。我从网上和其他SO帖子中抓取并测试了很多代码,但最终得到的代码仍然不能正常工作。以下是我想做的: 1) 如果页面请求是针对/home、/test、/404、/etc,则从/app目录加载文件,并按如下方式遍历:/home、/news/topics/post、/contact/、/blog/category/date/post 2) 如果页面请求仅针对/admin,则从/admin目录加载文件,并能够像这样遍历:/admin或/dashboard、/admin/pa

我试图使用重写规则访问两个目录。我从网上和其他SO帖子中抓取并测试了很多代码,但最终得到的代码仍然不能正常工作。以下是我想做的:

1) 如果页面请求是针对/home、/test、/404、/etc,则从/app目录加载文件,并按如下方式遍历:/home、/news/topics/post、/contact/、/blog/category/date/post

2) 如果页面请求仅针对/admin,则从/admin目录加载文件,并能够像这样遍历:/admin或/dashboard、/admin/page/edit、/admin/page/delete、/admin/page/create、/admin/section/delete/

发生了什么事? 如果我去/家或只是去/就行了!但是如果我访问/test、/contact、/admin、/etc,我会发现一个404文件没有找到

文件结构(可以更改):

我开始认为,像这样硬编码重写可能更好:

RewriteRule ^home$ /atlas.php [NC,L]
RewriteRule ^admin$ /admin/dashboard.php [NC,L]
…但我想在我放弃这件事之前,我会看看SO社区是否能提供帮助

有什么建议吗

我当前的代码:

Options -Indexes
Options -MultiViews
Options +FollowSymLinks


# Set the default site page
DirectoryIndex /app/index.php home


# Rewrite Rules and Conditions
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

# Remove www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L,NC]


# Assets are in /app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.(gif|jpg|png|jpeg|css|js|svg|swf)$ /app/$1.$2 [L,NC]


# First, check if request is to site pages, which is in /app/
RewriteCond %{DOCUMENT_ROOT}/app%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/app%{REQUEST_URI} -d
RewriteRule ^(.*)$ /app/index.php?/$1 [NC,L]


# Second, check if request is to /admin, which is in /admin/
RewriteCond %{DOCUMENT_ROOT}/admin%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/admin%{REQUEST_URI} -d
RewriteRule ^(.*)$ /admin/dashboard.php?/$1 [NC,L]


# Default to /app
RewriteCond ${REQUEST_URI} !^/app/
RewriteRule ^(\w+)$ /app/$1 [L]

</IfModule>
选项-索引
选项-多视图
选项+FollowSymLinks
#设置默认站点页面
DirectoryIndex/app/index.php主页
#重写规则和条件
重新启动发动机
重写基/
#删除www
重写cond%{HTTPS}=在…上
重写cond%{HTTP_HOST}^www\.(.+)$[NC]
重写规则^(.*)$http://%1/$1[R=301,L,NC]
#资产位于/应用程序中
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)\(gif | jpg | png | jpeg | css | js | svg | swf)$/app/$1.$2[L,NC]
#首先,检查请求是否指向站点页面,该页面位于/app中/
重写cond%{DOCUMENT_ROOT}/app%{REQUEST_URI}-f[或]
重写cond%{DOCUMENT\u ROOT}/app%{REQUEST\u URI}-d
重写规则^(.*)$/app/index.php?/$1[NC,L]
#其次,检查请求是否为/admin,它位于/admin中/
重写cond%{DOCUMENT_ROOT}/admin%{REQUEST_URI}-f[或]
重写cond%{DOCUMENT\u ROOT}/admin%{REQUEST\u URI}-d
重写规则^(.*)$/admin/dashboard.php?/$1[NC,L]
#默认设置为/app
重写cond${REQUEST_URI}^/应用程序/
重写规则^(\w+)$/app/$1[L]

我想你可以:

#admin
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/admin(.*)$ /admin_handler.php?path=$1 [QSA]

#app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ /app_handler.php?path=$1 [QSA]
如果你所有的URL都需要
/app
,为什么不把
/app
放进去呢?在重写中添加它是没有意义的


当您进行重写时,最好只有一个入口点,因为您只需将所有内容重写到该文件中,然后让PHP处理它。用PHP比用
mod\u rewrite
更容易理解请求是什么。此设置现在有两个入口点,一个用于访问者,一个用于管理员,但您甚至可以将它们合并为一个。

我不使用此方法:?path=$1&$2&$3没有
$2
$3
$1
已经匹配了所有内容。您格式错误的查询字符串有点奇怪,我不建议这样做。我将管理文件与网站或应用程序分开,因为可能有多个应用程序。我不喜欢混合管理代码,这可能会被开发人员搞得乱七八糟,因为到目前为止我在应用程序上发现了这一点。htaccess是我发现的对其他人有用的东西的混合体。所以也许我需要重写整件事?这是我的建议。您将看到的许多解决方案都适用于只需要在这里或那里稍加重写的应用程序。如果你打算重写你所有的URL,最好只做一个单一的入口点,去掉所有的噱头。
#admin
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/admin(.*)$ /admin_handler.php?path=$1 [QSA]

#app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ /app_handler.php?path=$1 [QSA]