mod_rewrite的:如何在本地文件夹,子文件夹搜索,然后重定向到index.php [英] mod_rewrite: How to search in local folder, subfolder and then redirect to index.php

查看:148
本文介绍了mod_rewrite的:如何在本地文件夹,子文件夹搜索,然后重定向到index.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建以下规则在我的.htaccess放置

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

  1. 首先,我想execetue文件中的主文件夹(/)(如果requestedfile存在)
  2. 其次,我想搜索子文件夹中的文件 - > /子文件夹
  3. ,如果文件不点1存在,2 ID喜欢重定向请求做的index.php

什么是做到这一点的最好办法。

What would be the best way to do it.

感谢您的帮助:)

PS的第一点是可以消除的一个简单的解决方案。因此,如果用户输入的URL /file.jpeg服务器将检索算法用于/子文件夹这个文件,如果没有找到这个文件,会比重定向rquest做的index.php

ps The first point could be eliminated as a simpler solution. So if the user entered url /file.jpeg the server would serach for this file in /subfolder and if didn't find this file than it would redirect the rquest do index.php

推荐答案

我假设你的URL看起来像这样: somesite.com/subfolder/file.extension 如果URL是不同的详细说明。
添加到您的的.htaccess 的DocumentRoot

I am assuming your URL looks like this: somesite.com/subfolder/file.extension. If URL is different give details.
Add this to your .htaccess in your DocumentRoot.

Options +FollowSymLinks -MultiViews -Indexes

RewriteEngine on
RewriteBase /

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

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

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^ /index.php [L]


改变,以适应OP的要求。


Changed to suit OPs requirement.

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 %{REQUEST_URI} ([\w]+\.[\w]+)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/subdir/%1 -f
RewriteRule ^ /subdir/%1 [L]

RewriteRule ^ /index.php [L]

这篇关于mod_rewrite的:如何在本地文件夹,子文件夹搜索,然后重定向到index.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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