Rewrite dynamic url structure with .hataccess

后端 未结 3 1255
轮回少年
轮回少年 2021-01-24 23:07

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.ph
相关标签:
3条回答
  • 2021-01-24 23:16

    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]
    
    0 讨论(0)
  • 2021-01-24 23:20

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

    0 讨论(0)
  • 2021-01-24 23:32

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

    0 讨论(0)
提交回复
热议问题