How to Redirect URLs Using htaccess

To permanently redirect all traffic from your site to another site’s page using the .htaccess file, you can use the following rule:

RewriteEngine on
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Replace http://www.example.com/ with the URL of the page you want to redirect all your traffic to. This rule uses a 301 redirect, which is understood by browsers and search engines as a permanent redirect. This means that the redirection will also be cached by browsers and recognized by search engines, transferring the SEO value of your pages to the new URL.

Here’s a breakdown of the rule:

  • RewriteEngine on: Enables the rewrite capabilities.
  • RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]: This rule matches any request (^(.*)$) and redirects it to http://www.example.com/ while preserving the rest of the URL path. The [R=301,L] flags indicate that it is a permanent redirect (301) and that this rule should be the last (L) rule considered if it matches.

Remember to replace http://www.example.com/ with the actual URL you wish to redirect to.

In most cases, you will have to insert this code to the existing htaccess file that looks something like this:

If you have never worked with htaccess before, you might be tempted to shove the new code at the beginning or at the end.

Correct insertion point

To add a new section for permanently redirecting all links to another site’s page in your existing .htaccess file, you should carefully place it in a location that doesn’t interfere with the existing rules, especially those managing caching and WordPress’s rewrite rules. A good practice is to add new redirection rules before the # BEGIN WordPress section or after the existing rewrite rules but before the WordPress-specific rules to ensure that the redirection takes precedence over WordPress’s internal handling of URLs.

Given your current .htaccess content, you can insert your new redirection rule just before the # BEGIN WordPress section. This placement ensures that your redirection rule is processed before any WordPress or caching rules that might conflict with the intended redirection behavior.

Here is how you can integrate the new redirection rule:

# BEGIN LSCACHE
... existing rules ...

# END LSCACHE
# BEGIN NON_LSCACHE
... existing rules ...
# END NON_LSCACHE

# Permanent redirect to another site
RewriteEngine on
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_expires.c>
... existing rules ...
</IfModule>

Remember to replace http://www.example.com/ with the actual URL you want to redirect all your traffic to. Adding the rule in this manner ensures that the server processes the redirect before handling any WordPress-specific routing, thereby avoiding potential conflicts with WordPress or cache-related rules.

What is that “$1” trigger?

The $1 at the end of the URL in the RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] serves as a placeholder that captures and appends the original requested path to the new URL. Here’s a breakdown of its function:

  • ^(.*)$ is a regular expression that matches the entire path of the URL requested on your server. The ^ symbol matches the beginning of the URL, and the $ symbol matches the end. .* matches any character (.) zero or more times (*), effectively capturing the entire path of the URL after the domain name.
  • The parentheses () around .* create a capture group, which means whatever matches inside those parentheses (in this case, the entire path and query string of the original URL) can be referenced later in the rule.
  • $1 references the first (and in this case, the only) capture group defined in the pattern. It’s used in the substitution URL http://www.example.com/$1 to dynamically insert the captured path from the original request into the destination URL.

So, if someone visits http://yourdomain.com/page1, the rule will redirect them to http://www.example.com/page1. The $1 ensures that the /page1 part of the original URL is appended to the base URL http://www.example.com/, allowing for a seamless redirect that preserves the path and query components of the original request. This mechanism is essential for maintaining the structure of the original URL in the redirection process.

Example

To redirect all traffic from your site to a specific page, such as https://yourname.com/this-page, you can modify the .htaccess rule accordingly. Given the placement advice and your existing .htaccess content, here’s how you can insert the redirection:

# BEGIN LSCACHE
... existing rules ...

# END LSCACHE
# BEGIN NON_LSCACHE
... existing rules ...
# END NON_LSCACHE

# Permanent redirect to a specific page
RewriteEngine on
RewriteRule ^(.*)$ https://yourname.com/this-page [R=301,L]

# BEGIN WordPress
<IfModule mod_expires.c>
... existing rules ...
</IfModule>

This rule will redirect all requests from your current site to https://yourname.com/this-page. It’s important to note that this will override any specific URL paths or files on your original site, directing everything to the specified page. The [R=301,L] flags indicate that this is a permanent redirect (which search engines will honor by transferring SEO value) and that no subsequent rewrite rules should be processed if this rule is matched.

Final Thoughts

I hope this help you a little bit as you learn how to manage htaccess files.

By the way, make sure to test this configuration in a development environment or at a time when you can afford to troubleshoot, as improper .htaccess configurations can result in site downtime or unexpected behavior.

Leave a Reply