重写 /public(根文档)文件夹中 2 个子文件夹的 URL



目前我有一个"公共"文件夹,是我所有前端代码所在的根目录。

但由于我的公用文件夹同时包含网站内容和应用程序内容,因此我想将其拆分得更多。所以我的想法是:

.htaccess (rewriting public url)
src (php backend code)
public/
  web/
    index.php
  app/
    account.php

所以我的问题是,我可以很容易地转到这样的页面:

http://example.com/web/index.phphttp://example.com/app/account.php

但我想从 URL 中删除"网络"和"应用程序"......

http://example.com/index.php

http://example.com/account.php如何使用 .htaccess 执行此操作?我正在运行 apache 2.4。

编辑
我尝试过的一个例子:

RewriteEngine On
RewriteCond %{REQUEST_URI} !app/
RewriteRule (.*) /app/$1
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1

可能需要调整您的请求。但第一种方法是:

首先,确保您已启用 mod 重写。二、在.htaccess中写下以下内容

RewriteEngine On
RewriteRule ^[/]?index.php$ /web/index.php [NS,NC,L]
RewriteRule ^[/]?account.php$ /app/account.php [NS,NC,L]

您需要阅读文档以制定其他规则。你可以在这里阅读https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html

还要特别注意旗帜。阅读文档以设置适用于您的情况的文档。

我现在尝试了很多不同的东西,但我终于想通了。

RewriteEngine on
RewriteBase /
# Rewrite every web request
RewriteCond         "%{DOCUMENT_ROOT}/web/%{REQUEST_URI}"  -f
RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/web/$1"
# Rewrite every app request
RewriteCond         "%{DOCUMENT_ROOT}/app/%{REQUEST_URI}"  -f
RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/app/$1"
# Remove .php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]

这在进行设置时有效:

public/ (root folder)<br>
   web/ (contains website frontend)<br>
   app/ (contains app frontend)<br>
   .htaccess (contains the code above)

最新更新