【问题标题】:htaccess: redirect old domain and all pages to a new domainhtaccess:将旧域和所有页面重定向到新域
【发布时间】:2011-09-24 22:49:46
【问题描述】:

我知道 Stackoverflow 上有很多示例,但我仍然错过了一些东西。

我正在尝试使用以下规则将 http://old.domain.com/fr/ 重定向到 http://brand.new-domain.com/fr/,但这不起作用:

# Enable Rewrite Engine
RewriteEngine On
RewriteBase /

# Add a trailing slash to paths without an extension
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]

# Redirect domain
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^old.domain.com [OR]
RewriteCond %{HTTP_HOST} ^other-old.domain.com [NC]
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [r=301,L]

# Remove index.php
# Uses the "exclude method"
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Exclude_List_Method
# This method seems to work best for us, you might also use the include method.
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Include_List_Method
# Exclude root files
RewriteCond $1 !^(index\.php) [NC]
# Exclude EE folders
RewriteCond $1 !^(assets|ee-admin|images|templates|themes|fr|nl)/ [NC]
# Exclude user created folders
RewriteCond $1 !^(assets|css|img|js|swf|uploads)/ [NC]
# Exlude favico, robots, ipad icon
RewriteCond $1 !^(favicon\.ico|robots\.txt|pple-touch-icon\.png) [NC]
# Remove index.php
RewriteCond %{QUERY_STRING} !^(ACT=.*)$ [NC]
RewriteCond %{QUERY_STRING} !^(URL=.*)$ [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]

当我调用根 URL 时它会正确重定向,但当我调用页面时它不会。我做错了什么?

提前致谢!

Pv

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite expressionengine


    【解决方案1】:

    在尝试 hacky-mod-rewrite 之前,您是否尝试过使用 mod_alias 简单重定向指令(您拥有的核心模块)?

    我会用ServerName old.domain.com 做一个虚拟主机,在这个 VH 中我会添加这个规则:

    Redirect /fr http://brand.new-domain.com/fr
    

    来自文档:

    那么任何以 URL-Path 开头的请求都会在目标 URL 的位置向客户端返回一个重定向请求。匹配的 URL-Path 之外的其他路径信息将附加到目标 URL。

    所以为brand.new-domain.com(带有ServerName brand.new-domain.com)获取一个单独的虚拟主机,并且不要在这个虚拟主机中设置重定向规则。

    如果您仍想在同一个 VirtualHost 中处理 2 个域,那么您将不得不使用 mod-rewrite,因为即使 RedirectMatch 也无法检查查询中的请求域。

    【讨论】:

      【解决方案2】:

      在编写mod_rewrite 规则时,规则会按照它们出现的顺序应用。

      要将旧域重定向到新域,您需要在 .htaccesshttpd.conf 文件中将该规则放在首位 - 所有其他规则都应出现在其之后。 p>

      如果您只想重定向某个目录,以下规则会这样做,同时允许站点的其余部分正常运行:

      <IfModule mod_rewrite.c>
          RewriteEngine On
      
          # Redirect Only Matching Directories
          RewriteCond %{REQUEST_URI} ^/(fr|fr/.*)$
          RewriteRule ^(.*)$ http://brand.new-domain.com/fr/$1 [R=301,L]
      </IfModule>
      

      如果你想重定向整个网站,下面的规则会这样做:

      <IfModule mod_rewrite.c>
          RewriteEngine On
      
          # Redirect Entire Site to New Domain
          RewriteCond %{HTTP_HOST} ^old.domain.com$ [OR]
          RewriteCond %{HTTP_HOST} ^other-old.domain.com$ [NC]
          RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [R=301,L]
      </IfModule>
      

      如果您想让抓取工具知道您的内容已移动并希望尽可能无缝地进行转换,请务必在 RewriteRule 中保留 301 Redirect 标志。

      这将确保用户和搜索引擎被引导到正确的页面。


      虽然我们正在讨论这个主题,但作为 EE 2.2 版本的一部分,EllisLab 现在“正式”为removing index.php from ExpressionEngine URLs 提供有限的技术支持。

      只需将您的代码添加或更新为以下内容,确保考虑您可能已经制定的任何规则:

      <IfModule mod_rewrite.c>
          RewriteEngine On
      
          # Removes index.php
          RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ /index.php/$1 [L]
      
          # If 404s, "No Input File" or every URL returns the same thing
          # make it /index.php?/$1 above (add the question mark)
      </IfModule>
      

      【讨论】:

        【解决方案3】:

        尝试使用以下 ruke 作为第一个:

        # Redirect domain
        Options +FollowSymLinks
        RewriteCond %{HTTP_HOST} ^old.domain.com [OR]
        RewriteCond %{HTTP_HOST} ^other-old.domain.com [NC]
        RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [R=301,L]
        

        还要注意大写 R 是小写 redirect 的缩写形式。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-07-17
          • 2010-12-29
          • 1970-01-01
          • 2014-02-14
          • 2013-02-10
          • 2016-06-24
          • 2021-11-28
          • 2011-11-04
          相关资源
          最近更新 更多