How to Write Redirection Rule In .Htaccess Using Regex?

6 minutes read

To write a redirection rule in .htaccess using regex, you need to use the RewriteRule directive. The RewriteRule directive allows you to specify a regular expression pattern that matches the URLs you want to redirect and define the destination URL for the redirection.

To create a redirection rule using regex in .htaccess, you need to first enable the RewriteEngine by adding the following line at the beginning of your .htaccess file:

RewriteEngine On

Next, you can use the RewriteRule directive to define the redirection rule. For example, if you want to redirect all URLs that contain the word "example" to a specific destination URL, you can use the following syntax:

RewriteRule ^(.)example(.)$$1 [R=301,L]

In this example, the regular expression ^(.)example(.)$ will match any URL that contains the word "example". The $1 in the destination URL will include the content of the first matched group in the regular expression, allowing you to maintain the original URL structure in the redirection.

You can also use flags like [R=301] to specify the redirection type (in this case, a 301 permanent redirect) and [L] to indicate that this is the last rule to be processed.

By using regular expressions in your RewriteRule directives, you can create flexible and powerful redirection rules in .htaccess to effectively manage your website's URL structure.

Best Cloud Hosting Services of July 2024


Rating is 5 out of 5


  • Ultra-fast Intel Core Processors
  • Great Uptime and Support
  • High Performance and Cheap Cloud Dedicated Servers
Digital Ocean

Rating is 4.9 out of 5

Digital Ocean

  • Professional hosting starting at $5 per month
  • Remarkable Performance

Rating is 4.8 out of 5



Rating is 4.7 out of 5


What is the purpose of using regex in .htaccess redirection rules?

The purpose of using regex in .htaccess redirection rules is to match and redirect certain URL patterns to different locations. Regex allows for more flexibility and control in defining the patterns for redirection, making it easier to handle multiple redirection rules and complex scenarios. It allows you to specify rules based on specific patterns or criteria, such as matching certain strings, numbers, or characters in the URL. This can be useful for implementing SEO-friendly URLs, handling redirects for old or outdated URLs, setting up aliases, or performing other custom redirection tasks.

What is the impact of redirection rules on SEO performance?

Redirection rules can have a significant impact on SEO performance, both positively and negatively. Proper implementation of redirects can help maintain the authority and rankings of old pages when they are moved or deleted, and ensure that users are directed to relevant content. On the other hand, incorrect or excessive use of redirects can hinder crawlability, confuse search engines, and negatively affect user experience. It's important to carefully plan and implement redirection rules to avoid any negative impacts on SEO performance.

What is the difference between a temporary and permanent redirection rule in .htaccess?

A temporary redirection rule (using a 302 status code) tells search engines and browsers that the redirection is temporary and the original URL might be used again in the future. This type of redirection is often used when a website is undergoing maintenance or when a specific page has been temporarily moved.

On the other hand, a permanent redirection rule (using a 301 status code) tells search engines and browsers that the original URL has permanently moved to a new location. This type of redirection is used when a website or specific page has permanently moved to a new address. It also helps to preserve search engine rankings and transfer link equity from the old URL to the new one.

How to redirect an old URL to a new URL using regex in .htaccess?

To redirect an old URL to a new URL using regex in .htaccess, you can use the following code:

RewriteEngine On
RewriteRule ^old-url$ /new-url [R=301,L]

In this code snippet:

  • RewriteEngine On enables the rewrite engine for this .htaccess file.
  • RewriteRule specifies the rule for redirecting the old URL to the new URL.
  • ^old-url$ is the regex pattern that matches the old URL.
  • /new-url is the new URL to which the old URL will be redirected.
  • [R=301,L] specifies that the redirection is a permanent (301) redirect and the rule should be the last one to be applied.

You can customize the regex pattern and the redirect URL according to your specific needs. Just make sure to test the redirection thoroughly to ensure it is working as expected.

How to redirect an entire website using regex in .htaccess?

To redirect an entire website using regex in .htaccess file, you can use the following code:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^oldwebsite\.com$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

In this code:

  • RewriteEngine on turns on the Apache mod_rewrite engine.
  • RewriteCond %{HTTP_HOST} ^oldwebsite\.com$ [NC] checks if the requested domain is "" regardless of case.
  • RewriteRule ^(.*)$$1 [R=301,L] redirects all requests from "" to "", including all paths, with a permanent redirect (301) and stops further processing with the "L" flag.

Make sure to replace "" and "" with your actual domain names. This code should be placed in the .htaccess file in the root directory of the old website.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To exclude admin urls from the lowercase rule in .htaccess, you can add a condition using regex to exclude specific URLs. This can be done by adding a RewriteCond directive before the RewriteRule directive in your .htaccess file. The condition should check if ...
To apply a rule in .htaccess file, you need to first create or edit the .htaccess file in the root directory of your website. Then, add the desired rule using the correct syntax.Rules in .htaccess are written using Apache mod_rewrite module. This module allows...
To remove %20 from a URL using .htaccess, you can add the following rule to your .htaccess file: RewriteRule ^(.*)\%20(.*)$ /$1 $2 [R=301,NE,L] This rule will match any occurrence of %20 in the URL and replace it with a space. The [R=301,NE,L] flags are option...
To redirect from HTTPS to HTTP, you need to modify your website's .htaccess file or configure your server settings. Here's how you can do it:Open the .htaccess file: Connect to your web server using FTP or file manager. Locate the root directory of you...
To rewrite a long URL with .htaccess, you can use the RewriteRule directive in your .htaccess file. This directive allows you to specify a pattern to match and a substitution to replace it with. For example, if you have a long URL like
To redirect all requests using the .htaccess file, you can use the mod_rewrite module in Apache. You can specify the desired redirect behavior using rules in the .htaccess file. For example, to redirect all requests to a specific URL, you can use the following...