apachequery-stringclean-urls

Add hyphen to clean URL with htaccess


I am following the single page approach for my website, all the GET requests were built like: index.php?ressource=product&identifier=5

To achieve clean URLs, I have found a generator which created this:

RewriteEngine On
RewriteCond %{HTTPS} !^on$ [NC]
RewriteRule . https://%{HTTP_HOST}/ [L] HTTPS_HOST ???

RewriteEngine On
RewriteRule ^([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ ?ressource=$1&identifier=$2&action=$3 [L]
RewriteRule ^([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ ?ressource=$1&identifier=$2 [L]

#EOF

I have added the first three lines to force the user's client to go to my https site.

Now I also would like to have a hyphen within the identifier value, something like article/terms-of-service.

I did some research and I need to either use the NC, QSA or both flags, but whenever I add one of those flags I get the Server Error 500.


Solution

  • Okay, i have found the solution to add hyphen within the clean urls:

    RewriteEngine On
    RewriteRule ^([a-z]+)/([0-9a-üA-Ü\-]+)/([a-z]+)$ ?ressource=$1&identifier=$2&kwargs=$3 [L]
    RewriteRule ^([a-z]+)/([0-9a-üA-Ü\-\%\s]+)$ ?ressource=$1&identifier=$2 [L]
    RewriteRule ^([a-z]+)$ ?ressource=$1 [L]
    

    the hyphen character had to be added to the regular expression