Welcome to WebmasterWorld Guest from 54.205.88.118

Forum Moderators: Ocean10000 & incrediBILL & phranque

Setting Up .htaccess File For Apache

Setting up 301 redirect and 404 custom pages in an .htaccess

   
11:44 pm on Mar 23, 2011 (gmt 0)



Hi All,

Looking through apache.org and a other couple forums, Iíve put together the code that is listed below for my .htaccess file. Since Iím just a beginner at putting together an .htaccess file, can anyone see any issues with the code? Iíve listed the functions of this code below.

1. Redirects non-www. to www.

2. Redirects www.example.com/index.html to the root (www.example.com)

3. Take all 404 Errors to the custom error page


RewriteEngine on RewriteCond %{HTTP_HOST} !^www.example.com [NC] RewriteCond %{HTTP_HOST} !^$ RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]

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

ErrorDocument 404 /Errors/404.html




Thank you for your help,

TeamSDA_Christian
12:43 am on Mar 24, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Run through what happens when you request example.com/index.html and see that the request is redirected twice, once to www and again to remove the index.html part.

Consider if the two rules were reversed, this would not happen. Always try to avoid a multi-step redirection chain.

There's a number of typos in the code. Literal periods in patterns need escaping too.

One of the redirects is a 302. You need 301.

The code redirects non-www requests but doesn't fix requests for www.example.com:80

The rules should each be on a new line.

ErrorDocument 404 /Errors/404.html
#
RewriteEngine on
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html(\?[^\ ])?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]
#
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


You'll find the exact same code in several hundred prior threads stretching back several years.
10:42 pm on Mar 24, 2011 (gmt 0)



Hi g1smd,

Thank you very much for your help.

I tested both your and my code, both giving the proper functions.

I read on apache.org that the code could have a small unknown issue that could bring down you SEO rankings, even though everything is working properly. How can you test the code to make sure the robots are reading everything correctly.

TeamSDA_Christian
12:39 am on Mar 25, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Run Xenu LinkSleuth over your site and carefully study the reports.

Use the Live HTTP Headers for Firefox extension and request both www and non-www URLs, both with and without the index.html filename.
8:23 pm on Mar 25, 2011 (gmt 0)



Hi g1smd,

Thanks again for your help. I tested the HTTP Headers and everything looks good. I'll need to look over my reports later.

I ran into an issue with our 404 error page. It works perfectly when www.example.com/noexistantpage is typed in the url. When I type in www.example.com/nonexistentfolder/nonexistantpage it breaks. The html code is functioning but the images can't be found. How do I solve this issue.

Christian
9:47 pm on Mar 25, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



It is the browser that resolves what the URL for the images is, so you must use absolute paths on your links to images on the error page.

If you use relative paths, the image will be looked for in a place relative to the failed URL and the images will fail to display.

Use a leading slash on all paths to images and specify the full path to the file.
8:56 pm on Mar 28, 2011 (gmt 0)



Hi g1smd,

Now that my 404 error page and index.html/non-www. being redirected properly. I'm trying to figure out how to turn on mod_deflate for compression. I've included two examples of what I found on activating mod_deflate. Both are functioning correctly according to [gidnetwork.com...] Which one should be used?

Secondly is it better to use mod_deflate or mod_gzip for an Apache server? I've read mod_deflate requires a lot less server power to compress the mod_gzip. What are your thoughts?



ErrorDocument 404 /errorpage.html

RewriteEngine on

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

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>


or


ErrorDocument 404 /errorpage.html

RewriteEngine on

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

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
10:16 pm on Mar 28, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



AddOutputFilterByType is now deprecated by Apache.

Use mod_filter in the future.
4:14 pm on Mar 30, 2011 (gmt 0)



Hi g1smd,

When you say us mod_filter, what are you referring to? How would I set that up?


Christian
7:25 am on Mar 31, 2011 (gmt 0)

10+ Year Member



I am also curious to know how to use mod_filter to achieve this:

AddOutputFilterByType DEFLATE text/htm text/html text/css text/xml text/plain application/javascript

Thanks for your help!
8:21 am on Mar 31, 2011 (gmt 0)

10+ Year Member



I located the following on github (https://github.com/adeelejaz/html5-boilerplate/commit/8336a72bbd5897d8661b853e8d701125e6197b7f):

<IfModule mod_filter.c>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE resp=Content-Type $\btext/(html|plain|css|javascript|xml|x-component)\b
FilterProvider COMPRESS DEFLATE resp=Content-Type $\bapplication/(javascript|json|xml|x-javascript)\b
FilterChain COMPRESS
FilterProtocol COMPRESS change=yes;byteranges=no
</IfModule>

I have tried this on my server (2 sites) and it worked.
5:54 pm on Mar 31, 2011 (gmt 0)



Hi G1msd and SEOtop10,

Thank you for your help. Seeing as this as branched off into a new topic then I had originally posted. I posted a new thread, link below.

[webmasterworld.com...]


Christian
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month