如何在 htaccess 中将旧 url 重定向到新域 url?
How to redirect old url to new domain url in htaccess?
我一直在尝试使用 htaccess 文件将旧 url 重定向到新域 url。带有 301 重定向代码。但是旧的 urls 重定向到同一个新域 urls。我尝试使用以下代码
Htaccess 文件
##
# @package Joomla
# @copyright Copyright (C) 2005 - 2015 Open Source Matters. All rights reserved.
# @license GNU General Public License version 2 or later; see LICENSE.txt
##
##
# READ THIS COMPLETELY IF YOU CHOOSE TO USE THIS FILE!
#
# The line just below this section: 'Options +FollowSymLinks' may cause problems
# with some server configurations. It is required for use of mod_rewrite, but may already
# be set by your server administrator in a way that disallows changing it in
# your .htaccess file. If using it causes your server to error out, comment it out (add # to
# beginning of line), reload your site in your browser and test your sef url's. If they work,
# it has been set by your server administrator and you do not need it set here.
##
## No directory listings
IndexIgnore *
## Can be commented out if causes errors, see notes above.
Options +FollowSymlinks
Options -Indexes
## Mod_rewrite in use.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule (.*)$ http://www.example1.com/ [R=301,L]
#for http to https
##RewriteCond %{HTTPS} !on
##RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
#for www
##RewriteCond %{HTTP_HOST} !^www\.
##RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site block out the
operations listed below
# This attempts to block the most common type of exploit `attempts` to Joomla!
#
# Block out any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block out any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via
URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root
homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.
## Begin - Custom redirects
#
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.
#
## End - Custom redirects
##
# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root).
##
# RewriteBase /
## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
#
## End - Joomla! core SEF Section.
Redirect 301 /x-ee-ee/sub-category/ererrre/
https://www.example1.com/category/exurl
输出
当我输入地址 https://www.example.com/x-ee-ee/sub-category/ererrre/ it is redirecting to https://www.example1.com/x-ee-ee/sub-category/ererrre/
预期输出
当我输入地址时 https://www.example.com/x-ee-ee/sub-category/ererrre/ 应该重定向到
https://www.example1.com/category/exurl
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule (.*)$ http://www.example1.com/ [R=301,L]
:
:
:
Redirect 301 /x-ee-ee/sub-category/ererrre/
https://www.example1.com/category/exurl
因为第一条规则将所有内容从 example.com
重定向到 相同的 URL 在 example1.com
。请注意,第一条规则重定向到 HTTP,而不是 HTTPS。
您的 Redirect
指令没有执行任何操作。
无论指令在文件中的顺序如何,mod_rewrite 指令(RewriteRule
- 第一条规则)在 mod_alias(Redirect
- 第二条规则)之前处理).所以简单地改变指令的顺序并没有帮助。
您需要使用 mod_rewrite RewriteRule
(不是 mod_alias Redirect
)并将指令 放在 通用指令之前重定向其他所有内容的规则。
这些指令(和您的文件)的性质似乎表明 example.com
和 example1.com
都在此处解析,因此您需要检查请求的 Host
作为规则(Redirect
指令不可能做到的事情)。
例如:
RewriteEngine On
# Redirect "example.com/x-ee-ee/sub-category/ererrre/" (trailing slash)
# to "https://www.example1.com/category/exurl" (no trailing slash)
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^x-ee-ee/sub-category/ererrre/$ https://www.example1.com/category/exurl [R=301,L]
# Redirect everything else to the same URL at "example1.com"
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule (.*) https://www.example1.com/ [R=301,L]
请注意,RewriteRule
指令的行为与 Redirect
指令并不完全相同。 mod_alias Redirect
指令是前缀匹配并将匹配后的所有内容复制到目标 URL 的末尾 - 但我假设您不希望出现这种情况,因为您的尾部斜杠不匹配会导致格式错误的重定向。
另请注意,与 RewriteRule
模式 匹配的 URL 路径 不是 以斜杠,与 Redirect
指令不同。
如果 example.com
和 example1.com
确实指向不同的主机,那么您可以删除前面检查 HTTP_HOST
的 RewriteCond
指令。但如果是这种情况,那么您也可以删除大部分其他指令,因为它们不会做任何事情。
您需要在测试前清除浏览器缓存,因为错误的 301(永久)重定向将被缓存。首先使用 302(临时)重定向进行测试以避免缓存问题。
更新#1:
how to rewrite this url RewriteRule ^index.php?option=com_content&view=article&id=478&catid=16$ https://www.example1.com/checking/debit-cards
[R=301,L]
我假设你的意思是“重定向”。 RewriteRule
模式 仅匹配 URL 路径。为了匹配 URL 的查询字符串部分,您需要使用额外的 条件 并匹配 QUERY_STRING
服务器变量。
例如:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{QUERY_STRING} =option=com_content&view=article&id=478&catid=16
RewriteRule ^index\.php$ https://www.example1.com/checking/debit-cards [QSD,R=301,L]
CondPattern 上的 =
前缀(即 =option=com_content&view....
)将其视为完全匹配字符串比较,而不是正则表达式。
UPDATE#2: 需要 QSD
标志来丢弃重定向响应中的原始查询字符串。
UPDATE#3: Also i would like to redirect RewriteRule ^/images/docs/ATM-Dispute-Form.pdf$ https://www.example1.com/tools/security-center
[R=301,L]
如上所述,“与RewriteRule
模式匹配的URL-路径不以斜杠开头”,所以应该这样写:
RewriteRule ^images/docs/ATM-Dispute-Form\.pdf$ https://www.example1.com/tools/security-center [R=301,L]
不要忘记在 RewriteRule
模式
中反斜杠转义文字点
Also can i access backend of old domain url www.example.com/administrator/index.php?SecureJscadmin=xxx
we need to keep alive this url alone in old domain
您可以尝试向重定向其他所有内容的最后一条规则添加例外。例如:
# Redirect everything else to the same URL at "example1.com"
# EXCEPT "/administrator/index.php?SecureJscadmin=xxx"
RewriteCond %{REQUEST_URI} !^administrator/index\.php$ [OR]
RewriteCond %{QUERY_STRING} !^SecureJscadmin=
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule (.*) https://www.example1.com/ [R=301,L]
CondPattern上的!
前缀对表达式求反,所以不匹配就成功了。
我一直在尝试使用 htaccess 文件将旧 url 重定向到新域 url。带有 301 重定向代码。但是旧的 urls 重定向到同一个新域 urls。我尝试使用以下代码 Htaccess 文件
##
# @package Joomla
# @copyright Copyright (C) 2005 - 2015 Open Source Matters. All rights reserved.
# @license GNU General Public License version 2 or later; see LICENSE.txt
##
##
# READ THIS COMPLETELY IF YOU CHOOSE TO USE THIS FILE!
#
# The line just below this section: 'Options +FollowSymLinks' may cause problems
# with some server configurations. It is required for use of mod_rewrite, but may already
# be set by your server administrator in a way that disallows changing it in
# your .htaccess file. If using it causes your server to error out, comment it out (add # to
# beginning of line), reload your site in your browser and test your sef url's. If they work,
# it has been set by your server administrator and you do not need it set here.
##
## No directory listings
IndexIgnore *
## Can be commented out if causes errors, see notes above.
Options +FollowSymlinks
Options -Indexes
## Mod_rewrite in use.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule (.*)$ http://www.example1.com/ [R=301,L]
#for http to https
##RewriteCond %{HTTPS} !on
##RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
#for www
##RewriteCond %{HTTP_HOST} !^www\.
##RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site block out the
operations listed below
# This attempts to block the most common type of exploit `attempts` to Joomla!
#
# Block out any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block out any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via
URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root
homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.
## Begin - Custom redirects
#
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.
#
## End - Custom redirects
##
# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root).
##
# RewriteBase /
## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
#
## End - Joomla! core SEF Section.
Redirect 301 /x-ee-ee/sub-category/ererrre/
https://www.example1.com/category/exurl
输出 当我输入地址 https://www.example.com/x-ee-ee/sub-category/ererrre/ it is redirecting to https://www.example1.com/x-ee-ee/sub-category/ererrre/
预期输出 当我输入地址时 https://www.example.com/x-ee-ee/sub-category/ererrre/ 应该重定向到 https://www.example1.com/category/exurl
RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com$ [OR] RewriteCond %{HTTP_HOST} ^www.example.com$ RewriteRule (.*)$ http://www.example1.com/ [R=301,L] : : : Redirect 301 /x-ee-ee/sub-category/ererrre/ https://www.example1.com/category/exurl
因为第一条规则将所有内容从 example.com
重定向到 相同的 URL 在 example1.com
。请注意,第一条规则重定向到 HTTP,而不是 HTTPS。
您的 Redirect
指令没有执行任何操作。
无论指令在文件中的顺序如何,mod_rewrite 指令(RewriteRule
- 第一条规则)在 mod_alias(Redirect
- 第二条规则)之前处理).所以简单地改变指令的顺序并没有帮助。
您需要使用 mod_rewrite RewriteRule
(不是 mod_alias Redirect
)并将指令 放在 通用指令之前重定向其他所有内容的规则。
这些指令(和您的文件)的性质似乎表明 example.com
和 example1.com
都在此处解析,因此您需要检查请求的 Host
作为规则(Redirect
指令不可能做到的事情)。
例如:
RewriteEngine On
# Redirect "example.com/x-ee-ee/sub-category/ererrre/" (trailing slash)
# to "https://www.example1.com/category/exurl" (no trailing slash)
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^x-ee-ee/sub-category/ererrre/$ https://www.example1.com/category/exurl [R=301,L]
# Redirect everything else to the same URL at "example1.com"
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule (.*) https://www.example1.com/ [R=301,L]
请注意,RewriteRule
指令的行为与 Redirect
指令并不完全相同。 mod_alias Redirect
指令是前缀匹配并将匹配后的所有内容复制到目标 URL 的末尾 - 但我假设您不希望出现这种情况,因为您的尾部斜杠不匹配会导致格式错误的重定向。
另请注意,与 RewriteRule
模式 匹配的 URL 路径 不是 以斜杠,与 Redirect
指令不同。
如果 example.com
和 example1.com
确实指向不同的主机,那么您可以删除前面检查 HTTP_HOST
的 RewriteCond
指令。但如果是这种情况,那么您也可以删除大部分其他指令,因为它们不会做任何事情。
您需要在测试前清除浏览器缓存,因为错误的 301(永久)重定向将被缓存。首先使用 302(临时)重定向进行测试以避免缓存问题。
更新#1:
how to rewrite this url RewriteRule ^index.php?option=com_content&view=article&id=478&catid=16$
https://www.example1.com/checking/debit-cards
[R=301,L]
我假设你的意思是“重定向”。 RewriteRule
模式 仅匹配 URL 路径。为了匹配 URL 的查询字符串部分,您需要使用额外的 条件 并匹配 QUERY_STRING
服务器变量。
例如:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{QUERY_STRING} =option=com_content&view=article&id=478&catid=16
RewriteRule ^index\.php$ https://www.example1.com/checking/debit-cards [QSD,R=301,L]
CondPattern 上的 =
前缀(即 =option=com_content&view....
)将其视为完全匹配字符串比较,而不是正则表达式。
UPDATE#2: 需要 QSD
标志来丢弃重定向响应中的原始查询字符串。
UPDATE#3: Also i would like to redirect RewriteRule ^/images/docs/ATM-Dispute-Form.pdf$
https://www.example1.com/tools/security-center
[R=301,L]
如上所述,“与RewriteRule
模式匹配的URL-路径不以斜杠开头”,所以应该这样写:
RewriteRule ^images/docs/ATM-Dispute-Form\.pdf$ https://www.example1.com/tools/security-center [R=301,L]
不要忘记在 RewriteRule
模式
Also can i access backend of old domain url
www.example.com/administrator/index.php?SecureJscadmin=xxx
we need to keep alive this url alone in old domain
您可以尝试向重定向其他所有内容的最后一条规则添加例外。例如:
# Redirect everything else to the same URL at "example1.com"
# EXCEPT "/administrator/index.php?SecureJscadmin=xxx"
RewriteCond %{REQUEST_URI} !^administrator/index\.php$ [OR]
RewriteCond %{QUERY_STRING} !^SecureJscadmin=
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule (.*) https://www.example1.com/ [R=301,L]
CondPattern上的!
前缀对表达式求反,所以不匹配就成功了。