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

2023-09-02 00:19:54 作者:尛爺ミ半生沉浮

我成功地改变了我的网址,从丑陋的人在查询字符串几个参数来清洁寻找那些与mod-rewrite的帮助。不过,也有很多网址为我的网站。而不是回去和编辑在每个href属性,每一个我的一个锚标记,我试着写在.htaccess文件中自动重定向旧的URL到新的重定向功能。

I successfully changed my URLs from ugly ones with several parameters in the querystring to clean looking ones with the help of mod rewrite. However, there are many url's for my site. Rather than go back and edit the href attribute on each and every one of my anchor tags, I tried to write a redirect function in the .htaccess file that automatically redirects the old url to the new one.

在我的.htaccess文件,我有以下几点:

In my .htaccess file, I have the following:

RewriteEngine On

Redirect teams.php?league=$1&team=$2&year=$3&tab=$4 teams/(.*)/(.*)/(.*)/(.*)

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

没有运气,但...什么想法?

No luck though... any thoughts?

感谢

推荐答案

您需要做的检查,旧的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]