重写URL,以便查询字符串位于路径中 [英] Rewrite URL so that query string is in path

查看:68
本文介绍了重写URL,以便查询字符串位于路径中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取以下路径:/faculty/index.php?PID=FirstLast&type=alpha

I'm trying to get the following path: /faculty/index.php?PID=FirstLast&type=alpha

要重写为此:/faculty/FirstLast

我正确地假设以下内容可以放入.htaccess吗?

Am I correct to assume the following would be acceptable to put in .htaccess?

# Rewrite old URLS
RewriteCond %{QUERY_STRING} ^PID=([0-9a-zA-Z]*)$
RewriteRule ^/faculty/index.php$ /faculty/%1 [R=302,L]

我可以丢弃其他任何查询字符串变量.我正在.htaccess文件级别应用这些规则.该项目是从旧系统到Drupal的迁移.

I'm okay to throw away any other query string variables. I'm applying these rules at the .htaccess file level. This project is a migration from an older system into Drupal.

我的.htaccess看起来像

My .htaccess looks like

  # Rewrite old  URLS
  RewriteCond %{QUERY_STRING} PID=([0-9a-zA-Z]*)
  RewriteRule ^faculty/ /faculty/%1/? [R=301,L]

  RewriteCond %{QUERY_STRING} vidID=([0-9]*)
  RewriteRule ^videos/ /video/id/%1/? [R=301,L]

我也发现了这个很棒的工具-一个mod_rewrite测试器 http://htaccess.madewithlove.be/

I also found this wonderful tool -- a mod_rewrite tester http://htaccess.madewithlove.be/

一切都好!

推荐答案

尝试以下方法:

RewriteRule ^faculty/index.php$ /faculty/%1? [R=302,L]

前导斜杠不在规则测试的URI路径中,因此也不能在正则表达式中.

The leading slash is not in the URI-path tested in the rule, so can't be in the regex either.

除非在规则中创建新查询,否则查询会自动附加到替代URL(不更改地传递),因此在使用规则时,尾随问号?会删除现有查询字符串.

As the query is automatically appended to the substitution URL (passed through unchanged) unless a new query is created in the rule, the trailing question mark ? erases the existing query string when the rule is used.

这篇关于重写URL,以便查询字符串位于路径中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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