网络营销电子商务研究中心

网络营销电子商务研究中心 (https://www.0058.net/index.php)
-   建站交流 (https://www.0058.net/forumdisplay.php?f=11)
-   -   .htaccess 301 Redirect (https://www.0058.net/showthread.php?t=4764)

Packard 2014-06-15 07:52 PM

.htaccess 301 Redirect
 
If you have a standard Linux server, you probably have Apache as the actual Web server software. The other alternatives include Lighttpd (pronounced “Lighty”) and Nginx (pronounced “Engine-X”) but they’re not overly common. If you have a file called “.htaccess” in your “public_html” folder, you know you’ve got Apache running on your server.

.htaccess file 301 Redirects taught on this page include:
Redirect a Single Page
Redirect an Entire Site
Redirect all subpages without redirecting the entire domain
Redirect a site to WWW
Redirect a site to Non-WWW

Note:

If you end your old site or folder URL with a / it will keep the names and folders that are accessed intact so if you create a redirect from www.website.com/data/ to www.kickasswebsite.com/data/ and a visitor attempts to go to www.website.com/data/oldpage.htm will become www.kickasswebsite.com/data/oldpage.htm. This ability will help you most when you need to transfer a site to a new domain.

Also, I am using FileZilla as my FTP program and these instructions are tailored to it. If you use a different FTP program, the processes may vary.



How To Redirect a single page
Log in to your web hosting account in your FTP program and navigate to the /public_html folder.
Right-Click on the file called “.htaccess”.
Choose “View/Edit”.
Right at the top, copy in one of the lines below that starts with “Redirect 301″.
Change the “/oldpage.html” to the page or folder you want to redirect from.
Change the http://www.yoursite.com/newpage.html” to the page or folder you want visitors to go to.
Save the document.
Close the file editor.
Click in the FTP Editor.
Click on “Finish editing and delete local file.”
Code:

The Code Required:

Redirect 301 /oldpage.html http://www.yoursite.com/newpage.html
 Redirect 301 /oldpage2.html http://www.yoursite.com/folder/
 Redirect 301 / http://www.yoursite.com/blog/

How To Redirect an entire site
Log in to your web hosting account in your FTP program and navigate to the /public_html folder.
Right-Clisk on the file called “.htaccess”.
Choose “View/Edit”.
Right at the top, copy in one of the lines below that starts with “Redirect 301″.
Change the http://website.com/” to the page or folder you want visitors to go to.
Save the document.
Close the file editor.
Click in the FTP Editor.
Click on “Finish editing and delete local file.”

Code:

The Code Required:
 Redirect 301 / http://website.com/

Redirect Only Subpages

If you need to 301 redirect all the subpages of a domain but don’t want to redirect www.domain.com itself, you can use the following bit of code.

Code:

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/$
 RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

Redirect to WWW or to non-WWW

Some people want to have the WWW at the beginning of your URL. Other people think it looks grittier not to have it. Whichever you prefer, and thankfully it doesn’t truly matter, is up to you. Doing a redirect from websitecom to wwwwebsitecom is fine as long as it is a correct 301 redirect. It’s also perfectly acceptable to redirect wwwwebsitecom to websitecom.

It’s just best if you do choose 1 way or the other for SEO because Google can view website.com and www.website.com as separate sites. This means that if you have incoming links going to both website.com and www.website.com there’s a chance that they will both build up different pages in Google’s eyes. So to correct that, here’s how you can make everything look uniform using the htaccesss file.

Redirect to WWW with the htaccesss file

To edit the .htaccess file, follow the instructions above and insert the following code at the head of the file.

Code:

RewriteEngine On
 RewriteCond %{HTTP_HOST} ^domain.com$
 RewriteRule (.*) http://www.website.com/$1 [R=301,L]

Redirect to non-WWW with the htaccesss file
Code:

RewriteEngine On
 RewriteCond %{HTTP_HOST} !^website.com$
 RewriteRule (.*) http://website.com/$1 [R=301,L]

Redirect 404 Errors to home page

If you have multiple pages that come up with a 404 error and you don’t want to take the time to redirect each and every one, I highly recommend entering the following code into your .htaccess file. It’ll automatically redirect all 404 error pages to your home page, thus saving your SEO and keeping people on your site.


Code:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]


Packard 2014-06-15 07:53 PM

Code:

http://www.free-patent-search.net/product
http://www.free-patent-search.net/contact
http://www.free-patent-search.net/23.html
http://www.free-patent-search.net/9.html
http://www.free-patent-search.net/7.html
http://www.free-patent-search.net/5.html
 


Redirect 301 /product http://www.yeahfine.com/product
Redirect 301 /contact http://www.yeahfine.com/contact
Redirect 301 /23.html http://www.yeahfine.com/23.html
Redirect 301 /9.html http://www.yeahfine.com/9.html
Redirect 301 /7.html http://www.yeahfine.com/7.html
Redirect 301 /5.html http://www.yeahfine.com/5.html



All times are GMT +8. The time now is 10:54 AM.

Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.