【问题标题】:Mod Rewrite redirect URL with query string to pretty url使用查询字符串将重定向 URL 重写为漂亮的 url
【发布时间】:2012-07-22 17:07:57
【问题描述】:

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

在我的 .htaccess 文件中,我有以下内容:

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]

不过运气不好……有什么想法吗?

谢谢

【问题讨论】:

  • 我是否正确地说您希望点击teams.php?league=.*... 的任何人显示为teams/$1/...

标签: .htaccess mod-rewrite


【解决方案1】:

您需要通过与%{THE_REQUEST} 匹配来检查其中包含php 的旧网址实际上正在被请求,否则它将永远重定向循环(例如,用户转到team.php,服务重定向到团队,浏览器请求团队,服务器重写为teams.php,服务器看到“teams.php”并重定向到团队,浏览器请求团队,服务器重写为teams.php等)

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]

【讨论】:

  • 为什么我们需要 !^(css|js|img)/ 如果我使用你的语法来重定向许多文件,如team.php,我应该复制/粘贴 RewriteCond !^(css| js|img) 每次,或者在 htaccess 中一次就足够了。谢谢:)
  • 提问者不想重写css、js或img文件夹的请求
猜你喜欢
  • 2017-04-25
  • 2020-05-24
  • 1970-01-01
  • 1970-01-01
  • 2010-11-23
  • 1970-01-01
  • 2021-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多