htaccess的 - 重定向在子目录中的所有请求,除非请求的文件是否存在 [英] htaccess - redirect all requests within subdirectory, except if a requested file exists

查看:211
本文介绍了htaccess的 - 重定向在子目录中的所有请求,除非请求的文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发在PHP的应用程序,我需要建立一个pretty的广泛的.htaccess重定向。我做了一些阅读,试着写RewriteConds自己,但它是一个有点以上我paygrade - 我希望有人有更多的经验可以提供帮助。这就是我要完成的:

I'm developing an app in php, and I need to set up a pretty broad .htaccess redirect. I did some reading and tried to write the RewriteConds myself, but it's a bit above my paygrade - I'm hoping someone with more experience can help. Here's what I'm trying to accomplish:

  • 在该应用程序包含在 www.example.com/app / 。请不要在此目录以上重定向任何东西。
  • 此目录,将需要被访问中存在的某些文件。目前这些 / APP /包括/ * / APP / sb_pages / * 。这将改变,并在将来扩展,所以我需要一个优雅的解决方案,包括所有现有文件。这很好,如果重定向这些目录内的触发时,未找到文件 - 我所关心的是能够在访问文件,而无需重定向触发
  • 在所有其他请求应被重定向到 /app/index.php ,在查询字符串传递尾随URL。例如,请求 / APP /路径1 /路径/ 应重定向到 /app/index.php?path=path1/path2 /
  • 重定向不应该是透明的。当用户请求 / APP /路径1 /路径/ ,我想让他们相信他们已经在那里停留。他们的不应该看到URL更改为 /app/index.php?path=path1/path2 /
  • The app is contained in www.example.com/app/. Don't redirect anything above this directory.
  • Some files exist in this directory that will need to be accessed. Currently these are /app/includes/* and /app/sb_pages/*. This will change and expand in the future, so I need an elegant solution that encompasses all existing files. It's fine if the redirect triggers within these directories when a file isn't found - all I care about is being able to access the files within without the redirect triggering.
  • All other requests should be redirected to /app/index.php, with the trailing url passed in the querystring. For example, a request to /app/path1/path2/ should redirect to /app/index.php?path=path1/path2/
  • The redirect should not be transparent. When the user requests /app/path1/path2/, I want them to believe they have remained there. They should not see the url change to /app/index.php?path=path1/path2/.

只是为了更加清晰,这里有一个少数情况下阐述:

Just for added clarity, here's a few cases to elaborate:

  • /app/includes/sidebar.php 不应该重定向。
  • /app/includes/nothing.html 不存在 - 重定向是确定
  • / APP /路径1 /路径/ 应重定向到 /app/index.php?path=path1/path2 / 。用户还是应该看他们的当前URL为 / APP /路径1 /路径/
  • /app/includes/sidebar.php should not redirect.
  • /app/includes/nothing.html does not exist - redirect is OK
  • /app/path1/path2/ should redirect to /app/index.php?path=path1/path2/. User should still see their current URL as /app/path1/path2/.

我希望我已经清楚地解释它,pre-抢占大部分问题。如果你需要澄清,请不要犹豫,问。在此先感谢您的帮助!

I hope I've explained it clearly and pre-empted most questions. If you need clarification, please don't hesitate to ask. Thanks in advance for the help!

推荐答案

尝试在你的文档根目录添加到您的.htaccess文件:

Try adding this to your .htaccess file in your document root:

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

请注意,如果你想访问现有目录(而不是文件)也没有被重定向,添加一个的RewriteCond%{} REQUEST_FILENAME!-d 上面的规则

Note that if you want accesses to existing directories (as opposed to files) to also not be redirected, add a RewriteCond %{REQUEST_FILENAME} !-d above the rule.

这篇关于htaccess的 - 重定向在子目录中的所有请求,除非请求的文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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