Search code examples
html.htaccesshttp-redirecthttp-status-code-301

How to properly make 301 redirect


I have over 50 html pages that I'm going to move to different folders in the same domain.

How to properly make 301 redirects for each one?

Some people said to place the redirect in meta html tags. Like this <meta http-equiv="refresh" content="0; url=http://example.com/" />

Some other people are saying to make it inside the .htaccess file, I'm not sure what's the best way?

My goal is to redirect the old URLs to the URLs without losing the page rank in Google.


Solution

  • In most instances, a 301 redirect is the best way to implement redirects on a website. Using a meta refresh won't let the reading entity (Google, a browser or otherwise) know that it's permanent and thus would have a negative impact in terms of SEO.

    A 301 redirect essentially means "Moved Permanently" as an HTTP status code and will be recognised for SEO purposes.

    Achieving this within an .htaccess file is the most efficient way of doing this as it's all done in one place and won't require the potential editing of all the individual files (50 in your case).

    Do this as a simple list inside an .htaccess file:

    RewriteEngine on
    Redirect 301 /oldfolder/file1.html /newfolder/file1.html
    Redirect 301 /oldfolder/file2.html /newfolder/file2.html
    

    Or, if all the files reside in one folder, you could use a ReWriteRule similar to below that's a lot quicker to test and implement:

    RewriteEngine on
    RewriteRule ^oldfolder/(.*)$ /newfolder/$1 [R=301,NC,L]
    

    For reference:
    R=301 tells search engines that the redirection is permanent
    NC tells the engine not to care about the characters in the rule
    L tells Apache it's the last rule and will avoid parsing/loops etc