【问题标题】:How to flatten url with more than one query string using htacess如何使用 htaccess 将包含多个查询字符串的 url 展平
【发布时间】:2014-10-05 19:01:36
【问题描述】:

我问了this 问题并得到了很好的答案,只是我仍然不明白这是怎么发生的。也许有人可以解释这个过程以及每个标志的作用。

所以现在我有,

site.com/index.php?query1=this&query2=that&query3=those and i want this translated into

site.com/song/this/that/those

这是现有的代码。

Options -Indexes +FollowSymLinks
RewriteEngine On
DirectoryIndex index.php
#/////////////append www before all urls///////////////////////////
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{THE_REQUEST} \ /+index\.php\?u=([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
#/////////////song
#///////////////////remove index.php from url/////////////
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ )
RewriteRule ^ /%1 [R=301,L]
#////////////Convert 'u' Query String////////////
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?u=$1 [L]

【问题讨论】:

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


    【解决方案1】:

    您需要再添加 2 个规则来处理 3 个查询字符串(如果您还想处理 2 个查询字符串,则需要再添加 4 个)。

    检查%{THE_REQUEST} 的规则会查看HTTP 请求中的URL,并匹配查询字符串(u=([^&\ ]+))并重定向浏览器,以便新的URL 显示在浏览器的位置栏中。集合中的第二条规则在内部将请求 back 重写为具有查询字符串,因为您的资源位于 index.php

    所以你要添加(根据RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301] 规则):

    RewriteCond %{THE_REQUEST} \ /+index\.php\?query1=([^&]+)&query2=([^&]+)&query3=([^&\ ]+)
    RewriteRule ^ /%1/%2/%3? [L,R=301]
    RewriteCond %{THE_REQUEST} \ /+index\.php\?query1=([^&]+)&query2=([^&\ ]+)
    RewriteRule ^ /%1/%2? [L,R=301]
    

    重定向浏览器。

    然后内部重写请求,将这些规则添加到末尾:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?query1=$1&query2=$2&query3=$3 [L,QSA]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([^/]+)$ index.php?query1=$1&query2=$2 [L,QSA]
    

    【讨论】:

      猜你喜欢
      • 2022-01-26
      • 1970-01-01
      • 2011-05-14
      • 1970-01-01
      • 2016-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多