mod_rewrite的:重定向请求子目录具有不同结构的许多子+重定向到index.php重定向、子目录、不同、结构

2023-09-02 00:42:30 作者:高冷潜质在发光

我需要建立一个下面的规则在我的.htaccess放置

I need to create a following rule to place in my .htaccess

对于每个请求,我想在子文件夹中子目录执行文件/路径。 如果该文件不存在,存在的话,我想转发此请求到index.php

让说的.htaccess IW放在的http://域名/文件夹

Let say that .htaccess iw placed at http://domain/folder

当用户打开网址 HTTP://domain/folder/Subfolder/xxx.html //domain/folder/subdir/Subfolder/xxx.html

When the user opens url http://domain/folder/Subfolder/xxx.html he should recieve file from http://domain/folder/subdir/Subfolder/xxx.html

所以,这些规则必须考虑的是,其内部子目录的子文件夹。 (不同的子文件夹结构:-) 且仅当在子目录不存在,该请求的路径应该被转发到的index.php

So these rules have to consider the fact that there are subfolders inside subdir. (Different structures of subfolders :-) And only if the path under subdir doesn't exist the request should be forwarded to index.php

Plase帮助:)

PS这是一个后续问题,以我的previous之一。它是完全不同的,并且包括一个事实,即有内部子目录子文件夹。 - > mod_rewrite:如何在本地文件夹,子文件夹搜索,然后重定向到index.php

ps This is a followup question to my previous one. It is quite different and includes the fact that there are subfolders inside subdir. -> mod_rewrite: How to search in local folder, subfolder and then redirect to index.php

推荐答案

这应该做到这一点:

RewriteEngine on
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

RewriteCond %{REQUEST_URI} ([w]+.[w]+)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1 -f 
RewriteRule ^ /%1 [NC,L] 

RewriteCond %{DOCUMENT_ROOT}/subdir%{REQUEST_URI} -f
RewriteRule ^ /subdir%{REQUEST_URI} [L]

RewriteRule ^ /index.php [L]

有轻微的优化更新。

RewriteEngine on
#RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^[^/]+.[^/]+$ - [NC,L]

RewriteCond %{REQUEST_FILENAME} (.*/)([w]+.[w]+)$ [NC]
RewriteCond %1subdir/%2 -f
RewriteRule ^ subdir/%2 [L]

RewriteRule ^ index.php [L]

在上面将与anydirectory /子目录

更新日志:

的RewriteBase评论使用相对路径。

RewriteBase commented for use of relative path.

检查/file.ext(或者是否在当前目录)。 下面将检查文件是否present在当前目录中。

Checking for /file.ext (or whether in current directory). the below will check whether file is present in current directory.

的RewriteCond%{} REQUEST_FILENAME -f 重写规则^ [^ /] + [^ /] + $ - [NC,L]

的RewriteCond%{DOCUMENT_ROOT} /子目录%{REQUEST_URI} -f

RewriteCond %{DOCUMENT_ROOT}/subdir%{REQUEST_URI} -f

捕获%1当前目录和%的file.ext 2

captures current directory in %1 and the file.ext in %2