正则表达式 – htaccess:将旧域和所有页面重定向到新域

我知道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时正确地重定向,但在我调用页面时却没有.我究竟做错了什么?

提前致谢!

光伏

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

要将旧域重定向到新域,您需要将该规则放在.htaccess或httpd.conf文件中的第一个 – 所有其他规则应该出现在它之后.

如果您只想重定向某个目录,则以下规则将执行此操作,同时允许该站点的其余部分正常运行:

<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>

如果您关心让抓取工具知道您的内容已移动并希望尽可能无缝地进行转换,请确保将301 Redirect标记保留在RewriteRule中.

这将确保用户搜索引擎定向到正确的页面.

虽然我们正在讨论这个问题,但作为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>

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...