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
-
Using hreflang tags properly.
On my site "example.com" I have set up the following in the header: The problem is that the tags are universal across the site, so every page has these tags, leading obviously to no return tag errors. I.e. the page www.example.ca/testing.html still has the tags: Not tags with "testing.html" in them. How bad is this? Does it matter?
Technical SEO | | absoauto0 -
Has anyone tested or knows whether it makes a difference to upload a disavow file to both www. and non-www. versions of your site in GWMT?
Although Google treats both as separate sites, I always assumed that uploading the disavow file to the canonical version of your site would solve the problem. Is this the case, or has anyone seen better results uploading to both versions?
Technical SEO | | CustardOnlineMarketing0 -
Sub Domains and Robot.txt files...
This is going to seem like a stupid question, and perhaps it is but I am pulling out what little hair I have left. I have a sub level domain on which a website sits. The Main domain has a robots.txt file that disallows all robots. It has been two weeks, I submitted the sitemap through webmaster tools and still, Google has not indexed the sub domain website. My question is, could the robots.txt file on the main domain be affecting the crawlability of the website on the sub domain? I wouldn't have thought so but I can find nothing else. Thanks in advance.
Technical SEO | | Vizergy0 -
Htaccess - multiple matches by error
Hi all, I stumbled upon an issue on my site. We have a video section: www.holdnyt.dk/video htaccess rule: RewriteCond %{REQUEST_FILENAME} !-f
Technical SEO | | rasmusbang
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^video index.php?area=video [L,QSA] Problem is that these URLs give the same content:
www.holdnyt.dk/anystring/video
www.holdnyt.dk/whatsoever/video Any one with a take on whats wrong with the htaccess line? -Rasmus0 -
Combining 2 blogs into one. What is quicker, easier and better - rel canonical or an htaccess/ 301?
The objective I have is to archive an entire blog (which I no longer have time to keep up) with multiple posts over 4years , into another blog as a a folder. My question: would it be quicker and easier to do a rel canonical, or separately list all pages in htaccess and do a 301 redirect.
Technical SEO | | charlesgrimm0 -
Converting files from .html to .php or editing .htaccess file
Good day all, I have a bunch of files that are .html and I want to add some .php to them. It seems my 2 options are Convert .html to .php and 301 redirect or add this line of code to my .htaccess file and keep all files that are .html as .html AddType application/x-httpd-php .html My gut is that the 2nd way is better so as not alter any SEO rankings, but wanted to see if anybody had any experience with this line of code in their .htaccess file as definitely don't wan to mess up my entire site 🙂 Thanks for any help! John
Technical SEO | | JohnHerrigel0 -
Track PDF files downloaded from my site
I came across this code for tracking PDF files [1. map.pdf ( name of PDF file ) and files is the folder name. Am i right ? 2. What shall i be able to track using the code given above ? a ) No. of clicks on links or how many persons downloaded the PDF files ? 3. Where in Google this report will be visible ? Thanks a lot.](http://www.example.com/files/map.pdf)
Technical SEO | | seoug_20050 -
Htaccess 301s to 3 different sites
Hi, I'm an htaccess newbie, and I have to redirect and split traffic to three new domains from site A. The original home page has most of the inbound links so I've set up a 301 that goes to site B, the new corporate domain. Options +FollowSymLinks
Technical SEO | | ellenru
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L] Brand websites C and D need 301s for their folders in site A but I have no idea how to write that in relationship to the first redirect, which really is about the home page, contact and only a few other pages. The urls are duplicates except for the new domain names. They're all on Linux..Site A is about 150 pages, should I write it by page, or can I do some kind of catch all (the first 301) plus the two folders? I'd really appreciate any insight you have and especially if you can show me how to write it. Thanks 🙂0