php 如何在 .htaccess 中编写 rewriteCond 以排除子目录及其子目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1257578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to write a rewriteCond in .htaccess to exclude a subdir and its subdirs
提问by
I have the following problem. I have a website and a blog in a subdirectory. Both of them are php. I have a .htaccess file in the root folder and another one in the blog folder. I dont' think is relevant, but the blog script is wordpress.
我有以下问题。我在一个子目录中有一个网站和一个博客。他们两个都是php。我在根文件夹中有一个 .htaccess 文件,在 blog 文件夹中有另一个。我不认为是相关的,但博客脚本是 wordpress。
I added a condition in the root .htaccess to skip the requests made for the blog,
我在根 .htaccess 中添加了一个条件来跳过对博客的请求,
rewriteCond %{REQUEST_URI} !^/blog.*
Here it is how it looks. I removed the rest of the file:
这是它的外观。我删除了文件的其余部分:
Options +FollowSymlinks -MultiViews RewriteEngine on
RewriteBase / RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
// my added line RewriteCond %{REQUEST_URI} !^/blog.*
RewriteCond %{HTTP_HOST} !^www\. RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^(all)/([^/]+)/?$ story.php?title= [L] RewriteRule ^(all)/?$ ?category= [L]
RewriteRule ^/?$ index.php [L] RewriteRule ^advanced-search/?$ advancedsearch.php [L] ...
The problem I have is related to the blog requests. For example sometimes if I try to open an url it works fine, sometimes the home (root page not the blog) is opened. It seems very strange. I think it is related to the host. When the host is to busy the blog page I request is not found so the request is going to the root .htaccess.
我遇到的问题与博客请求有关。例如,有时如果我尝试打开一个 url,它工作正常,有时会打开主页(根页面而不是博客)。看起来很奇怪。我认为这与主机有关。当主机忙时,找不到我请求的博客页面,因此请求将转到根 .htaccess。
I have 2 questions:
我有两个问题:
- how to write a rule and where to place it to exclude all the requests for /blog to be rewritten by the root .htaccess? the blog requests might look like http: //test.com/blog, http: //test.com/blog/, http: //test.com/blog/title, http: //test.com/blog/title/, http: //test.com/blog/category/title
- does anyone has any idea what happens? Why when I open a blog page it opens the home root page, and if I refresh the page it goes to the blog post page?
- 如何编写规则以及将其放置在何处以排除根 .htaccess 对 /blog 重写的所有请求?博客请求可能看起来像 http://test.com/blog、http://test.com/blog/、http://test.com/blog/title、http://test.com/blog/title /, http://test.com/blog/category/title
- 有谁知道会发生什么?为什么当我打开一个博客页面时,它会打开主页根页面,如果我刷新页面,它会转到博客文章页面?
回答by Alix Axel
Take a look into the mod_rewrite documentation, specifically the -d flag in RewriteCond.
查看mod_rewrite 文档,特别是 RewriteCond 中的 -d 标志。
It should be something like this:
它应该是这样的:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule HERE

