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
-
Question about Image Optimization and File Size -- Does it really matter
I was using Moz's guidelines (https://moz.com/learn/seo/page-speed) to reduce the file size of my pages to improve load speed, but I'm not sure it really makes much of a difference. On this page https://www.mtecorp.com/cad/MAPP0006A002/, the file size is 710KB and it's Google Page Insight Score is 84 on desktop and 66 on mobile. On this page https://www.mtecorp.com/cad/SWNW0130E/, I got the image size to 227KB and its Google Page insight Score is virtually the same, 87 on desktop and 62 on mobile. Any ideas if it is really worth the time to get images down? (or maybe it doesn't matter if it is less than 1,000KB.
Technical SEO | | EricVallee1 -
Where to put 301 redirects in my Wordpress htaccess file?
I have about 25 301 redirects in my Wordpress htaccess file, that look like this: <code>Redirect301/store/index.html https://www.notesinspanish.com/store-home/</code> At the moment they are at the bottom of my htaccess file, below the usual Wordpress rewrite rules: <code># BEGIN WordPress <ifmodulemod_rewrite.c>RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress</ifmodulemod_rewrite.c></code> So they are below all that. Above my WP rewrite rules I have a number of other rules from plugins (caching, ssl). Are my 301's OK where they are at the very bottom of that file? They are working, and redircting pages correctly. Should they be somewhere else? Many thanks for any help. Thanks for any help.
Technical SEO | | Benspain0 -
Several Items in the Organization schema structured file
Hi MOZ community! Could you please help me with the issue? I have implemented Organization Schema on the website. But according to the structure I can not markup data once. So now I have 2 Items for a Organization schema on a page. The questions are: 1. Does Google consider both of them? 2. Is that OK to have a few Items for one type of schema on the page? Thank you
Technical SEO | | juicefromtheraw10 -
Multiple robots.txt files on server
Hi! I have previously hired a developer to put up my site and noticed afterwards that he did not know much about SEO. This lead me to starting to learn myself and applying some changes step by step. One of the things I am currently doing is inserting sitemap reference in robots.txt file (which was not there before). But just now when I wanted to upload the file via FTP to my server I found multiple ones - in different sizes - and I dont know what to do with them? Can I remove them? I have downloaded and opened them and they seem to be 2 textfiles and 2 dupplicates. Names: robots.txt (original dupplicate)
Technical SEO | | mjukhud
robots.txt-Original (original)
robots.txt-NEW (other content)
robots.txt-Working (other content dupplicate) Would really appreciate help and expertise suggestions. Thanks!0 -
Removing CSS & JS Files from Index
Hi, Google has indexed a few .CSS and .JS files that belong to our WordPress plugins and themes. I had them blocked via robots, but realized this doesn't prevent indexation (and can likely hurt us since Google wants to access these files). I've since removed the robots instructions, submitted a removal request via Search Console, but want to make sure they don't come back. Is there a way to put a noindex tag within .CSS and .JS files? Or should I do something with .htaccess instead?
Technical SEO | | kirmeliux1 -
Kill your htaccess file, take the risk to learn a little
Last week I was browsing Google's index with "site:www.mydomain.com and wanted to scan over to see what Google had indexed with my site. I came across a URL that was mistakenly indexed. It went something like this www.mydomain.com/link1/link2/link1/link4/link3 I didn't understand why Google had indexed a page like that of mine when the "link" pages were links that were on my main bar which were site wide links. It seemed to be looping infinitely over and over. So I started trying to see how many of these Google had indexed and I came across about 20 pages. I went through the process of removing the URL's in Webmaster Tools, but then I wanted to know why it was happening. I had discovered that I had mistakenly placed some links on my site in my header in such a manner link1 link2 link3 If you know HTML you will realize that by not placing the "/" in the front of the link I was telling that page to add that link in addition to the URL that is was currently on. What this did was create an infinite loop of links which is not good 🙂 Basically when Google went to www.mydomain.com/link1/ it found the other links which then told Google to add that url to the existing URL and then go to that link. Something like: www.mydomain.com/links1/link2/... When you do not add the "/" in front of the directory you are linking too it will do this. The "/" refers to the root so if you place that in front of your directory you are linking too it will always assume that first "/" as the root then the url will follow. So what did I do? Even though I was able to find about 20 URL's using the "site:" search method there had to be more out there. Even though I tried to search I was not able to find anymore, but I was not convinced. The light bulb went on at this point My .htaccess file contained many 301 redirects in my attempt to try and redirect those pages to a real page, they were not really relevant pages to redirect too. So how could I really find out what Google had indexed out there for me since Webmaster Tools only reports the top 1000 links. I decided to kill my htaccess file. Knowing that Google is "forgiving" when major changes to your site happen I knew Google would not simply just kill my site for removing my htaccess file immediately. I waited 3 days then BOOM! Webmaster Tools was reporting to me that it found a ton of 401's on my site. I looked at the Crawl Errors and there they were. All those infinite loop links that I knew had to be more out there, I was able to see. How many were there? Google found in the first crawl over 5,000 of them. OMG! Yeah could you imagine the "Low quality" score I was getting on those pages? By seeing all those links I was able to determine about 4 patterns in the links. For example: www.mydomain.com/link1/link2/ www.mydomain.com/link1/link3/ www.mydomain.com/link1/link4/ www.mydomain.com/link1/link5/ Now my issue was I wanted to keep all the URL's that were pointing to www.mydomain.com/link1 but anything after that I needed gone. I went into my Robots.txt file and added this Disallow: www.mydomain.com/link1/link2/ Disallow: www.mydomain.com/link1/link3/ Disallow: www.mydomain.com/link1/link4/ Disallow: www.mydomain.com/link1/link5/ Now there were many more pages indexed that went deeper into those links but I knew I wanted anything after the 2nd URL gone since it was the start of the loop that I detected. With that I was able to have from what I know at least 5k links if not more. What did I learn from this? Kill your htaccess file for a few days and see what comes back in your reports. You might learn something 🙂 After doing this I simply replaced my htaccess file and I am on my way to removing a ton of "low quality" links I didn't even know I had.
Technical SEO | | cbielich0 -
Proper structure for site with multiple catagories of same products
Hi, we have products (trophies and awards) that can be catagorized in many ways. Using Award Medals as an example: - Medals by type: 1 1/2", 2", etc. - Medals by sport Baseball, Basketball, Cheer - Medals by Style Color, Gold, Silver, Bronze Right now, we have an Award Medals section off of our home page. The section has a decent page rank, but should be much better (I think). My guess is that we are loosing page range since we have separate sections with the groups above as we want our customers to be able to find the medals easily. Unfortunately, when we setup our site 10 years ago, we organized by type and this is what is hanging off the home page. The other groupings we added more recently. I have attached a snap shot of what the sections look like. We would like customers to find an individual medal when they do a Google search. For example a search for Baseball Medals. In Goggle, they likely would not search for 1 1/2" medals. My question is this: Can we keep the same structure we have today (to enable customer flexibility) but improve page rank and also have the sections like basball medals rank well? I have thought about using canonical tags, but the pages are not the same - in one case it is all baseball medals, in another it is all 1 1/2" medals, etc. Thanks for your help!!
Technical SEO | | trophycentraltrophiesandawards0 -
How to get user genreated reviews indexed properly?
We are currently working to improve the deployment of a review widget on our website. The widget was deployed about 18 months ago and all reviews are behind Java navigation. I have been working with our IT staff to get the reviews into an HTML page which will either live on the product page as a tab or will be a link from the product page. Our IT staff has suggested leaving the Java navigation for users and creating separate HTML pages specifically for search engines. Based on my experience, this sounds like a bad idea, basically creating pages just for search engines that will not be use by site visitors, although the visitors will have access to the same content via the Java navigation. Anyone care to comment on this? Is creating HTML pages specifically for search engines a bad idea? An acceptable idea?
Technical SEO | | seorunner0