为什么前导斜杠与我的RewriteRule不匹配

为什么前导斜杠与我的RewriteRule不匹配

本文介绍了为什么前导斜杠与我的RewriteRule不匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我错过了一些愚蠢的事情.

I hope I'm missing something silly.

我正在尝试使用OpenShift的免费托管服务上的PHP 5.4卡盒在Apache 2.2上使用.htaccess重定向URL.

I'm trying to redirect URLs using .htaccess on Apache 2.2 using the PHP 5.4 cartridge on OpenShift's free hosting service.

这与URI/permalink/a123匹配(请注意,规则的过滤器模式中没有前导斜杠):

This matches the URI /permalink/a123 (note the lack of leading slash in the rule's filter pattern):

RewriteEngine On
RewriteRule permalink/a.*$ /permalink/b [R=301,L]

这与URI/permalink/a123不匹配(请注意规则过滤模式中的斜杠):

This does not match the URI /permalink/a123 (note the leading slash in the rule's filter pattern):

RewriteEngine On
RewriteRule /permalink/a.*$ /permalink/b [R=301,L]

那我到底有什么愚蠢的错误?

So what stupid thing do I have wrong?

谢谢.

推荐答案

用于匹配RewriteRule中的模式的URI在每个目录上下文中(在htaccess文件或<Directory>容器中)被规范化.通过删除开头的/.因此,如果请求的URL是:

The URI used to match patterns in a RewriteRule are canonicalized in a per-directory context (either in an htaccess file or in a <Directory> container) by removing the leading /. So if the requested URL is:

http://example.com/web/permalink/123

在文档根目录的htaccess文件中,用于匹配规则的URI为web/permalink/123.但是在web文件夹中的htaccess文件中,URI是permalink/123等.

And from within an htaccess file in the document root, the URI used to match rules is web/permalink/123. But within an htaccess file in the web folder, the URI is permalink/123, etc.

因此,您不能让模式以/开头,因为它们是在htaccess文件的上下文中从URI中剥离的.

Thus you can't have your patterns start with a / because they're stripped from the URI in the context of an htaccess file.

这篇关于为什么前导斜杠与我的RewriteRule不匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:10