简体   繁体   English

Apache mod_rewrite不保留名称

[英]Apache mod_rewrite not persisting the name

I usually put my mod_rewrite conditions in an .htaccess file, but this is a case where it must go into the httpd.conf file. 我通常将我的mod_rewrite条件放在.htaccess文件中,但是在这种情况下,必须将其放入httpd.conf文件中。 I am confused because what I want to do seems simple: The root of the site is a nested directory: mydomain.com/foo/bar/ It just has to be that way. 我很困惑,因为我想做的事情看起来很简单:该站点的根目录是一个嵌套目录:mydomain.com/foo/bar/只是那样。 I want to write a rule so a person can enter: mydomain.com/simple and it will show content from mydomain/foo/bar Also, if a person clicks around the site, I want the mydomain.com/simple/some-other-page structure to persist. 我想编写一条规则,以便一个人可以输入:mydomain.com/simple,它将显示mydomain / foo / bar中的内容。此外,如果一个人在网站上单击,我希望mydomain.com/simple/some-other页结构持久化。 The closest I've gotten is this: 我得到的最接近的是:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule   ^simple$ /foo/bar/$1 [PT]
</IfModule>

However, using this rule, when a person types mydomain.com/simple it rewrites the URI in the browser to mydomain.com/foo/bar What am I doing wrong? 但是,使用此规则,当某人键入mydomain.com/simple时,它将浏览器中的URI重写为mydomain.com/foo/bar我在做什么错? Thanks in advance. 提前致谢。

First, there may be a problem with this rule: 首先,此规则可能存在问题:

RewriteRule   ^simple$ /foo/bar/$1 [PT]

The expression ^simple will probably never match, since all requests will start with a / . 表达式^simple可能永远不会匹配,因为所有请求都将以/开头。

You're using $1 in the right-hand side of the rule, but there are no match groups in the left-hand side that will populate this. 您在规则的右侧使用了$1 ,但左侧没有匹配项组可填充此内容。 This means that a request for /simple would get you /foo/bar/ , but a request for /simple/somethingelse wouldn't match the rule. 这意味着对/simple的请求会使您/foo/bar/ ,但对/simple/somethingelse的请求将不符合该规则。 If this isn't the behavior you want, you probably mean this: 如果这不是您想要的行为,则可能是这个意思:

    RewriteRule   ^/simple(.*)$ /foo/bar$1 [PT]

(Note that I've added the missing leading / here as well). (请注意,我也在此处添加了缺少的前导/ )。

With these changes in place, this rule behaves on my system as I think you're expecting. 完成这些更改后,此规则将在我的系统上运行,就像我认为的那样。

Lastly, turning on the RewriteLog and setting RewriteLogLevel (assuming a pre-2.4 version of Apache) will help expose the details of exactly what's happening. 最后,打开RewriteLog并设置RewriteLogLevel (假设使用Apache的2.4之前的版本)将有助于详细了解正在发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM