用 .hataccess 重写动态 url 结构 [英] Rewrite dynamic url structure with .hataccess

查看:22
本文介绍了用 .hataccess 重写动态 url 结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've tried to rewrite the dynamic URL (below) to a slightly different structure; either does not work or I am not sure if it is correct:

  • 旧网址 (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

*基本上改变了名称,搜索"字符串不再重要

*basically changed the names and the "search" string is not important any more

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

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

In a second step, after all search engines show the new urls and we finished all test that are easier with non-sef urls, we would like to rewrite URL#11 to 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 文件时,任何帮助将不胜感激.谢谢!

I am not very familiar with apache programming and even when my solution works, we are not sure if it is the right one or if it will generate errors with certain URLs. Any help would be highly appreciated in creating a .htaccess file that does the step 1 redirection and a separate .htaccess file to be used later, for SEF urls. THANK YOU!

推荐答案

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 文件中尝试此操作:

You may try this in one .htacces file in root directory:

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].

If the query contains parameters in this format: %nn, try adding the B flag to both rules. Example: [L,NC,B].

这篇关于用 .hataccess 重写动态 url 结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆