.htaccess Url重写删除查询字符串键


.htaccess Url Rewrite Remove Query-String Keys

我有一组url,比如。。。

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/

并希望将其转换为..

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

基本上,我想从查询字符串中隐藏关键字。

如何做到这一点?有什么帮助吗?

编辑

我每一页都有不同的键,每页大约有25个键。

您可以使用Zend_Controller_Router_Route和/或Zend_Controler_Router_Route_Regex并定义路由

$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);

Front控制器是一种通过单点映射uri的设计模式,Zend使用Front控制器模式。这个映射可以在bootstrap.php上更改。我在谷歌上搜索了一下,发现了这个非常相似的问题。

我将维护默认的.htaccess,它将所有请求(除了img、css、js等前端资源)路由到index.php。然后在引导程序中添加路由来处理重定向。

对于您想要更改为新URL的现有URL,我会创建一个RedirectController,其中包含重定向操作——实际上是用HTTP301代码重定向,而不仅仅是执行相同的操作——到您想要的URL。

有一个有效的论点反对通过路由在应用程序级别实现向下重定向,而不是向上处理.htaccess:这将降低性能,因为需要整个引导/调度周期才能实现重定向。这是一个合理的论点,但我发现将所有路由信息放在一个地方更清楚,而不是在.htaccess和我的应用程序级路由中进行拆分。

试试这个

<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之前。

  1. 尝试统一你的url。这样就更容易使用了。这是一个实际的例子

.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);
?>