如何在不影响php代码的情况下用htaccess压平查询字符串


How to flatten a query string with htacess without affecting how php code

我想要一个类似的url

"www.site.com/?u=1"转换为"www.site.com/s"

不影响我的php脚本。

我还想了解这种转换是如何以及何时在服务器上发生的。

以下是我在查询字符串上所做的尝试,但没有成功

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

谢谢。

您不希望在重定向之前使用u规则。这些需要在重定向后发生:

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]
#///////////////////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]