How should I properly setup my .htaccess file?
-
I have searched google for 'how to setup .htaccess file' and it seems that every website has some variation. For example:
RewriteCond %{HTTP_HOST} ^yoursite.com
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [R=permanent,L]
On SEOMOZ someone posted this:
RewriteCond %{HTTP_HOST} !^www.yoursite.com [NC]
RewriteRule (.*) http://www.yoursite.com/$1 [L,R=301]
On yet another website, I found this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^your-site.com$ [NC]
RewriteRule ^(.*)$ http://your-site.com/$1 [L,R=301]
As you can see there are slight differences.
Which one do I use? I'm on Apache CentOS and I have HTML5 websites and several Joomla! wesites. Would the HTACCESS File be different for both?
-
Well, after trying out all 3 of the options i posted, none worked and one threw a server error.
BUT, alas... I found the correct way to do it:
<code class="htaccess" title="in your .htaccess file">RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]</code>
This is the correct way to forward all non www requests to www.yoursite.com
No need to put your domain name, simply add the code above to a .htaccess file on your server and viola!
If someone wants to redirect all www to non www then the code would be: ``` > ``` > ``` > <code class="htaccess" title="in your .htaccess file">RewriteEngine On > RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC] > RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]</code> > ``` > ``` ``` ``` <code class="htaccess" title="in your .htaccess file">and of course replace the my-domain with your domain ;)</code> ``` ```
-
Hi Max
1 should work for your requirements. It is what we use to direct all non www traffic to the www page on 3 different platforms. Oscommerce, php custom build, Wordpress.
Just verify everything by trying to hit a couple none www urls to verify everything re-directs okay.
Also here is a good htaccess shortcut sheet (Note site sometimes loads slow so you may want to copy and paste it to a text file for later reference)
Hope that helps
-
Thanks for the reply Casey!
I'm trying to redirect non www to www. and I'm on a dedicated server running apache. For my HTML5 pages, what should my HTACCESS file look like.
My ultimate goal is to be SEO optimized. Any other settings that will help my sites would be appreciated
-
There are many many things you can do with your .htaccess
What are you trying to accomplish? Are you trying to redirect non-www to www? Trying to get a better link structure? Trying to redirect pages? Or a site?
In most cases it depends on your server setup and your needs. Since you mentioned Joomla, this post has some pretty good explanations and examples at the bottom you can download.
Got a burning SEO question?
Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.
Browse Questions
Explore more categories
-
Moz Tools
Chat with the community about the Moz tools.
-
SEO Tactics
Discuss the SEO process with fellow marketers
-
Community
Discuss industry events, jobs, and news!
-
Digital Marketing
Chat about tactics outside of SEO
-
Research & Trends
Dive into research and trends in the search industry.
-
Support
Connect on product support and feature requests.
Related Questions
-
An article we wrote was published on the Daily Business Review, we'd like to post it on our site. What is the proper way?
Part 1
Technical SEO | | peteboyd
We wrote an article and submitted it to the Daily Business Review. They published the article on their website. We want to also post the article on our website for our users but we want to make sure we are doing this properly. We don't want to be penalized for duplicating content. Is this the correct way to handle this scenario written below? We added a rel="canonical" to the blog post (on our website). The rel="canonical" is set to the Daily Business Review URL where the article was originally published. At the end of the blog post we wrote. "This article was originally posted on The Daily Business Review." and we link to the original post on the Daily Business Review. Should we be setting the blog post (on our website) to be a "noindex" or rel="canonical" ? Part 2 Our company was mentioned in a number of articles. We DID NOT write those articles, we were only mentioned. We have also posted those same articles on our website (verbatim from the original article). We want to show our users that we have been mentioned in highly credited articles. All of these articles were posted on our website and are set to be a "noindex". Is that the correct thing to do? Should we be using a rel="canonical" instead and pointing to the original article URL? Thanks in advance MOZ community for your assistance! We tried to do the leg work of our own research for the answers but couldn't find the exact same scenario that we are encountering**.**0 -
Creating a CSV file for uploading 301 redirect URL map
Hi if i'm bulk uploading 301 redirects whats needed to create a csv file? is it just a case of creating an excel spreadsheet & have the old urls in column A and new urls in column B and then just convert to csv and upload ? or do i need to put in other details or paremeters etc etc ? Cheers Dan
Technical SEO | | Dan-Lawrence0 -
Blocked by meta-robots but there is no robots file
OK, I'm a little frustred here. I've waited a week for the next weekly index to take place after changing the privacy setting in a wordpress website so Google can index, but I still got the same problem. Blocked by meta-robots, no index, no follow. But I do not see a robot file anywhere and the privacy setting in this Wordpress site is set to allow search engines to index this site. Website is www.marketalert.ca What am I missing here? Why can't I index the rest of the website and is there a faster way to test this rather than wait another week just to find out it didn't work again?
Technical SEO | | Twinbytes0 -
Limits to 301 in htaccess?
I'm about to launch a redesign of my company's main website, and we've updated most of the URLs to be more user friendly and SEO optimized. I've just finished editing my spreadsheet, and see that I'll need to implement 244 redirects. My question is: Are there performance issues with loading your .htaccess file up with almost 250 301 redirect commands? I've heard a bloated htaccess file can really slow down apache, should I be approaching this a different way, maybe with php?
Technical SEO | | AdoptionHelp0 -
Blog Setup
A client has been blogging for about a year on http://blog.abccompany.com/ and has a page authority of 37. With it's built up past history, should we change the blog to a subdirectory, http://www.abccompany.com/blog ? Obviously thinking what is the best long-term strategy / benefits. Thanks, Chris
Technical SEO | | evereffect0 -
How do you properly handle syndicated content?
The same piece of content is pulled in and presented (syndicated) within a frame on different web sites (owned by the same company). However, I would like only one web site to rank on Google's search results for that content. How do I set this up? Thanks, claudia
Technical SEO | | claudmar0 -
Any value in external links to image files?
Let's say you have www.example.com. On this website, you have www.example.com/example-image.jpg. When someone links externally to this image - like below... { is < {a href="www.example.com/example-image.jpg"} {img src="www.example.com/example-image.jpg"} {/a} The external site would be using the image hosted on your site, but the image is also linked back to the same image file on your site. Does this have any value even though the link is back to the image file and not the website? Also - how much value do you guys feel image links have in relation to tech links? In terms of passing link juice and adding to a natural link profile. Thanks!
Technical SEO | | qlkasdjfw1