Redirection (301) - Htaccess Tutorial

extremephp

BANNED
Joined
Oct 19, 2010
Messages
1,288
Reaction score
1,281
I thought, this would help some people, atleast some who doesnt Google much!


How to Save an Htaccess file
1.Open Notepad
2.Type in the Htaccess commands
3. File > Save
4.remove .txt and type .htaccess
5. From File type, select All Files.
6. Save
7. Done!


Q: I see my site having 100 backlinks with WWW and 10 Without WWW
A : 301 Redirect WWW to Non WWW or Vice Versa!


WWW to Non WWW


#Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^sitenamehere.com
RewriteRule ^(.*) http://www.sitenamehere.com/$1 [R=301,L]


Non WWW to WWW

#Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^http://www.sitenamehere.com
RewriteRule ^(.*) http://sitenamehere.com/$1 [R=301,L]


Q: I bought an SSL! And I want to force my url to be https://
A: Can be done with out without WWW. (codes below)

Non WWW to WWW with Https

#Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^sitenamehere.com
RewriteRule ^(.*) https://www.sitenamehere.com/$1 [R=301,L]

WWW to Non WWW with Https

#Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^http://www.sitenamehere.com
RewriteRule ^(.*) https://sitenamehere.com/$1 [R=301,L]


Q : I dont want my site to come with .html, .php extensions
A: Can easily be hidden!



Hiding Page Extentions


RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Replace .php with .html .txt or whatever you need to hide!


Q: Google deindexed my site. I need all the link juice to my new site. And I dont want to break the old urls.

A: 301 olddomain.com/page to newdomain.com/page
. Kick Googles Butt using this htaccess mod!


Domain.com/page to newdomain.com/page

#Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.yourolddomain.com$[OR]
RewriteCond %{HTTP_HOST} ^yourolddomain.com$
RewriteRule ^(.*)$ http://www.yournewdomain.com/$1 [R=301,L]


Am not a pro in htaccess, but if you have some problems with this, Ask loud!


~ExP~
 
I am trying to make a redirect to entire site but it doesn't work. I am using this code - redirect 301 / http://www.mynewdomain.com/.
The host doesn't support PHP/CGI/ASP/FPext scripts.
Could this be the reason?
 
good share buddy, was actually looking for this today
 
I am trying to make a redirect to entire site but it doesn't work. I am using this code - redirect 301 / http://www.mynewdomain.com/.
The host doesn't support PHP/CGI/ASP/FPext scripts.
Could this be the reason?

All hosts does follow htaccess rules. Just follow what the code above, and you will be able to get it working!


good share buddy, was actually looking for this today

:)

~ExP~
 
i recently changed the .htaccess file on a Joomla shopping cart site and now my site is being indexed with the "https" how do i get rid of this??? please help ... Thanks in advance
 
I did mine though my cpanel (change of domain) & this is the .htaccess it produced:

Code:
RewriteEngine On
RewriteRule ^.*$ http://www.mynewsite.com [R=301]
Is this acceptable for changing domain & retaining link juice?

What is the difference between [r=301] & [r=301,L] ? I also seen a few other "extensions"

Thanks
 
Last edited:
Oh, and I failed to mention, I only want the rule to affect the subdirectory - not any further subdirectories of file names.
 
I did mine though my cpanel (change of domain) & this is the .htaccess it produced:

Code:
RewriteEngine On
RewriteRule ^.*$ http://www.mynewsite.com [R=301]
Is this acceptable for changing domain & retaining link juice?

What is the difference between [r=301] & [r=301,L] ? I also seen a few other "extensions"

Thanks

"L" stands for "last rule" - it orders apache to stop rewriting after the line ending with "L" has been processed.
 
Hey, I have been trying to do some 301 redirects because I noticed that I have 98 "404 Not Found " for my main site in G webmasters tool. So I tried the code below on 1 of my html sites and it worked fine but when I tried it on my wordpress(main site) all my post are getting "Not Found" error. I know that there are wp plugins to do this that works perfect, but I dont want to use a plugin because I prefer to do manually. Can you please tell me what the correct 301 redirect code for wordpress is?

301 CODE I tried in .htaccess

Redirect 301 /page http://www.mysite.com/newpage/

This is the genertated .htaccess that I found in my ftp currently:

RewriteEngine on

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
 
Anyone with experience please any suggestions to my post above.
 
Back
Top