online_p
online_p

Reputation: 103

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. 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. 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. 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? Thanks in advance.

Upvotes: 1

Views: 54

Answers (1)

larsks
larsks

Reputation: 312038

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 /.

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. This means that a request for /simple would get you /foo/bar/, but a request for /simple/somethingelse wouldn't match the rule. 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.

Upvotes: 1

Related Questions