justanotherhobbyist
justanotherhobbyist

Reputation: 2190

htaccess mod_rewrite doesn't recognize '-'

I have a clean url rewrite function in my .htaccess, it works fine unless if there's a "-" in the link for example my-pages.php. I need to know how to add that as a condition to:

RewriteRule ^([a-zA-Z0-9]+)/?$ $1.php

Upvotes: 0

Views: 31

Answers (2)

Alastair McCormack
Alastair McCormack

Reputation: 27724

I would use a non-greedy .* to catch anything up to a possible slash:

RewriteRule  ^(.*?)\/?$ $1.php

Upvotes: 0

ProgramFOX
ProgramFOX

Reputation: 6390

Just add a dash at the begin or the end of your character class:

RewriteRule ^([a-zA-Z0-9-]+)/?$ $1.php

If a dash is added at the begin or the end of a character class, it isn't treated as a special character.

Upvotes: 4

Related Questions