如何将子域重写为url

时间:2012-09-10 18:22:12

标签: .htaccess rewrite

我想在.htaccess中使用重写规则来重写从子域(michigan.sitename.com)到网址的访问权限 - 在本例中为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]

任何建议都会有所帮助

2 个答案:

答案 0 :(得分:0)

更改您的规则:

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

要:

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

你在R=301,后面有一个空格,mod改写不喜欢,它会假设有另一个参数和呕吐。您还想删除!,因为您想匹配密歇根子域名。 URI也将被忽略,这意味着如果你去:

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

将全部重定向到:

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

您还希望将Rewrite URLs of the form 'x' to the form 'index.php?q=x'.规则与New information for Michigan sub domain交换,以便首先发生密歇根重定向,否则请求将通过index.php进行路由。或者,您可以为index.php规则添加其他条件:

RewriteCond %{HTTP_HOST} !^michigan\.sitename\.com

答案 1 :(得分: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]