Rewrite URL so that query string is in path

爷,独闯天下 提交于 2019-12-25 05:33:07

问题


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

To rewrite to this: /faculty/FirstLast

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]

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.

Outcome:

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]

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

All good!


回答1:


Try this instead:

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

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

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.



来源:https://stackoverflow.com/questions/16020967/rewrite-url-so-that-query-string-is-in-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!