.htaccess 如何使用两个htaccess重定向到不同的index.php

gupuwyp2  于 2023-08-06  发布在  PHP
关注(0)|答案(1)|浏览(116)

我已经安装了2个不同的脚本,一个在根文件夹,第二个在子文件夹。
这两个脚本被重定向到各自文件夹中的index.php
当我访问子文件夹页面时,我被发送到父文件夹的404页面,除了子文件夹主页。

htaccess文件夹:

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.html [L]

</IfModule>

字符串

htaccess子文件夹:

<IfModule mod_rewrite.c>

    <IfModule mod_negotiation.c>

        Options -MultiViews

    </IfModule>

  RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?website\.it)$ [NC]

RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]

    # Redirect Trailing Slashes...

    # Handle Front Controller...

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteRule ^ index.php [L]
</IfModule>

eagi6jfj

eagi6jfj1#

当我访问子文件夹页面时,我被发送到父文件夹的404页面,除了子文件夹主页。
如果子文件夹中的.htaccess文件实际上未被处理,则会发生这种情况。可能只为根目录配置了AllowOverride(实质上是启用/禁用.htaccess文件)。这只能在服务器/vHost配置中配置。
当您访问“子文件夹主页”时,.htaccess根本不是必需的(并且指令不会被处理),因为mod_dir服务于该目录中的DirectoryIndex文档。
您可以将两个.htaccess文件合并到根目录中的一个配置文件中。
举例来说:

Options -MultiViews

DirectoryIndex index.php index.html

RewriteEngine on

# Remove www subdomain
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?website\.it)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]

# Front-controller for subfolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(subfolder)/. $1/index.php [L]

# Front-controller for root
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]

字符串
我假设你会想删除www子域,不管你正在访问哪个脚本。
<IfModule> Package 器不是必需的,除非这些指令是 optional(看起来不是这样)。

相关问题