Vince Kronlein Vince Kronlein - 1 month ago 13
PHP Question

Dynamically Parse URL in .htaccess

Is it possible to do an odd and even replacement in

.htaccess
to replace
/
in a url with
= &
?

In other words I have a link like so:

page/subscriber/action/manage/sortby/id


and I'm wondering if there's way to simply replace the odd numbered
/
with
=
and the even
/
with
&
?

I have some urls that are very long with lots of arguments and I don't want to have to write a specific rule for each one.

I'm trying to convert over a very large PHP script to nicely formatted urls and it would take weeks to find every possible url and argument combination.

Answer

Why don't you want to make it in PHP script itself? I think I saw this for the first time in Zend framework, but now using similar approach in my projects.

.htaccess:

RewriteEngine on
RewriteRule .* index.php

index.php

$path = $_SERVER['REQUEST_URI'];
$paths = explode('/', path);
// Add some logic for showing the page you need. Maybe remove domain before processing

For example you can use even arguments as array keys and add argument as value for these keys.