mod_rewrite的:重定向请求子目录具有不同结构的许多子+重定向到index.php [英] mod_rewrite: Redirect request to subdir with many subfolders of different structures + redirect to index.php

查看:161
本文介绍了mod_rewrite的:重定向请求子目录具有不同结构的许多子+重定向到index.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  1. 对于每个请求,我想在子文件夹中子目录执行文件/路径。
  2. 如果该文件不存在,存在的话,我想转发此请求到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 HTTP免费获赠文件>

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之一。它是完全不同的,并且包括一个事实,即有内部子目录子文件夹。 - > <一个href="http://stackoverflow.com/questions/9327144/mod-rewrite-how-to-search-in-local-folder-subfolder-and-then-redirect-to-index">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

    这篇关于mod_rewrite的:重定向请求子目录具有不同结构的许多子+重定向到index.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆