0

我想在 .htaccess 中使用重写规则来重写从子域 ( michigan.sitename.com) 到 url 的访问 - 在这种情况下,http://sitename.org/content/michigan-21st-century-community-learning-centers.

该站点基于 Drupal。

这是我到目前为止所拥有的,前几行来自 Drupal,换行后的所有内容都来自我。

我在尝试子域时收到 404 错误

  RewriteEngine on
  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

  # New information for Michigan sub domain
  RewriteCond %{HTTP_HOST} .
  RewriteCond %{HTTP_HOST} !^michigan.sitename\.com
  RewriteRule (.*) http://www.sitename.com/$1 [R=301, L]

任何意见将是有益的

4

2 回答 2

0

Change your rule from:

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^michigan.sitename\.com
RewriteRule (.*) http://www.sitename.com/$1 [R=301, L]

To:

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} ^michigan\.sitename\.com
RewriteRule (.*) http://www.sitename.com/content/michigan-21st-century-community-learning-centers [R=301,L]

You have a space after the R=301, which mod rewrite won't like, it's going to assume that there's another parameter and puke. Also you want to remove the ! because you want to match the michigan subdomain. The URI is also going to be ignored, meaning if you go to:

http://michigan.sitename.com/
http://michigan.sitename.com/foo/bar
http://michigan.sitename.com/blah.html

will all get redirected to:

http://sitename.org/content/michigan-21st-century-community-learning-centers

You also want to swap the Rewrite URLs of the form 'x' to the form 'index.php?q=x'. rules with the New information for Michigan sub domain so that the michigan redirect happens first, otherwise the request will get routed through index.php. Or, you can add an additional condition to your index.php rule:

RewriteCond %{HTTP_HOST} !^michigan\.sitename\.com
于 2012-09-10T18:28:12.840 回答
0

您必须首先放置密歇根规则(其余的,请参见 jon lin 的回答):

  RewriteEngine on

  # New information for Michigan sub domain
  RewriteCond %{HTTP_HOST} ^michigan\.sitename\.com
  RewriteRule . http://www.sitename.com/content/michigan-21st-century-community-learning-centers [R=301,L]

  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
于 2012-09-11T10:19:01.250 回答