我一直在嘗試使用 htaccess 檔案將舊 url 重定向到新域 url。帶有 301 重定向代碼。但是舊的 url 使用新的域 url 重定向。我嘗試使用以下代碼 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} (<|<)([^s]*s) cript.*(>|>) [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-類別/錯誤/
當我輸入地址https://www.example.com/x-ee-ee/sub-category/ererrre/時的預期輸出應該重定向到 https://www.example1.com/category/exurl
uj5u.com熱心網友回復:
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到同一個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/$1 [R=301,L]
請注意,RewriteRule指令的行為與指令并不完全相同Redirect。mod_aliasRedirect指令是前綴匹配,并將匹配后的所有內容復制到目標 URL 的末尾 - 但我假設您沒有預料到這一點,因為您的尾部斜杠不匹配,這會導致格式錯誤的重定向。
還需要注意的是由匹配的URL路徑RewriteRule 模式并沒有以斜線開頭,不像Redirect指令。
如果example.com并且example1.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]
I assume you mean "redirect". The RewriteRule pattern only matches against the URL-path. In order to match the query string part of the URL you need to use an additional condition and match against the QUERY_STRING server variable.
For example:
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]
The = prefix on the CondPattern (ie. =option=com_content&view....) treats it as an exact match string comparison, not a regex.
UPDATE#2: The QSD flag is required to discard the original query string from the redirected response.
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]
As noted above, "the URL-path matched by the RewriteRule pattern does not start with a slash", so this should be written:
RewriteRule ^images/docs/ATM-Dispute-Form\.pdf$ https://www.example1.com/tools/security-center [R=301,L]
Don't forget to backslash-escape literal dots in the RewriteRule pattern
Also can i access backend of old domain url
www.example.com/administrator/index.php?SecureJscadmin=xxxwe need to keep alive this url alone in old domain
You can try adding an exception to the last rule that redircts everything else. For example:
# 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]
The ! prefix on the CondPattern negates the expression, so it is successful when it does not match.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/332877.html
標籤:.htaccess
