.htaccess Htaccess -管理在多个目录和一个htaccess文件中捕获所有内容

gcmastyq  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(124)

我正试图根据一个特定的关键字调度多个目录中的所有流量。
我有以下目录结构:

dotcom/
dotcom/directory1/ (with subdirs)
dotcom/directory2/ (with subdirs)
dotcom/directory3/ (with subdirs)

我有一个.htaccess文件位于dotcom,我想重定向每个目录后面的一切到每个目录中的索引文件。
示例:

dotcom/directory1/anything/blabla to dotcom/directory1/index.php
dotcom/directory2/anything/blabla to dotcom/directory2/index.php
dotcom/anythingNotExisting to dotcom/index.php

不在现有目录中的任何内容都应重定向到dotcom/index.php
我为网络公司尝试了以下方法:

RewriteEngine On
RewriteCond %{ENV:HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

这能抓住所有的东西
但是当我试图添加如下条件时,我得到一个404:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/directory1/(.*)$ directory1/index.php?path=$1 [NC,L,QSA]

有了这个,如果我试图访问dotcom/directory 1/blabla,我有一个404,而如果我访问dotcom/directory 1/,它会进入正确的index.php
我尝试使用完整路径dotcom/directory 1/,但没有任何帮助。

w51jfk4q

w51jfk4q1#

您可以在dotcom/.htaccess中使用这些规则:

DirectoryIndex index.php
RewriteEngine On

RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# ignore all rules below this for real files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# for URIs starting with know directory paths
RewriteRule ^(directory1|directory2)/(.*)$ $1/index.php?path=$2 [NC,L,QSA]

# everything else
RewriteRule .+ index.php?path=$0 [L,QSA]
00jrzges

00jrzges2#

我已经找到了一些与以下工作:

RewriteEngine ON

RewriteCond HTTPS off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^directory1/(.*)$ /dotcom/directory1/index.php?path=$1 [NC,L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^directory2/(.*)$ /dotcom/directory2/index.php?path=$1 [NC,L,QSA]

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /dotcom/index.php?path=$1 [NC,L,QSA]

这样,我将捕获/directoryX/中的所有内容,并将其重定向到目录的根目录,其他内容将转到dotcom

相关问题