How do I configure .htaccess to accept hyphen in the URL?

6

I'm customizing the URLs of my site, but .htaccess does not accept URLs with hyphens, for example:

postagem/1/criando-efeito-fadeout-com-javascript

When I write the title without the hyphens works, but with them not, why? How do I resolve this?

My .htaccess rule is written like this:

RewriteRule ^postagem/([0-9]+)(/([a-z]+)/)?$ ler.php?post=$1&titulo=$2 [NC,L]
    
asked by anonymous 12.02.2014 / 17:26

1 answer

5

Your regular expression contains the [a-z] stretch, defining that only letters are accepted.

Add the hyphen as follows: [a-z\-] . You get the expression:

RewriteRule ^postagem/([0-9]+)(/([a-z\-]+)/)?$ ler.php?post=$1&titulo=$2 [NC,L]

Optionally, you could include numbers like this:

RewriteRule ^postagem/([0-9]+)(/([a-z0-9\-]+)/)?$ ler.php?post=$1&titulo=$2 [NC,L]

Note: Since the hyphen is a special character for the regular expression, I have added a escape     

12.02.2014 / 17:31