Morgan Green Morgan Green - 6 months ago 56
Apache Configuration Question

Can I allow hyphen in URLs using htaccess for dynamically loaded pages?

I'm creating a forum, and after sanitizing the slugs I want them to look like so for the url slug

, I have everything called from the index.php file and it simply rewrites the urls. Is there anyway to allow the - to be used in the dynamic call? Right now they simply return a 404 error.
My current .htaccess file

RewriteEngine On

# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^(\w+)/?$ index.php?ident=$1 [L,QSA]
RewriteRule ^(\w+)/(\w+)/?$ index.php?ident=$1&ident2=$2
RewriteRule ^(\w+)/(\w+)/(\w+)/?$ index.php?ident=$1&ident2=$2&ident3=$3 [L,QSA]
RewriteRule ^(\w+)/(\w+)/(\w+)/(\w+)/?$ index.php?ident=$1&ident2=$2&ident3=$3&ident4=$4 [L,QSA]
RewriteRule ^(\w+)/(\w+)/(\w+)/(\w+)/(\w+)/?$ index.php?ident=$1&ident2=$2&ident3=$3&ident4=$4&ident5=$5 [L,QSA]
RewriteRule ^(\w+)/(\w+)/(\w+)/(\w+)/(\w+)/(\w+)/?$ index.php?ident=$1&ident2=$2&ident3=$3&ident4=$4&ident5=$5&ident6=$6 [L,QSA]
RewriteRule ^(\w+)/(\w+)/(\w+)/(\w+)/(\w+)/(\w+)/(\w+)/?$ index.php?ident=$1&ident2=$2&ident3=$3&ident4=$4&ident5=$5&ident6=$6&ident7=$7 [L,QSA]

php_value max_execution_time 300


\w is a PCRE regex base class for (a-z 0-9 _). It doesn't match hyphen - characters. Only letters from a to z, number characters from 0 through 9 and underscore characters _. You will need to specify the hyphen yourself using [a-z0-9-]+. Unless you also want the underscore in there.