Hosting & Domaining Forum

Domain Name Discussion => General Domain Discussion => Topic started by: jesusbond on Feb 27, 2023, 03:30 AM

Title: How to write redirect in .htaccess if the domain has changed?
Post by: jesusbond on Feb 27, 2023, 03:30 AM
To avoid redirection from the main site site.fr to site.city, how can we configure it in .htaccess?
There exists an outdated subdomain like 2015.site.com which has been replaced by 2015.site.city as the main domain. The need of the hour is to ensure that any requests to 2015.site.com get automatically forwarded to 2015.site.city.
Title: Re: How to write a redirect in .htaccess if the domain has changed?
Post by: ElizabethParker on Feb 27, 2023, 03:58 AM
This is a classic example of .htaccess file code. It typically involves turning the rewrite engine on and using certain rules to manipulate website URLs. In this particular example, the rewrite condition checks if the incoming request is coming from a.example.com. If this condition is satisfied, then the URL is rewritten with the target being b.example.com.

rewriteengine on
rewritecond %{HTTP_HOST} ^a\.example\.com
rewriterule ^(.*)$ http://b.example.com/$1 [r,l]

The mod_rewrite module in Apache is responsible for handling all the rewriting requests. It allows webmasters to modify the path or query string of a requested URL dynamically. This feature comes in handy when you are migrating from an old site to a new one, or you want to redirect users to a different version of your site based on their location. Overall, it's a useful tool for SEO optimization, so it's worth taking the time to learn how to use it effectively.
Title: Re: How to write a redirect in .htaccess if the domain has changed?
Post by: alexfernando on Feb 27, 2023, 05:32 AM
If we want to implement a redirect using only the HTTP protocol, this can be achieved by writing necessary redirect rules. For instance, the Rewrite Engine is enabled and a condition is checked to see if the incoming HTTP request is coming from 2015.site.ru. If satisfied, the URL is rewritten and redirected to 2015.site.moskow using the RewriteRule.

 

However, if you wish to redirect the HTTPS protocol in the same way, the rule should be modified to include a few extra conditions and requirements. The improved rule includes optimized code that removes duplicate elements and ensures efficiency. Instead of multiple RewriteCond statements, the protocol is determined using RewriteRules, and the result is used in the final RewriteRule to redirect the given URL to the desired destination.

Rewrite Engine On
RewriteCond %{HTTP_HOST} ^2015\.site\.ru$
RewriteRule ^(.*)$ http://2015.site.moskow/$1 [R=301,L]

If you need to redirect the HTTPS protocol in the same way, then the rule will be a little more complicated:

Rewrite Engine On

RewriteCond %{HTTP_HOST} ^2015\.site\.ru$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ http://2015.site.moskow/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^2015\.site\.ru$ [NC]
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ https://2015.site.moskow/$1 [R=301,L]

This rule is understandable, but redundant and done too straightforward. You can optimize it by removing duplicate elements. The result will be the following:

Rewrite Engine On

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ - [env=protocol:http]
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ - [env=protocol:https]

RewriteCond %{HTTP_HOST} ^2015\.site\.ru$ [NC]
RewriteRule ^(.*)$ %{ENV:protocol}://2015.site.moskow/$1 [R=301,L]
Title: Re: How to write redirect in .htaccess if the domain has changed?
Post by: Gaursiz on May 04, 2024, 03:52 AM
I would address this issue by configuring the .htaccess file to redirect any requests from 2015.site.com to 2015.site.city. This can be achieved by adding a few lines of code to the .htaccess file located in the root directory of the website.

To do this, you would need to access the .htaccess file using a text editor or an FTP client. Once opened, you can add the following lines of code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^2015\.site\.com$ [NC]
RewriteRule ^(.*)$ http://2015.site.city/$1 [L,R=301]


Let's break down what each line of code does:

1. `RewriteEngine On`: This line tells the server to enable the mod_rewrite engine, which is required for URL rewriting.

2. `RewriteCond %{HTTP_HOST} ^2015\.site\.com$ [NC]`: This line checks if the incoming request is for the 2015.site.com domain, ignoring the case sensitivity with the `[NC]` flag.

3. `RewriteRule ^(.*)$ http://2015.site.city/$1 [L,R=301]`: This line specifies the actual rewrite rule. It captures the requested URI with `^(.*)$` and appends it to the new domain, 2015.site.city. The `[L]` flag tells the server to stop processing any more rules, and the `[R=301]` flag indicates a permanent redirect.

After adding these lines to the .htaccess file, save it and upload it back to the server. Once this change takes effect, any requests to 2015.site.com should be automatically redirected to 2015.site.city.

Making such changes to the .htaccess file can potentially impact the website's functionality, so it's always a good idea to have a backup of the original file and to test the redirection to ensure it works as intended. Additionally, consider the implications for search engine optimization and update any relevant links or references to the new domain.
Title: Re: How to write redirect in .htaccess if the domain has changed?
Post by: Dewlance on May 07, 2024, 05:41 AM
You can use meta refresh to redirect users:

<meta http-equiv="refresh" content="0;URL=https://your-new-website-here.tld">

And write in body of your page that your website has been moved to new address, If it does not redirect within 5 seconds then click here to redirect to our new website