Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess Url Rewrite Remove Query-String Keys

I have a set of urls like...

www.example.com/page/1/name/abc
www.example.com/city/la/name/abc
www.example.com/page/1/name/abc/city/la
www.example.com/page/1/

And want to convert them as..

www.example.com/1/abc
www.example.com/la/abc
www.example.com/1/abc/la
www.example.com/1/

Basically i want to hide keys from query string.

How to do this? Any Help?

Edit

I have different keys for every page and there are about 25 keys per page.

like image 250
Ashwini Agarwal Avatar asked Sep 03 '12 06:09

Ashwini Agarwal


People also ask

What is RewriteCond and RewriteRule?

There are two main directive of this module: RewriteCond & RewriteRule . RewriteRule is used to rewrite the url as the name signifies if all the conditions defined in RewriteCond are matching. One or more RewriteCond can precede a RewriteRule directive.

What is htaccess RewriteCond?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.


2 Answers

You can use Zend_Controller_Router_Route and/or Zend_Controller_Router_Route_Regex and define the routes

$route = new Zend_Controller_Router_Route(
    ':key/:city_name',
    array(
        'controller' => 'somecontroller',
        'action'     => 'pageAction'
    ),
    array('key' => '^\d+$')
);
$router->addRoute('page', $route); // www.example.com/1/abc

$route = new Zend_Controller_Router_Route(
    ':key/:city_name',
    array(
        'controller' => 'somecontroller',
        'action'     => 'cityAction'
    ),
    array('key' => '^\d+\w+$') // www.example.com/1a/abc
);
$router->addRoute('city', $route);
like image 131
Maks3w Avatar answered Sep 28 '22 23:09

Maks3w


Front controller is a design pattern which maps uri through single point and Zend is using front controller pattern. This maping can be change at bootstrap.php. I googled and found this question which is pretty similar.

like image 39
Ruwantha Avatar answered Sep 28 '22 23:09

Ruwantha