A Guide on Setting Up 301 Redirects in the htaccess and web.config. Files

301 Redirect (Moved Permanently 301) — this redirect code means that the page or website was moved to a new address and will remain there forever. Meanwhile, the users will be directed to a new address, but the search engines will still index the old one, and, in this case, the link equity will be fully transmitted. 

Redirect through htaccess

To execute this redirect, you need to create the .htaccess file in the website folder. If you’re using the WordPress or Joomla CMS, then it’s likely that you already have this file. In the case when you have the file, you’ll just need to edit it.

Below, you can see settings, which you need to write in the .htaccess file to create the redirect.

Website redirect from HTTP to HTTPS:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirect from the old static page to the new page:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$ [NC]
RewriteRule ^knowlage-base/guide_on_setting_up_301_redirects/$ http://www.yourdomain.com/newpage/ [R=301,L]

Redirect to the homepage:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$ [NC]
RewriteRule ^knowlage-base/guide_on_setting_up_301_redirects/$ http://www.yourdomain.com/ [R=301,L]

Redirect from the homepage to the internal page:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$ [NC]
RewriteRule ^$ http://www.yourdomain.com/guide_on_setting_up_301_redirects/ [R=301,L]

Redirect from the website pages with WWW to the pages without WWW

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/robots.*
RewriteRule ^(.*)$ http://yourdomain.com/$1 [R=301,L]

Redirect from the website pages without WWW to the pages with WWW:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/robots.*
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

The following example allows you to take away the slash at the end of URL.

301 Redirect from the pages with the slash at the end to the pages without the slash:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$ /$1 [R=301,L]

Here we add the slash at the end of URL

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*[^/])$ $1/ [L,R=301]

301 Redirect with index.php to the homepage

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.yourdomain.ru/ [R=301,L]

Redirecting all internal pages to the homepage:

RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule !(^$|.*\.(css|jpg|gif)) / [R=301,L]

Redirecting URL with the parameter to any internal website page:

 RewriteEngine On
RewriteCond %{QUERY_STRING} category=1
RewriteRule ^news.php http://www.domain.com/news.php?category=2 [R=301,L]

Redirecting URL with the parameter to the homepage of the website: (For instance, http://domain.com/index.php?show_section=12 to the homepage

http://domain.com/ (here the parameter is
show_section=12)):

RewriteEngine On
RewriteCond %{QUERY_STRING} show_section=12
RewriteRule ^index.php http://site.com/? [R=301,L]

Redirect from the old domain to the new one (write in .htaccess of the old website):

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domainold\.com$ [NC]
RewriteRule ^(.*)$ http://domainnew.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^domainold\.com$ [NC]
RewriteRule ^(.*)$ http://domainnew.com/$1 [L,R=301]

When there are two websites with the same information, but they are available via two different addresses (duplicate websites). These websites have the same robots.txt, sitemap.xml, and one .htaccess for both sites. There is a variant of 301 Redirect  from the secondary site (domain2) to the main one (domain1):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain2.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^www.domain2.com$ [NC]
RewriteCond %{REQUEST_URI} !^/robots.*
RewriteRule ^(.*)$ http://www.domain1.com/$1 [R=301,L]

An inserter that includes all redirects from index|default pages with the extensions htm|html|php|asp|aspx to the homepage

RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ .*/(index|default)\.(htm|html|php|asp|aspx)\ HTTP
RewriteRule ^(.*)(index|default)\.(htm|html|php|asp|aspx)$ http://www.site.com/$1 [R=301,L]

Redirect via web.config

This file is usually located in the website folder. But if it’s absent, you need to create the file.

An example of a redirect from the pages without WWW to pages with WWW in web.config for IIS:

<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^yoursite.com$" />
</conditions>
<action type="Redirect" url="http://www.yoursite.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>

An example of a redirect from the default.aspx page to the homepage in web.config for IIS:

<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1">
<match url="^default\.aspx" ignoreCase="false" />
<conditions>
<add input="{QUERY_STRING}" pattern="^$" ignoreCase="false" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.yoursite.com/" />
</rule>
</rules>
</rewrite>

Was this answer helpful? 0 Users Found This Useful (0 Votes)