Redirect index.html to domain root

Discuss practical ways rearrange URLs using mod_rewrite.

Redirect index.html to domain root

Postby Falcon1986 » Sun Oct 19, 2008 11:10 am

Good day!

After viewing some 'NOT FOUND' errors for my Wordpress website through Google Webmaster Tools, I went ahead and started doing some 301 redirects to the correct links. However, since I no longer use an index.html file in the website's root directory, I would prefer if there was a way to redirect all requests for http://mydomain.com/index.html to http://mydomain.com/. A working index.php file resides in the website's root directory for which Wordpress loads upon requests for http://mydomain.com/, but hides the index.php part in the URL.

I found the following through a Google search and it worked for a while, until I started getting "500 Internal Server Errors" just recently. Upon deleting it, things returned to normal (strange!). I really don't know what happened.

Code: Select all
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.html\ HTTP/
RewriteRule ^/?index\.php$ http://mydomain.com/ [R=301,L]RewriteCond %{HTTP_HOST} ^mydomain.com$RewriteCond %{HTTP_HOST} ^mydomain.com$


Does anyone see what may be wrong or needs modifications? Thanks!
Falcon1986
 
Posts: 8
Joined: Sun Oct 19, 2008 10:56 am

Postby richardk » Sun Oct 19, 2008 11:28 am

What you have there matches index.php, not index.html.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{THE_REQUEST} \ /(.+/)?index\.html\  [NC]
RewriteRule ^(.+/)?index\.html$ http://mydomain.com/$1? [R=301,L]

Do you have any other mod_rewrite?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Falcon1986 » Mon Oct 20, 2008 8:35 am

Thanks for the suggestion, 'richardk'!

Unfortunately, that modification had no effect on redirecting requests for index.html to the website root. I only get a 404.

I do have a few other mod_rewrite entries, but they are for security purposes and dealing with bots/spam, most of which I got to work successfully after reading articles on AskApache.com.
Falcon1986
 
Posts: 8
Joined: Sun Oct 19, 2008 10:56 am

Postby richardk » Mon Oct 20, 2008 11:50 am

Post your full mod_rewrite so i can see if there is a conflict.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Falcon1986 » Tue Oct 21, 2008 5:08 am

Sorry that I did not respond earlier. I get the following error when trying to post the .htaccess file code:
Error 412 Precondition Failed
The precondition on the request for the URL /posting.php evaluated to false.
Falcon1986
 
Posts: 8
Joined: Sun Oct 19, 2008 10:56 am

Postby Falcon1986 » Tue Oct 21, 2008 5:22 am

I'll try posting the code in segments instead. It is pretty long! BTW, all references to my real domain name have been replaced with mydomain.com. Thanks again.

Segment 1
Code: Select all
# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php

# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

RewriteCond %{THE_REQUEST} \ /(.+/)?index\.html\  [NC]
RewriteRule ^(.+/)?index\.html$ http://mydomain.com/$1? [R=301,L]

<IfModule mod_gzip.c>
mod_gzip_on       Yes
mod_gzip_dechunk  Yes
mod_gzip_item_include file      \.(html?|txt|css|js|php|pl|jpg|png|gif)$
mod_gzip_item_include handler   ^cgi-script$
mod_gzip_item_include mime      ^text/.*
mod_gzip_item_include mime      ^application/x-javascript.*
mod_gzip_item_exclude mime      ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>


Segment 2
Code: Select all
# Force use of this timezone when webserver timezone is different
SetEnv TZ America/Toronto

# Specify 404 error for Google 404
ErrorDocument 404 /index.php?error=404

# Block no-referrer requests
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_METHOD}  POST
RewriteCond %{REQUEST_URI}     .wp-comments-post\.php*
RewriteCond %{HTTP_REFERER}    !.*mydomain.com.* [OR,NC]
RewriteCond %{HTTP_USER_AGENT} !^.*mozilla.*            [OR,NC]
RewriteCond %{HTTP_USER_AGENT} !^.*google.*             [OR,NC]
RewriteCond %{HTTP_USER_AGENT} !^.*slurp.*              [OR,NC]
RewriteCond %{HTTP_USER_AGENT} !^.*msn.*                [NC]
RewriteCond %{HTTP_USER_AGENT} ^$                       [NC]
RewriteRule ^(.*)$ ^http://www.google.com/$ [R=301,L]
</IfModule>

# Block proxies from commenting
RewriteCond %{REQUEST_METHOD} =POST
RewriteCond %{HTTP:VIA}%{HTTP:FORWARDED}%{HTTP:USERAGENT_VIA}%{HTTP:X_FORWARDED_FOR}%{HTTP:PROXY_CONNECTION} !^$ [OR]
RewriteCond %{HTTP:XPROXY_CONNECTION}%{HTTP:HTTP_PC_REMOTE_ADDR}%{HTTP:HTTP_CLIENT_IP} !^$
RewriteCond %{REQUEST_URI} !^/(wp-login.php|wp-admin/|wp-content/plugins/|wp-includes/).* [NC]
RewriteRule .* - [F,NS,L]


Segment 3
Code: Select all
<Files php.ini>
deny from all
</Files>

<Files fastphp.ini>
deny from all
</Files>

<Files wp-config.php>
Order Deny,Allow
Deny from All
</Files>

# Force PDF download
AddType application/octet-stream .pdf

# preserve bandwidth for PHP enabled servers
<ifmodule mod_php4.c>
php_value zlib.output_compression 16386
</ifmodule>

# secure htaccess file
<Files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
</Files>

# Directory index protection
Options -Indexes
DirectoryIndex index.php /index.php


Segment 4
Code: Select all
# Specify commends posts file
RewriteRule ^(.*)/wp-comments-post.php - [F,L]

# Counter AVG LinkScanner
RewriteCond %{HTTP_USER_AGENT} ".*MSIE 6.0; Windows NT 5.1; SV1.$" [OR]
RewriteCond %{HTTP_USER_AGENT} ".*MSIE 6.0; Windows NT 5.1;1813.$"
RewriteCond %{HTTP_REFERER} ^$
RewriteCond %{HTTP:Accept-Encoding} ^$
RewriteRule ^.* http://www.avg.com/?LinkScannerSucks [R=307,L]

# Deny trackback spam
RewriteCond %{REQUEST_METHOD} =POST
RewriteCond %{HTTP_USER_AGENT} ^.*(opera|mozilla|firefox|msie|safari).*$ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.+/trackback/?\ HTTP/ [NC]
RewriteRule .* - [F,NS,L]

# Deny comment with blank referer
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*/wp-comments-post\.php.*\ HTTP/ [NC]
RewriteCond %{HTTP_REFERER} ^-?$
RewriteRule .* - [F,NS,L]

# Deny post with no user agent
RewriteCond %{REQUEST_METHOD} =POST
RewriteCond %{HTTP_USER_AGENT} ^-?$
RewriteCond %{REQUEST_URI} !^/(wp-login.php|wp-admin/|wp-content/plugins/|wp-includes/).* [NC]
RewriteRule .* - [F,NS,L]

# Deny exploit using bogus graphics
RewriteCond %{HTTP:Content-Disposition} \.php [NC]
RewriteCond %{HTTP:Content-Type} image/.+ [NC]
RewriteRule .* - [F,NS,L]

# Deny requests with no host header
RewriteCond %{REQUEST_URI} !^/(wp-login.php|wp-admin/|wp-content/plugins/|wp-includes/).* [NC]
RewriteCond %{HTTP_HOST} ^$

RewriteRule .* - [F,NS,L]

# Deny bad content type
RewriteCond %{REQUEST_METHOD} =POST
RewriteCond %{HTTP:Content-Type} !^(application/x-www-form-urlencoded|multipart/form-data.*(boundary.*)?)$ [NC]
RewriteCond %{REQUEST_URI} !^/(wp-login.php|wp-admin/|wp-content/plugins/|wp-includes/).* [NC]
RewriteRule .* - [F,NS,L]

# Deny use of specific characters
RewriteCond %{REQUEST_URI} !^/(wp-login.php|wp-admin/|wp-content/plugins/|wp-includes/).* [NC]
RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ [a-zA-Z0-9\.\+_/\-\?\=\&]+\ HTTP/ [NC]
RewriteRule .* - [F,NS,L]


Segment 5
Code: Select all
# Deny access to evil robots site rippers offline browsers and other nasty scum
RewriteCond %{HTTP_USER_AGENT} ^(aesop_com_spiderman|alexibot|backweb|bandit|batchftp|bigfoot) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(black.?hole|blackwidow|blowfish|botalot|buddy|builtbottough|bullseye) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(cheesebot|cherrypicker|chinaclaw|collector|copier|copyrightcheck) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(cosmos|crescent|curl|custo|da|diibot|disco|dittospyder|dragonfly) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(drip|easydl|ebingbong|ecatch|eirgrabber|emailcollector|emailsiphon) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(emailwolf|erocrawler|exabot|eyenetie|filehound|flashget|flunky) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(frontpage|getright|getweb|go.?zilla|go-ahead-got-it|gotit|grabnet) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(grafula|harvest|hloader|hmview|httplib|httrack|humanlinks|ilsebot) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(infonavirobot|infotekies|intelliseek|interget|iria|jennybot|jetcar) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(joc|justview|jyxobot|kenjin|keyword|larbin|leechftp|lexibot|lftp|libweb) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(likse|linkscan|linkwalker|lnspiderguy|lwp|magnet|mag-net|markwatch) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(mata.?hari|memo|microsoft.?url|midown.?tool|miixpc|mirror|missigua) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(mister.?pix|moget|mozilla.?newt|nameprotect|navroad|backdoorbot|nearsite) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(net.?vampire|netants|netcraft|netmechanic|netspider|nextgensearchbot) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(attach|nicerspro|nimblecrawler|npbot|octopus|offline.?explorer) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(offline.?navigator|openfind|outfoxbot|pagegrabber|papa|pavuk) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(pcbrowser|php.?version.?tracker|pockey|propowerbot|prowebwalker) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(psbot|pump|queryn|recorder|realdownload|reaper|reget|true_robot) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(repomonkey|rma|internetseer|sitesnagger|siphon|slysearch|smartdownload) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(snake|snapbot|snoopy|sogou|spacebison|spankbot|spanner|sqworm|superbot) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(superhttp|surfbot|asterias|suzuran|szukacz|takeout|teleport) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(telesoft|the.?intraformant|thenomad|tighttwatbot|titan|urldispatcher) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(turingos|turnitinbot|urly.?warning|vacuum|vci|voideye|whacker) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(wget|widow|wisenutbot|wwwoffle|xaldon|xenu|zeus|zyborg|anonymouse) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^web(zip|emaile|enhancer|fetch|go.?is|auto|bandit|clip|copier|master|reaper|sauger|site.?quester|whack) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*(craftbot|download|extract|stripper|sucker|ninja|clshttp|webspider|leacher|collector|grabber|webpictures).*$ [NC]
RewriteRule . - [F,L]


There is one more segment containing RewriteCond statements for blocking e-mail spiders, but whenever I try to include it, I get that Error 412, perhaps because the forum's security is detecting the spider names as a possible spam post. :-?
Falcon1986
 
Posts: 8
Joined: Sun Oct 19, 2008 10:56 am

Postby richardk » Tue Oct 21, 2008 9:54 am

Next time please just post the whole thing in one piece (unless there are different files).

Try the index.html code at the very beginning before the Wordpress mod_rewrite.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Falcon1986 » Tue Oct 21, 2008 10:20 am

Oh, great! It works after I placed it at the beginning, i.e. before the Wordpress-related entries. :D

BTW, I though 'RewriteEngine On' needed to come before any 'Rewrite' statements.
Falcon1986
 
Posts: 8
Joined: Sun Oct 19, 2008 10:56 am

Postby richardk » Wed Oct 22, 2008 7:13 am

I though 'RewriteEngine On' needed to come before any 'Rewrite' statements.

It's a good idea to put it first (you can and the index.html redirect will still work) but it is not required.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 18 guests

cron