从一个目录重定向到另一个mod_rewrite的 [英] Redirect from one directory to another with mod_rewrite

查看:262
本文介绍了从一个目录重定向到另一个mod_rewrite的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的目录结构:

 管理​​\
控制\
影像\
媒体\
LIB \
楷模\
意见\
的index.php
的.htaccess
 

下面是我的.htaccess

  RewriteEngine叙述上
重写规则/admin/images/(.*)/图片/ $ 1
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则。的index.php
 

我希望一切都在/管理/图片等于在根目录/图像。例如:<一href="http://www.example.com/admin/images/example.png">http://www.example.com/admin/images/example.png将是相同的 http://www.example.com/images/example.png

的问题与我的.htaccess是: 它去到index.php,而不是镜像管理/图像,图像/


解决方案

  RewriteEngine叙述上
重写规则/admin/images/(.*)/图片/ $ 1 [L]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则。的index.php
 

解决方案

您需要指定图像重写规则是最后一个行中,以prevent进一步改写。对于您只需指定 [L] 在图像重写规则的结束。

  RewriteEngine叙述上
重写规则/admin/images/(.*)/图片/ $ 1 [L]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则。的index.php
 

编辑:这是为什么发生问题的解释(从评论部分,以提供澄清服用)

您看到原始的%{REQUEST_FILENAME} 永远不会改变,无论怎样改写了多个阶段执行。这意味着,当到达第二个规则变量其实还是指向现有的图像(这是在 /管理/图片/ ),而要被改写一个非 - 现有( /图片/ )。这就是为什么第二条规则将始终被应用的原因,以及为什么要改写过程中使用的例子两个条件线几乎总是首当其冲。

The following is my directory structures:

admin\
controls\
images\
media\
lib\
models\
views\
index.php
.htaccess

The following is my .htaccess

RewriteEngine On 
RewriteRule /admin/images/(.*) /images/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php

I want everything in /admin/images be equal to /images in root directory. For example: http://www.example.com/admin/images/example.png will be the same as http://www.example.com/images/example.png

Problem with my .htaccess is: It goes to index.php instead of mirroring admin/images to images/


Solution

RewriteEngine On 
RewriteRule /admin/images/(.*) /images/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php

解决方案

You need to specify that your image rewrite rule is the last one in the row in order to prevent further rewriting. For that you simply specify [L] at the end of your image rewrite rule.

RewriteEngine On 
RewriteRule /admin/images/(.*) /images/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php

EDIT: Here is an explanation why the problem occurs (taken from the comments section in order to provide clarification).

You see the original %{REQUEST_FILENAME} will never change no matter how many stages of rewriting are performed. That means that when the second rule is reached that variable will actually still point to the existing image (which is in /admin/images/) rather to the one being rewritten and non-existing (/images/). That's the reason why the second rule will always be applied and why the two conditional lines from the example are almost always the first ones to be used during rewriting.

这篇关于从一个目录重定向到另一个mod_rewrite的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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