301 Redirect htaccess examples

Sometimes we need to change URL of pages, but it's a risky task, because if your pages are already indexed in search engines, you will loose not only visitors but also ranking. Your indexed URLs will start to disappear one by one, forcing you to start from scratch again. But with htaccess you can easily redirect your visitors as well as search engine bots to new location safely.

Where is my .htaccess file located?

Your .htaccess file is located in the root folder of your website. You can easily find it using some FTP program or file browser.

Examples

Here are few examples showing the use of htaccess redirects. You can simply copy and paste it in your .htaccess file.Redirect a single file : Sometimes you just want to redirect single page, to achive this just use following htaccess code.
HTACCESS
  • 1
  • 2
RewriteEngine On Redirect 301 /oldfile.html http://www.yoursite.com/file/newFile.html
Redirect whole domain : Incase you want to change your domain name, you can safely redirect users to new site using following codes.
HTACCESS
  • 1
  • 2
  • 3
  • 4
Options +FollowSymlinks RewriteEngine on rewritecond %{http_host} ^domain.com [nc] rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
Dynamic pages redirect to static pages: .htaccess code below will convert dynamic PHP pages, view.php?id=1234 to page_1234.html
HTACCESS
  • 1
  • 2
RewriteEngine On RewriteRule ^page_([^/.]+).html$ view.php?id=$1 [L]
New question is currently disabled!