Guide to 301 Redirects with an .htaccess File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • puertoblack2003
    OG Member
    • Nov 08
    • 8217

    Guide to 301 Redirects with an .htaccess File

    A 301 redirect using an .htaccess file is a redirect that you create when you rewrite a piece of code on your site.

    There are many reasons why you might want to input a 301 redirect on your site, but how do you do it, and why should you do it the .htaccess way? We’ve got the answers.

    But before we go into specifics, let’s dive into 301 redirects.

    [HEADING=1]What Is a 301 Redirect?[/HEADING]
    A 301 redirect is a permanent redirect. When a user tries to access an old URL, the server sends their browser the 301-Permanently Moved status code and sends them to another page. This is useful for site owners and users because it means they are directed to the next most relevant page.

    301 redirects are useful for search engines too because they help to keep their indexes updated. They inform search engines that content has been moved and ensure that the visibility associated with the original page is transferred to the new URL.

    For more information, take a look at our comprehensive SEO guide to 301 redirects.

    [HEADING=1]What Is an .htaccess File?[/HEADING]
    An .htaccess file is a part of what controls the high-level configuration of your website. You edit the contents of your .htaccess file to enable and disable certain features of your server software without editing in the server configuration file directly. It’s a pretty easy way to make important changes, but you have to be really careful that you’re editing the code correctly. One mistake in your code can cause a lot of problems for users.

    This means that a 301 redirect using an .htaccess file is a permanent redirect implemented via your .htaccess file rather than the server configuration file.

    [HEADING=2]When Should You Use a 301 Redirect with an .htaccess File?[/HEADING]
    A 301 redirect using an .htaccess file can generally be used in three different scenarios:
    1. To redirect visitors after you’ve moved to a new domain
    2. To redirect visitors after you’ve moved old site pages to a new site structure
    3. To redirect visitors to new pages after you’ve combined two sites

    It’s pretty easy to do. We’ll walk you through how it works.

    [HEADING=1]How to Do a 301 Redirect with an .htaccess File[/HEADING]
    A 301 redirect is executed differently depending on the server you use. The .htaccess method is usually used on Apache servers but can be implemented on other servers too.

    [HEADING=2]How to Do 301 Redirects with .htaccess Files on Apache[/HEADING]
    Before you do anything, you need to locate the RewriteEngine, which you can find in the mod_rewrite module in Apache. You can load it using the following code:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    </IfModule>
    Then you can add all your rules below “RewriteEngine On”. The rules you add depend on what exactly you want to redirect.

    [HEADING=3]Redirecting a Single URL[/HEADING]
    When redirecting a single URL, all you have to do is add this code (make sure you substitute “oldpage” and “newpage” for your page names!):

    RedirectMatch 301 /oldpage/ /newpage/
    [HEADING=3]Redirecting a Single Folder[/HEADING]
    When redirecting a single folder to a new location, you would use the following:

    RewriteRule ^/?folder/(.\*)$ /location/$1 [R,L]
    We’ve used “folder” and “location” as our examples. Substitute your folder and location names!

    [HEADING=3]Redirecting www to Non-www with a 301 .htaccess Redirect[/HEADING]
    Maybe you want to run a redirect because you don’t want to use a www subdomain. If this is the case, you’ll need to redirect to a non-www version. Here’s what you need; just replace “example” with your own domain:

    RewriteCond %{HTTP_HOST} ^(.\*)$ http://example.com/$1 [L,R=301]
    [HEADING=2]How to Do 301 Redirect with .htaccess Files on WordPress[/HEADING]
    Firstly, you’ll have to connect your site’s server to edit your .htaccess file. To do this, you have two options.
    1. Connect to your server through FTP
    2. Use the built-in File Manager tool, which you can find in cPanel

    To use the built-in File Manager tool, log in to your cPanel dashboard and locate the tool. Then back up your .htaccess content and use the Edit function to add your 301 redirect code. This will take you to the text editor. From here, you can add the code you need.

    Of course, the 301 redirect code you need depends on what you want to achieve.

    [HEADING=3]Redirecting a Single Page[/HEADING]
    To redirect a single page to another one, you’ll need to add the following code, replacing “oldpage”, “example”, and “newpage” with your details:

    Redirect 301 /oldpage.html http://www.example.com/newpage.html
    [HEADING=3]Redirecting a Domain Name [/HEADING]
    To redirect an entire domain name to another domain name, use the following code:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(?:www\.)oldsite\.com$ [NC]
    RewriteRule ^http://newsite.com%{REQUEST_URI} [L,R=301]
    Just, as always, make sure that you put in your information instead of “oldsite” and “newsite”.

    [HEADING=2]How to Do 301 Redirects with .htaccess Files in Windows[/HEADING]
    If you use Windows to run your site, we’ve also got you covered.

    [HEADING=3]Redirecting a Single Page[/HEADING]
    To redirect a single page, type in the page name under the Pattern field. Your code might look something like this, with your details subbed in:

    blog/page-name/
    [HEADING=3]Redirecting an Entire Website[/HEADING]
    Redirecting your entire site to another domain is also super easy. You just need to add the following to your .htaccess file:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule (.*) http://www.newsite.com/$1 [R=301,L]
    And replace “newsite” with your domain name!

    For more information, check out our article on the 16 crucial SEO steps when redirecting an entire website.

    [HEADING=3]Redirecting Old URLs to New URLs[/HEADING]
    If you change your filenames for specific pages, you can redirect to the new URLs by inputting the following code and replacing “oldpage”, “example”, and “newpage” with your info:

    redirect 301 /oldpage.html http://www.example.com/abc/newpage.html
    [HEADING=1]How to Identify & Fix Redirect Issues[/HEADING]
    SEO issues aren’t foreign to any of us, and, like any other feature, redirects can cause problems too.

    With our Site Audit tool, you can quickly recognize and resolve any problems you might have with your redirects.

    The Site Audit tool can recognize problems like:
    • No redirect or canonical to HTTPS homepage from HTTP version
    • Redirect chains and loops
    • Pages with temporary redirects
    • Pages with a broken canonical link
    • Broken external links
    • Broken internal links

    So, if you’ve just added some redirects to your site, here’s how to set up your Site Audit to find any technical issues and get them resolved.​

    SPJ Bulletin @
    forum.smartphonejunkie.org
    Law Enforcement Forum www.phillyfinest369.com
Working...