Mobile app version of vmapp.org
Login or Join
Radia820

: Mod_rewrite issue, won't work when links contain "-" I've been looking into mod_rewrite for my simple PHP CMS for a couple of days now and I found a rule that was supposed to work for me:

@Radia820

Posted in: #Apache #ModRewrite #Mysql #Php

I've been looking into mod_rewrite for my simple PHP CMS for a couple of days now and I found a rule that was supposed to work for me:

RewriteRule ^(w+)/?$ view_post.php?url=


This is supposed to take domain.com/example-post to the view_post.php page and query the post from a DB. This doesn't work if there are "-" dashes in the name. However, if I try domain.com/examplepost, it will work as I can see the view_posts.php page loading and the SQL being constructed correctly, however no content loads because obviously that won't match anything in the DB.

I could go a couple of ways around this, one would be to try to query the DB with both dashes and without, easily removed from the text with substr, however I would like for that rewrite rule to work with dashes as well for now. This is probably something related to the regexp, however it is beyond my comprehension.

Any help will be much appreciated!

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Radia820

2 Comments

Sorted by latest first Latest Oldest Best

 

@Candy875

You need to include the -(hyphen) in your rule match. Changing the line to:

RewriteRule ^([w-]+)/?$ view_post.php?url=


will work.

10% popularity Vote Up Vote Down


 

@Welton855

Obviously it does not work because - is not a word character.

You have to change the w to . which matches any non-newline character.

By doing so pretty much all the URL paths except the homepage will be directed.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme