Mod 将带有查询字符串的重定向 URL 重写为漂亮的 url [英] Mod Rewrite redirect URL with query string to pretty url

查看:31
本文介绍了Mod 将带有查询字符串的重定向 URL 重写为漂亮的 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 mod rewrite 的帮助下,我成功地将我的 URL 从带有几个查询字符串参数的丑陋 URL 更改为干净的 URL.但是,我的网站有很多网址.我没有返回并编辑每个锚标记上的 href 属性,而是尝试在 .htaccess 文件中编写一个重定向函数,该函数会自动将旧 URL 重定向到新 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?

谢谢

推荐答案

您需要检查包含 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]

这篇关于Mod 将带有查询字符串的重定向 URL 重写为漂亮的 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆