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

发布于 2024-11-17 16:20:50 字数 1565 浏览 3 评论 0原文

我知道 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]

当我调用root URL,但当我调用页面时则不然。我做错了什么?

提前致谢!

光伏

I know that there is a lot of examples on Stackoverflow but I still miss something.

I'm trying to redirect http://old.domain.com/fr/ to http://brand.new-domain.com/fr/ with the following rules, but that doesn't work:

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

It correctly redirect when I call the root URL, but not when I call a page. What am I doing wrong?

Thanks in advance!

Pv

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里°也失望 2024-11-24 16:20:50

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

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

<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 RewriteRule 中的重定向 标志。

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


当我们讨论这个主题时,作为 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>

When writing mod_rewrite rules, the rules get applied in the order that they appear.

To redirect an old domain to a new domain, you'll want that rule to be first in your .htaccess or httpd.conf file — all other rules should appear after it.

If you only want to redirect a certain directory, the following rule will do so, while allowing the rest of the site to function normally:

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

If you want to redirect the entire site, the following rule will do so:

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

If you care about letting crawlers know your content has moved and want to make the transition as seamless as possible, be sure to keep the 301 Redirect flag in the RewriteRule.

This will ensure that users and search engines are directed to the correct page.


While we're on the subject, as part of the EE 2.2 release, EllisLab now "officially" offers limited technical support for removing index.php from ExpressionEngine URLs.

Simply add or update your code to the following, making sure to consider any rules you may already have in place:

<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>
忘羡 2024-11-24 16:20:50

尝试使用以下 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 的缩写形式。

Try to use the following ruke as the first one:

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

Also mind the upper case R with is the short form for the lower case redirect.

野稚 2024-11-24 16:20:50

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

我将使用 ServerName old.domain.com 创建 VirtualHost,并在此 VH 中添加以下规则:

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

来自文档:

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

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

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

Have you tried using mod_alias simple redirect instructions (a core module that you have), before trying the hacky-mod-rewrite thing?

I would do a VirtualHost with ServerName old.domain.com and in this VH I would add this rule:

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

from doc:

Then any request beginning with URL-Path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-Path will be appended to the target URL.

So get a separate VirtualHost for brand.new-domain.com (with ServerName brand.new-domain.com) and in this one do not set the Redirect Rule.

If you still want to handle the 2 domains in the same VirtualHost then you'll have to use mod-rewrite as even RedirectMatch cannot check the request domain on the query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文