Rewrite dynamic url structure with .hataccess

心已入冬 提交于 2019-12-02 12:38:28

问题


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:

  • Old URL (URL#1): index.php?lang=AAA&zone=BBB&city=CCC&str=DDD&search=EEE
  • New URL (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

  • what I am trying to achieve is redirecting all visitors from (old) dynamic URL#1 to (new) dynamic URL#11

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

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

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!


回答1:


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

redirected to:

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

silently mapped to:

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

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]

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




回答2:


Assuming your variables only ever consist of lowercase letters and digits, try this:

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]

(Note that there must be no line breaks, but I've inserted them here with indents just to make the block readable on this webpage.)




回答3:


Here is the code that worked (first step), from 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]


来源:https://stackoverflow.com/questions/15622846/rewrite-dynamic-url-structure-with-hataccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!