PDA

View Full Version : htaccess



phanio
12-29-2009, 11:36 AM
I had someone recently tell me that an htaccess file can help consolidate website rankings - i.e. http://www.yoursite.com and http://yoursite.com would share the same ranking.

But, in my research, I am only finding that an htaccess file allows external access to your server files.

Can someone provide some clearity?

vangogh
12-29-2009, 11:57 AM
Joseph when a search engine sees domain.com and www.domain.com they see them as two completely different URLs. You and I know that they're probably pointing to the same page, but they don't have to be. www is technically a subdomain. So if your site is accessible through both the www and non www versions you essentially have duplicate content for every page of your site as far as a search engine is concerned. So every page on your site ends up competing with itself.

Say one page links to domain.com and another links to www.domain.com. Instead of having 2 links pointing to your home page, you actually have one link pointing to each of 2 different pages.

.htaccess files allow you to set directives for different things on your site, one being how one URL redirects to another. Assuming your server is running Apache you should be able to create and modify your .htaccess files.

To create one it needs to be named without a name and only the .htaccess extension. It also needs to be created in a text editor and not something like Microsoft Word. The extra characters Word created in the background will cause problems. You can use Notepad.

You can actually have more than one .htaccess file on your site. Once can go in each directory. For the non www to www redirection you'd place the file in the root (where your index.html files is located). It doesn't mater whether you point the non www version to the www version or the other way around, but convention is to point non www to www.

Here's some code you can use



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


Just change domain in the two lines above to your domain. Then if someone types domain.com in their address bar they'll end up on www.domain.com

Spider
12-29-2009, 01:06 PM
Excellent! I am going to create a .htaccess file to do that. Thanks, VG.

Is that the entirety of the code that's needed? No header code? No finishing code? <open> <close>? <body> </body>?

I believe another use for .htaccess is to direct a 404 error to read one's own 404.html file rather than the system 404 file. Do you know the code for that, and can it be added to the code you just gave us?

rezzy
12-29-2009, 03:20 PM
The htaccess file does alot for you. It pretty much tells the server what files to server and where to serve them.

Ive accidentally edited my file and thrown my site into an infinite loop of redirects. You can also password protect, redirect, block, etc directories.

To make a custom 404 page, you would add
ErrorDocument 404 /404page.html

And you would want to add that ontop of whats already there. In this case, error docuement 404 is location in the root directory and is named 404page.html. You can make anytype of modifications you like. The same would apply for any other type of error your site may encounter, 502, etc.

When you login into your system, the htaccess file maybe not be visible, it depends on your hosts configuration. In any case, you should be able to ftp or other upload and the file will take hold and be used.

Wordpress "automagically" does this type of manipulation.

vangogh
12-29-2009, 03:32 PM
Frederick it's not an html page so you don't need all that extra code. The 3 lines of coded I provided are what I use on my sites. I think there are different ways to do the same.

Content Management Systems regularly use the file to rewrite URLs. For example by default when you create a page or post in WordPress it's URL is going to be something like domain.com/?p=23

WordPress (and other CMSs) will create permalinks or seo friendly urls so the URL above becomes domain.com/category/postname or however you set it. It's all done through url rewriting in your .htaccess file.

phanio
01-05-2010, 03:35 PM
Thanks this is all good information. I will start working on the htaccess file. I already have a custom 404. But, I created a new html page, named it 404, then, through my hosting company, simply told them where and what it is. They did the rest. I really like custom 404s.

phanio
01-05-2010, 06:13 PM
Just implemented the code - works great.

What other uses are there - uses that are really needed?

Should this only be used for the home page or should this be done for all pages as all pages can be www or non www?

billbenson
01-05-2010, 07:58 PM
The htaccess file overrides the settings of software installed on a server such as php. It can let you change php settings to that required by software you are using in some cases (rarely necessary, but something to be aware of).

It can be used to create user friendly page names. You could rewrite a page name from domain.com/index.php?id=17 to domain.com/winter-coats.html for example.

It can be used for password protection, forwarding pages from one domain to another, forwarding an entire site...

It does a bunch of stuff.

vangogh
01-06-2010, 12:52 PM
hould this only be used for the home page or should this be done for all pages as all pages can be www or non www?

The code I gave you above should set this up across all pages and yes you want all pages to use either the www or the non www, but not both.

Like Bill said there are plenty of uses for .htaccess such as the few he mentioned.

phanio
01-07-2010, 12:00 PM
Any downsides to having this file?

TulsaWeb
01-07-2010, 12:04 PM
Thanks rezzy for mentioning the 404 page redirect in .htaccess. Having a 404 page with a search form or a sitemap is important. I don't want my site visitors to slip away just because of a spelling error or old link.

You can also set cache and file compression rules in .htaccess, which can be helpful in speeding up your site load time, and permanent (303) redirects for important pages that have moved so your visitors don't get the 404 error.

TulsaWeb
01-07-2010, 12:06 PM
Hi Phanio,

No downsides. It's the last config file recognized by apache server.

billbenson
01-07-2010, 04:03 PM
Only if misused. You could do things with it that create a greater server load or take your site down. Remember, its a file that tells the server what to do with you site or directory. If you use it to tell the server to do something stupid, it may.

phanio
01-15-2010, 11:41 AM
Thanks - It is working very well for me. One quick question: Should this also be used for index sites - i.e. www.yoursite.com/index? I just noticed that my url www.mysite.com/index is taking backlinks and ranking from where I want them to be - www.mysite.com. Should I also redirect the index to my main url?

Lastly, could you explain more about "cache and file compression rules"

Good stuff on this forum!

vangogh
01-15-2010, 12:30 PM
Joseph you can also redirect the index file back to the main domain. Usually when I link to the page internally I also only link to the domain and not to the index file specifically. Since domain.com and domain.com/index.html are the same page it does make sense to only have one version of the URL included in search engines.

With a CMS much of your content is pulled from a database. That's often the most time consuming part of displaying a page, especially when a lot of people are trying to visit that page. What caching does is it stores a static version of the page. Most of the time your content isn't really changing even if it's being stored in a database. With caching a static version of the page is saved and when someone requests the page the cached version is shown instead of making the call to the database. The cached version will also be updated periodically to reflect any changes that have been made to the page.

The idea behind compression is to remove as much extra information as possible. For example with html, css, javascript files we use a lot of whitespace to make the files easy for you and I to read. Your browser doesn't need that extra space so compressing the file could be as simple as removing all the extra whitespace.

With an image say you have a 16px x 16px square all filled with the color red. One way to record that info is to record each of the 256 individual px and what color each contains. Or you could store the information as a single block of 16px squared and know that everything inside is filled with the color red.

When the image is compressed the goal will also be to remove as much extra information as possible to make the file size smaller. Sometimes this does mean losing a bit of quality. It depends on how far you want to push the compression and approximate some information as opposed to recording every last detail.

phanio
01-16-2010, 01:35 PM
Thanks - I will get working on this - do you use the same code you displayed before or can you shorten it using a redirect old to new?

Thanks for the information on CMS. I will look into this some more. I try to keep my pages small so they load quick - but over time I keep adding more and more.

Lastly, I tried to sign in last night and could not for some reason. It kept locking up my browser. Thought you might like to know.

vangogh
01-18-2010, 11:07 AM
You can use this to redirect the index page



RewriteCond &#37;{REQUEST_URI} ^/.*index.html$
RewriteRule ^(.*)index.html$ $1 [R=301,L]


Just change the .html extension to whatever extension your index page uses.

Thanks for the info about the forum. Hopefully it was just a temporary thing.

phanio
01-19-2010, 10:54 AM
Vangogh - you are the best. Where do you find informaiton about htaccess code? I tried a few sites and then tried a few of their suggestions. The best I got was my site crashed.

vangogh
01-19-2010, 11:22 AM
Glad to help. Some of it I know from experience, but most of the specifics I just search for. I can never remember all the specific code for .htaccess, but I know how to find it online.

I think for the above code I searched "301 redirect old new domain" or something like that. I guess having searched for enough web development stuff like this I know which sites in the results will likely provide the best code.

phanio
01-19-2010, 06:18 PM
Thanks again. I am having trouble with this code - it is creating a 404 error no matter what url I load. But, I will try to figure this out as I would like to know more about this code. I will try your suggestion.

vangogh
01-19-2010, 06:41 PM
If you can't figure it out let me know and I'll see if I can help sort things out.

phanio
01-19-2010, 06:56 PM
I got it. Thanks for the search idea on what to search for. I found out that a lot of these 'redirect information sites' are providing some code that merely makes a site redirect to itself - thus a never-ending loop.

But, there is code that will specifically redirect an /index.html to the url you want. Here is what I found:

RewriteCond &#37;{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]

"This redirects *only* if the original client (i.e. browser or robot) request is for "/index.html", and will not redirect as a result of the action of the DirectoryIndex directive rewriting requests for "/" back to "/index.html", which is the likely cause of your loop as noted by your host. It will work at any directory level."

The 'noted by your host' comment was in reply to a post where the poster stated that when he contacted his host they told him he was in a redirect loop.

phanio
01-19-2010, 06:57 PM
If you can't figure it out let me know and I'll see if I can help sort things out.

Thanks Vangogh - learning everyday and loving it!

billbenson
01-19-2010, 10:16 PM
htaccess frequently uses "regular expressions" Regular expression - Wikipedia, the free encyclopedia (http://en.wikipedia.org/wiki/Regular_expression)

Take a peek at it to get an idea of some of what the htaccess is doing. It might help you de bug some of the htaccess stuff you find. Regular expressions are difficult and why I and I gather Vangogh prefer to just search for the code that someone else has written.

vangogh
01-20-2010, 11:01 AM
I'm glad you got it working Joseph. One thing I've found about .htaccess is that even a character wrong could mean your whole site stops working like you expect. Fortunately there's a lot of code online to try until you find the right snippet.