Get a free trial // Grow your CSS skills // Land your dream job

Help with htaccess rewrite not liking dashes

  • # July 17, 2013 at 1:12 pm

    I use the following rewrite rule to behind the scenes rewrite addresses such as /pageName and /pageName/ to index.php?page=pageName. It works perfectly except when the pageName contains a -.

    RewriteEngine On
    RewriteRule ^(w+)$ /index.php?page=$1
    RewriteRule ^(w+)/$ /index.php?page=$1

    How can I get these rewrite rules to accept any input (or at least input containing A-Z, 1-9, and -‘s.

    Thank you.

    # July 17, 2013 at 9:34 pm

    use a character class. For example, if you want “word” characters and dashes, try:


    Also, you don’t need two rules. Use `?` to make the trailing slash optional:

    RewriteRule ^([-w]+)/?$ /index.php?page=$1

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed