【问题标题】:Rewrite dynamic url structure with .hataccess用 .htaccess 重写动态 url 结构
【发布时间】:2013-03-15 09:48:42
【问题描述】:

我尝试将动态 URL(如下)重写为稍微不同的结构;要么不起作用,要么我不确定它是否正确:

  • 旧网址 (URL#1):index.php?lang=AAA&zone=BBB&city=CCC&str=DDD&search=EEE
  • 新网址 (URL#11):index.php?lang=AAA&country=BBB&place=CCC&street=DDD

*基本上改了名字,“搜索”字符串不再重要了

  • 我想要实现的是将所有访问者从(旧)动态 URL#1 重定向到(新)动态 URL#11

第二步,在所有搜索引擎显示新的 url 并且我们完成了所有使用非 sef url 更容易的测试之后,我们想将 URL#11 重写为 URL#2

  • 新网址 (URL#11):index.php?lang=AAA&country=BBB&place=CCC&street=DDD
  • Sef URL (URL#2):/AAA/BBB/CCC/DDD

我对 apache 编程不是很熟悉,即使我的解决方案有效,我们也不确定它是否正确,或者它是否会在某些 URL 上产生错误。在创建执行步骤 1 重定向的 .htaccess 文件和稍后用于 SEF url 的单独 .htaccess 文件时,任何帮助都将受到高度赞赏。谢谢!

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting sef


    【解决方案1】:

    http://example.com/index.php?lang=AAA&zone=BBB&city=CCC&str=DDD&search=EEE

    重定向到:

    http://example.com/AAA/BBB/CCC/DDD

    默默地映射到:

    http://example.com/index.php?lang=AAA&country=BBB&place=CCC&street=DDD

    您可以在根目录中的一个 .htacces 文件中尝试此操作:

    Options +FollowSymlinks -MultiViews
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{QUERY_STRING} lang=([^&]+)&zone=([^&]+)&city=([^&]+)&str=([^&]+)&search=  [NC]
    RewriteRule .* /%1/%2/%3/%4?   [R=301,L,NC]
    
    RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?  [NC]
    RewriteCond %{REQUEST_URI} !index\.php                          [NC]
    RewriteRule .* /index.php?lang=%1&country=%2&place=%3&street=%4 [L,NC]
    

    如果查询包含以下格式的参数:%nn,请尝试将 B 标志添加到两个规则中。示例:[L,NC,B]。

    【讨论】:

    • 这个成功了!我们认为还需要对 php 脚本进行一些更改,而仅通过 .htaccess 是不可能的。但是,您建议的方法是有效的,谢谢! Options +FollowSymlinks -MultiViews RewriteEngine On RewriteBase / RewriteCond %{QUERY_STRING} lang=([^&]+)&zone=([^&]+)&city=([^&]+)&str=([^&]+)&search= [NC] RewriteRule .* /index.php?lang=%1&country=%2&place=%3&street=%4 [L,NC]
    【解决方案2】:

    假设你的变量只包含小写字母和数字,试试这个:

    RewriteRule ^/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)$
        /index.php?lang=$1&country=$2&place=$3&street=$4
    RewriteCond %{QUERY_STRING}
        ^lang=([a-z0-9]+)&zone=([a-z0-9]+)&city=([a-z0-9]+)&str=([a-z0-9]+)&search=[a-z0-9]+$
    RewriteRule ^index.php$
        /index.php?lang=%1&country=%2&place=%3&street=%4 [R=permanent]
    

    (请注意,必须没有换行符,但我在此处插入了缩进,只是为了使该块在此网页上可读。)

    【讨论】:

    • 不幸的是,URL 都是大写和小写以及空格 (%20),甚至连破折号、句点、连字符和加号等标点符号:- index.php?lang=en&zone= Zone109&city=229MIA%20Florida&str=7800%20NW%205th%20Ct%20bl.%2021,%2022,%2022A
    【解决方案3】:

    这是有效的代码(第一步),来自faa

    Options +FollowSymlinks -MultiViews
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{QUERY_STRING} lang=([^&]+)&zone=([^&]+)&city=([^&]+)&str=([^&]+)&search=  [NC]
    RewriteRule .* /index.php?lang=%1&country=%2&place=%3&street=%4 [L,NC]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多