且构网

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

Mod 将带有查询字符串的重定向 URL 重写为漂亮的 url

更新时间:2023-09-05 13:10:10

您需要检查包含 php 的旧 URL 实际上正在被请求通过与 %{THE_REQUEST} 匹配,否则它将永远重定向循环(例如,用户转到 team.php,重定向到团队,浏览器请求团队,服务器重写为 team.php,服务器看到"team.php"并重定向到团队、浏览器请求团队、服务器重写为 team.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]