Search code examples
apache.htaccess

How to allow hyphens in htaccess rewrite?


I am having problems creating clean urls containing a hyphen.

I want the url to be /services/baking-cake, and I would like to redirect to the version of the url without a trailing slash.

My .htaccess now has these rules:

RewriteRule services/(.*)/ services/$1 [L,R=301]
RewriteRule services/(.*) services.php?page=$1

This works fine for the query-string baking, but not for baking-cake. In that case, when a trailing slash is added, it jumps to:

/services.php/baking-cake?page=baking-cake/

How do I change the match?


Solution

  • You just need is these 2 simple rules:

    RewriteRule ^(.*)/$ $1 [L,R]
    
    RewriteRule ^services/(.*)/?$ services.php?page=$1 [L,QSA]