【问题标题】:How to redirect old url to new domain url in htaccess?如何将旧网址重定向到 htaccess 中的新域网址?
【发布时间】:2021-12-07 09:39:18
【问题描述】:

我一直在尝试使用 htaccess 文件将旧 url 重定向到新域 url。带有 301 重定向代码。但旧网址与新域名网址相同。我试过以下代码 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/$1 [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/ 时,它会重定向到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

【问题讨论】:

  • 它似乎没有做任何事情。你到底把这条规则放在哪里?你有现有的指令吗?
  • 重定向网址重定向到相同的网址而不是新的网址
  • 您要的网址是什么? (我看到您刚刚编辑了您的问题以更改“输出”中的域 - 这是否意味着您正在请求不同的域?)
  • 是的,从旧域旧 url 到新域 url 不重定向它被重定向到与新域 url 相同的旧域
  • 那么,您请求的实际 URL 是什么?您的.htaccess 文件中是否有现有指令?您的子目录中还有其他.htaccess 文件吗?

标签: .htaccess


【解决方案1】:
 RewriteEngine On



 RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule (.*)$ http://www.example1.com/$1 [R=301,L]

   :
   :
   :

Redirect 301 /x-ee-ee/sub-category/ererrre/  
 https://www.example1.com/category/exurl

因为第一条规则将所有内容从example.com 重定向到example1.com 处的同一个URL。请注意,第一条规则重定向到 HTTP,而不是 HTTPS。

你的 Redirect 指令没有做任何事情。

不管文件中指令的顺序如何,mod_rewrite 指令(RewriteRule - 第一条规则)在 mod_alias(Redirect - 第二条规则)之前处理。所以简单地改变指令的顺序并没有帮助。

您需要使用 mod_rewrite RewriteRule(而不是 mod_alias Redirect)并将指令放在之前重定向其他所有内容的通用规则。

这些指令(和您的文件)的性质似乎表明 example.comexample1.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/$1 [R=301,L]

请注意,RewriteRule 指令的行为与Redirect 指令的行为并不完全相同。 mod_alias Redirect 指令是前缀匹配,并将匹配后的所有内容复制到目标 URL 的末尾 - 但我认为您不会期待这一点,因为您的尾部斜杠不匹配会导致格式错误的重定向。

另请注意,与Redirect 指令不同,RewriteRule pattern 匹配的 URL 路径以斜杠开头。

如果 example.comexample1.com 实际上确实指向不同的主机,那么您可以删除前面的 RewriteCond 指令来检查 HTTP_HOST。但如果是这种情况,那么您也可以删除大多数其他指令,因为它们不会做任何事情。

您需要在测试前清除浏览器缓存,因为错误的 301(永久)重定向将被缓存。首先使用 302(临时)重定向进行测试以避免缓存问题。


更新#1:

如何重写这个url RewriteRule ^index.php?option=com_content&view=article&id=478&catid=16$ https://www.example1.com/checking/debit-cards [R=301,L]

我假设您的意思是“重定向”。 RewriteRule pattern 只匹配 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&amp;view....)将其视为完全匹配字符串比较,而不是正则表达式。

UPDATE#2:QSD 标志是从重定向响应中丢弃原始查询字符串所必需的。


UPDATE#3:我还想重定向 RewriteRule ^/images/docs/ATM-Dispute-Form.pdf$ https://www.example1.com/tools/security-center [R=301,L]

如上所述,“RewriteRulepattern 匹配的 URL-path 不以斜杠开头”,所以应该这样写:

RewriteRule ^images/docs/ATM-Dispute-Form\.pdf$ https://www.example1.com/tools/security-center [R=301,L]

不要忘记在RewriteRule 中使用反斜杠转义文字点模式

我还可以访问旧域 url www.example.com/administrator/index.php?SecureJscadmin=xxx 的后端,我们需要在旧域中单独保持这个 url

您可以尝试在最后一条规则中添加一个例外来重定向其他所有内容。例如:

# 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/$1 [R=301,L]

CondPattern上的!前缀否定表达式,所以不匹配时成功。

【讨论】:

  • 如何重写这个url RewriteRule ^index.php?option=com_content&view=article&id=478&catid=16$ example1.com/checking/debit-cards [R=301,L]
  • @mike 你需要一个额外的条件。我已经更新了我的答案。
  • @mike 啊,对不起,您需要添加 QSD 标志来丢弃查询字符串。我已经更新了我的答案。
  • 我也想重定向 RewriteRule ^/images/docs/ATM-Dispute-Form.pdf$ example1.com/tools/security-center [R=301,L] 我也可以访问旧域 url www 的后端。 example.com/administrator/index.php?SecureJscadmin=xxx 我们需要在旧域中单独保持此 url 活动
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 2013-10-16
  • 2016-11-22
相关资源
最近更新 更多