当._request以列表中的字符串开头时,.htaccess重写请求

时间:2016-03-24 16:49:30

标签: apache .htaccess mod-rewrite

我必须在.htaccess文件(Apache Webserver)中实现以下重写规则,但不知道从哪里开始以及什么是最好的approch。 但实际上它应该做到以下几点:

伪代码:

  if (path begins with "patternA", "patternB" or "PatternC") {

      if (path matches "patternA")
          redirect to /xxxxxx-a
      else if (path matches "patternB")
          redirect to /xxxxxx-b
      else if (path matches "patternC")
          redirect to /xxxxxx-c             
  }

提前致谢!

2 个答案:

答案 0 :(得分:1)

您可以在root .htaccess中使用这些规则:

RewriteEngine On

RewriteCond %{THE_REQUEST} /patternA [NC]
RewriteRule ^ /xxxxxx-a [L,R=302]

RewriteCond %{THE_REQUEST} /patternB [NC]
RewriteRule ^ /xxxxxx-b [L,R=302]

RewriteCond %{THE_REQUEST} /patternC [NC]
RewriteRule ^ /xxxxxx-c [L,R=302]

答案 1 :(得分:0)

Apache docs是一个很好的起点,寻找RewriteRule。

基本示例:

RewriteEngine On
RewriteRule ^patternA /xxxxxx-a [L]   # internal redirect, invisible to visitor
RewriteRule ^patternB /xxxxxx-b [R,L] # external redirect, visible to visitor

等。您可能需要将AllowOverride添加到httpd conf以允许使用.htaccess文件。