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?
The last rule is causing a 500 Internal Server Error. What is causing this?
It's a loop. Your rule's target, /categories/
matches the pattern ^categories/([^?][^/]*)(/)?$
, since all that stuff after the /
is optional. Try making the *
a +
to force at least 1 character there:
RewriteRule ^categories/([^?][^/]+)(/)?$ /categories/?category-type=$1 [NC,L]
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.
Your regex in last rules appears to be faulty. Try these rules in your DOCUMENT_ROOT/.htaccess
file:
RewriteEngine On
RewriteRule ^categories/([^/]+)/([^/]+)/([^/]+)/?$ /categories/?category-type=$1&category=$2&sub-category=$3 [NC,QSA,L]
RewriteRule ^categories/([^/]+)/([^/]+)/?$ /categories/?category-type=$1&category=$2 [NC,QSA,L]
RewriteRule ^categories/([^/]+)/?$ /categories/?category-type=$1 [NC,QSA,L]
Your last rewrite falls into a loop which it can't get out! what you need is change your rules in a way to avoid such a situation, like putting a condition:
RewriteRule ^categories/([^/]*)/([^/]*)/([^/]+)(/)?$ /categories/?category-type=$1&category=$2&sub-category=$3 [NC,L]
RewriteRule ^categories/([^/]*)/([^/]+)(/)?$ /categories/?category-type=$1&category-=$2 [NC,L]
RewriteCond %{QUERY_STRING} !category-type
RewriteRule ^categories/([^\/]+)/?$ /categories/?category-type=$1 [NC,L]
in this way, it won't rewrite URL if there is category-type
in query string!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With