: 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:
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!
More posts by @Radia820
2 Comments
Sorted by latest first Latest Oldest Best
You need to include the -(hyphen) in your rule match. Changing the line to:
RewriteRule ^([w-]+)/?$ view_post.php?url=
will work.
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.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.