homepage Welcome to WebmasterWorld Guest from 54.161.191.254
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Setting Up .htaccess File For Apache
Setting up 301 redirect and 404 custom pages in an .htaccess
TeamSDA




msg:4286582
 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

 

g1smd




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

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.

TeamSDA




msg:4287115
 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

g1smd




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

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.

TeamSDA




msg:4287569
 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

g1smd




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

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.

TeamSDA




msg:4288706
 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

g1smd




msg:4288741
 10:16 pm on Mar 28, 2011 (gmt 0)

AddOutputFilterByType is now deprecated by Apache.

Use mod_filter in the future.

TeamSDA




msg:4289568
 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

SEOtop10




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

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!

SEOtop10




msg:4289986
 8:21 am on Mar 31, 2011 (gmt 0)

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.

TeamSDA




msg:4290270
 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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved