htaccess:将旧域和所有页面重定向到新域

pvl*_*oux 8 regex apache .htaccess mod-rewrite expressionengine

我知道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]
Run Code Online (Sandbox Code Playgroud)

它在我调用根URL时正确地重定向,但在我调用页面时却没有.我究竟做错了什么?

提前致谢!

光伏

rjb*_*rjb 9

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

要将旧域重定向到新域,您需要将该规则放在您的.htaccesshttpd.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>
Run Code Online (Sandbox Code Playgroud)

如果要重定向整个站点,以下规则将执行此操作:

<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>
Run Code Online (Sandbox Code Playgroud)

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

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


当我们讨论这个主题时,作为EE 2.2版本的一部分,EllisLab现在"正式"提供有限的技术支持,用于从ExpressionEngine URL中删除index.php.

只需将您的代码添加或更新为以下内容,请务必考虑您已有的任何规则:

<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>
Run Code Online (Sandbox Code Playgroud)