且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

现代重写重定向的URL与查询字符串,以pretty的网址

更新时间:2023-11-07 22:32:46

您需要做的检查,旧的URL与它的 PHP 实际上是被请求的匹配对%{THE_REQUEST} ,否则它会永远循环重定向(例如用户进入team.php,服务重定向到队,浏览器请求团队,服务器改写为teams.php,服务器发现teams.php,并重定向到队,浏览器请求的团队,服务器改写为teams.php,等等等等。)

You need to do a check that the old URL with the php in it is actually being requested by matching against %{THE_REQUEST}, otherwise it'll redirect loop forever (e.g. user goes to team.php, serve redirects to teams, browser requests teams, server rewrites as teams.php, server sees "teams.php" and redirects to teams, browser requests teams, server rewrites as teams.php, etc. etc.)

RewriteEngine On

# redirect when the user actually requests for teams.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /teams\.php\?league=([^&]+)&team=([^&]+)&year=([^&]+)&tab=([^\ ]+)
RewriteRule ^teams\.php$ /teams/%1/%2/%3/%4? [R=301,L]

# internally rewrite any /teams/ URI
RewriteCond %{REQUEST_URI} !^(css|js|img)/
RewriteRule ^teams/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ teams.php?league=$1&team=$2&year=$3&tab=$4 [L]