Home > Software design >  How to redirect old url to new domain url in htaccess?
How to redirect old url to new domain url in htaccess?

Time:10-23

I have b een trying to redirect old urls to new domain url using htaccess file. with 301 redirect codes. but the old urls redirecting with same one with new domain urls. I have tried with following code Htaccess file

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

output when i typing in address https://www.example.com/x-ee-ee/sub-category/ererrre/ it is redirecting to https://www.example1.com/x-ee-ee/sub-category/ererrre/

expected output when i typing in address https://www.example.com/x-ee-ee/sub-category/ererrre/ should redirect to https://www.example1.com/category/exurl

CodePudding user response:

 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

Because the first rule redirects everything from example.com to the same URL at example1.com. Note that the first rule redirects to HTTP, not HTTPS.

Your Redirect directive isn't doing anything.

Regardless of the order of the directives in the file, the mod_rewrite directives (RewriteRule - first rule) are processed before mod_alias (Redirect - second rule). So simply changing the order of the directives does not help.

You need to use a mod_rewrite RewriteRule (not mod_alias Redirect) and place the directive before the generic rule that redirects everything else.

The nature of these directives (and your file) would appear to suggest that both example.com and example1.com resolve here so you need to check the requested Host as part of the rule (something which is not possible with the Redirect directive).

For example:

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]

Note that the behaviour of the RewriteRule directive is not quite the same as the Redirect directive. The mod_alias Redirect directive is prefix matching and copies everything after the match onto the end of the target URL - but I assume you are not expecting this as you had a mismatch of trailing slashes which would have resulted in a malformed redirect.

Note also that the URL-path matched by the RewriteRule pattern does not start with a slash, unlike the Redirect directive.

If example.com and example1.com actually do point to different hosts then you can remove the preceding RewriteCond directive that checks against HTTP_HOST. But if that's the case then you could remove most of the other directives as well, since they wouldn't doing anything.

You will need to clear your browser cache before testing since the erroneous 301 (permanent) redirect will have been cached. Test first with 302 (temporary) redirects to avoid caching issues.


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

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=xxx we 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.

  • Related