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]
当我调用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编写
mod_rewrite
规则时,规则将按照它们出现的顺序应用。要将旧域重定向到新域,您需要将该规则放在
.htaccess
或httpd.conf
文件中的第一个 —所有其他规则应出现在其之后。如果您只想重定向某个目录,则以下规则将执行此操作,同时允许站点的其余部分正常运行:
如果您想重定向整个站点,则以下规则将执行此操作:
如果您想让抓取工具知道您的内容已移动并希望尽可能无缝地进行转换,请务必保留 301 RewriteRule 中的重定向 标志。
这将确保用户和搜索引擎被定向到正确的页面。
当我们讨论这个主题时,作为 EE 2.2 版本的一部分,EllisLab 现在“正式”为 提供有限的技术支持从 ExpressionEngine URL 中删除 index.php。
只需将您的代码添加或更新为以下内容,并确保考虑您可能已经存在的任何规则:
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
orhttpd.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:
If you want to redirect the entire site, the following rule will do so:
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:
尝试使用以下 ruke 作为第一个:
另请注意大写的
R
是小写的redirect
的缩写形式。Try to use the following ruke as the first one:
Also mind the upper case
R
with is the short form for the lower caseredirect
.您是否尝试过使用 mod_alias 简单的重定向指令(您有),在尝试 hacky-mod-rewrite 之前?
我将使用
ServerName old.domain.com
创建 VirtualHost,并在此 VH 中添加以下规则:来自文档:
因此,为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:from doc:
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.