Steve
Steve

Reputation: 4898

Simple mod_rewrite rules not working

In trying to get my head around mod_rewrite, I've added a simple rewrite rule:

RewriteRule ^z$ z1.html

The idea is to let someone access www.mysite.com/z and have them get www.mysite.com/z1.html

But this doesn't work. I have to change it to

RewriteRule ^/z$ /z1.html

But I'm not seeing anything in the mod_rewrite rules that requires a "/" in front of the terms, so why doesn't the first one work?

A bit more complicated, I have the rule

RewriteRule ^/([^.]+)$ /1$.html

and this one doesn't work either, even with the "/" characters.

The idea here is to let some enter www.mywebsite.com/z1 and have it become www.mywebsite.com/z1.html.

Does anyone see the problem?

Thanks.

Upvotes: 2

Views: 57

Answers (1)

Jon Lin
Jon Lin

Reputation: 143856

If you have mod rewrite rules in vhost/server config, it requires a / as part of the pattern. The second rule doesn't work because you want $1, and not 1$:

RewriteRule ^/([^.]+)$ /$1.html [L]

Upvotes: 1

Related Questions