【问题标题】:Mod Rewrite - redirect to a subdomain except for certain URL (codeigniter)Mod Rewrite - 重定向到除特定 URL 之外的子域(codeigniter)
【发布时间】:2012-12-29 04:24:33
【问题描述】:

我正在尝试将所有流量重定向到子域。

www.example.com、example.com、sub.othersite.com 全部重写到 sub.othersite.com

效果很好。现在我需要排除单个 URL 重定向到子域

所以如果 www.example.com/THE/URI 进来,我希望 mod rewrite 忽略子域重写并将其发送到主域。

看起来很简单,但我无法让它工作,因为我有一个额外的重写,以确保所有域(也有几个停放的域)汇集到一个域中以提供内容。

我还有一些其他的重写条件。它们都列在这里。如果有人能指出我正确的方向 - 我基本上需要这样做:

如果传入请求是针对 /THE/URI 然后去 www.example.com/THE/URI 否则 重写到 sub.othersite.com

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  # ignore system folder
  RewriteCond %{REQUEST_URI} ^system.*
  RewriteCond $1 !^(client/*)   
  RewriteRule ^(.*)$ /index.php?/$1 [L]

  #Checks to see if the user is attempting to access a valid file,
  #such as an image or css document, if this isn't true it sends the
  #request to index.php
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond $1 !^(index\.php|images|robots\.txt|css)
  RewriteRule ^(.*)$ index.php?/$1 [L] 

  # if it is the SPECIAL URI redirect and EXIT
  RewriteCond $1 !^(/THE/URI)   
  RewriteCond %{HTTP_HOST} !^www.example.com$
  RewriteRule ^(.*)$ http://www.example.com/THE/URI [L]

  #catch all other traffic        
  RewriteCond %{HTTP_HOST} !^example.anothersite.com$
  RewriteRule ^(.*)$ http://example.anothersite.us/$1 


</IfModule> 

【问题讨论】:

    标签: apache mod-rewrite subdomain


    【解决方案1】:

    解决了。 答案很简单。我只需要从最终重写中排除 URI 和 QUERY_STRING,因为重写的上半部分在 URL 中附加了一个查询字符串。

    为了让我的应用程序正常工作,我仍然需要进行初始重写(获取任何 URI 并在内部将其映射到 index.php?URI)

    最终代码:

    <IfModule mod_rewrite.c>
            RewriteEngine On
            RewriteBase /
    
            #Removes access to the system folder by users.
            RewriteCond %{REQUEST_URI} ^system.*
            RewriteCond $1 !^(client/*)   
            RewriteRule ^(.*)$ /index.php?/$1 [L]
    
                    # our URL is prepped now - query string appended, so let's redirect it
                    # ONLY if it's not the ONE URL we need to keep
    
            RewriteCond %{REQUEST_URI} !^/THE/URI   
            RewriteCond %{QUERY_STRING} !^/THE/URI
            RewriteCond %{HTTP_HOST} !^sub.example.com$
            RewriteRule ^(.*)$ http://sub.example.com/$1  [R=302]
    </IfModule>
    

    【讨论】:

      猜你喜欢
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      • 2011-05-06
      • 2017-02-20
      相关资源
      最近更新 更多