im building a site with lots of parameters. At the moment i'm using this Code in my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /epo
RewriteRule (.*)/(.*)/(.*)/(.*)/(.*)/$ index.php?section=$1&content=$2&site=$3¶m=$4¶m2=$5 [QSA]
RewriteRule (.*)/(.*)/(.*)/(.*)/$ index.php?section=$1&content=$2&site=$3&subsite=$4 [QSA]
RewriteRule (.*)/(.*)/(.*)/$ index.php?section=$1&content=$2&site=$3 [QSA]
RewriteRule (.*)/(.*)/$ index.php?section=$1&content=$2 [QSA]
RewriteRule (.*)/$ index.php?section=$1 [QSA]
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]
I'm new to mod_rewrite, thats why this code is a mess. Is there any better way to deal with all these parameters? The last two lines are just there to add a "/" at the end in case there is none. Would be also great if someone could explain their code, so i can understand what i did wrong :)
Personally I redirect all request to a single file and then handle it from there.
And then in index.php use something like
Keep in mind that you do need some extra validation on all parameters
It is similar to Hugo's example but without
GET
param:in
PHP
you can do following:Your url's can now look something like:
OR (if mod rewrite is not enabled)
In both ways the
$url
variable looks like/param1/param2
You can do
explode
on this string or feed a PHP routing library with this string for extracting your parameters.PHP routing example libraries:
https://github.com/robap/php-router
https://github.com/deceze/Kunststube-Router