.htaccess Url Rewrite Delete 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 I want to convert them like ..

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 the keys from the query string.

How to do it? Any help?

Edit

I have different keys for each page, and there are about 25 keys on the page.

+5
source share
5 answers

You can use Zend_Controller_Router_Route and / or Zend_Controller_Router_Route_Regex and define 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);
+4
source

, uri , Zend . bootstrap.php. googled this questio n, .

+2

.htaccess, ( , img, css, js ..) index.php. .

URL-, URL-, RedirectController , - , HTTP 301, - URL-.

.htaccess: , /. , , , .htaccess, .

+2

<IfModule mod_rewrite.c>
    RewriteEngine on

    #1 www.example.com/1/abc to www.example.com/page/1/name/abc (/number/string)
    RewriteRule ^([0-9]+)/([^/]+)$ /page/$1/name/$2 [L,QSA]

    #2 www.example.com/la/abc to www.example.com/city/la/name/abc (/string/string)
    RewriteRule ^([^/]+)/([^/]+)$ /city/$1/name/$2 [L,QSA]

    #3 www.example.com/1/abc/la to www.example.com/page/1/name/abc/city/la (/number/string/string)
    RewriteRule ^([0-9]+)/([^/]+)/([^/]+)$ /page/$1/name/$2/city/$3 [L,QSA]

    #4 www.example.com/1/ to www.example.com/page/1/ (/number/)
    RewriteRule ^([0-9]+)/$ /page/$1/ [L,QSA]
</IfModule>

. . 1 2. 4 . . 3 , 4.

+2
  • Try to unify your URL. Then it will be easier to work with her. Here is a practical example.

.htaccess :

RewriteEngine on

# Remove trailing '/' from URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} ^(.+[^/])/$
RewriteRule ^(.*)/$ /$1 [R=301,L]

# Convert url to ?q=/US/en/0001
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule ^(.*)$ index.php?q=%1 [NS,L]

# Input: /US/en/0001
# Output print_r($_GET);
# Array
# (
#    [country] => US
#    [locale] => en
#    [pid] => 0001
# )
RewriteCond %{QUERY_STRING} ^q=/([A-Z]{2})/([a-z]{2})/([0-9a-zA-Z]{4})$
RewriteRule ^(.*)$ ?country=%1&locale=%2&pid=%3 [NS,L]

# Input: /US/en
# Output print_r($_GET);
# Array
# (
#    [country] => US
#    [locale] => en
# )
RewriteCond %{QUERY_STRING} ^q=/([A-Z]{2})/([a-z]{2})$
RewriteRule ^(.*)$ ?country=%1&locale=%2 [NS,L]

# Input: /US
# Output print_r($_GET);
# Array
# (
#    [country] => US
# )
RewriteCond %{QUERY_STRING} ^q=/([A-Z]{2})$
RewriteRule ^(.*)$ ?country=%1 [NS,L]

index.php

<?php
    print_r($_GET);
?>
0
source

All Articles