Getting A 500 Internal Server Error in my URL Rewrite Rules

.htaccess, mod-rewrite, regex, url-rewriting

Solution

We could be here forever triying to fix it, but as soon as you create some new url type, you will go cazy again. I use to have complex httacess in my old websites, but since a couple years ago, i dont put much logic in the httacces, an delegate all to a php router.

This is a much more flexible solution, you can create more complicated rules, it is easier to change everything, and also works perfectly if you change the server, install the app in another server or in another folder or add new routes.

This is the htacess of Zend Framework 2, that is more or less like the one i'm using:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

All the rules means that if the request is not an actual file or directory, just send it to index.php

There, in index php you parse the actual url and fill your $_GET variables.

Problem

If have the following possible urls: ``` http://stackoverflow.com/categories/ http://stackoverflow.com/categories/category-type http://stackoverflow.com/categories/category-type/category http://stackoverflow.com/categories/category-type/category/sub-category ``` which I want to rewrite to ``` http://stackoverflow.com/categories/ http://stackoverflow.com/categories/?category-type=category-type http://stackoverflow.com/categories/?category-type=category-type&category=category http://stackoverflow.com/categories/?category-type=category-type&category=category&sub-category=sub-category ``` And here are my rewrite rules: ``` RewriteRule ^categories/([^/]*)/([^/]*)/([^/]+)(/)?$ /categories/?category-type=$1&category=$2&sub-category=$3 [NC,L] RewriteRule ^categories/([^/]*)/([^/]+)(/)?$ /categories/?category-category=$1&category-=$2 [NC,L] RewriteRule ^categories/([^?][^/]*)(/)?$ /categories/?category-type=$1 [NC,L] ``` The last rule is causing a `500 Internal Server Error`. What is causing this?

Original source