Forum Moderators: phranque

Message Too Old, No Replies

Htaccess redirect of index.html

         

snakefoot

9:53 am on Apr 30, 2009 (gmt 0)

10+ Year Member



Hi ya

I have moved host from a IIS to Apache, and I don't have much experience with htaccess files.

I'm trying to redirect the following url:

http://example.com/index.html

To this url:

http://example.com/

Very simple indeed but when I try the following:

Redirect 301 ^/index.html http://example.com/

Then nothing is happening. I also have access to modrewrite.

Do anyone know how to make this happen ?

jdMorgan

1:02 pm on Apr 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are actually some pitfalls involved with coding this, so instead of re-iterating them here, I'll suggest a review of some of these previous threads [google.com] as a good place to start. Please post specific questions or problems back here.

Jim

g1smd

12:24 am on May 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You'll likely want to invoke a redirect for any index file in any folder or in the root.

There's a lot of prior example code for that.

snakefoot

10:59 am on May 1, 2009 (gmt 0)

10+ Year Member



Thank you for your wonderful google search. I found a solution that redirects index.html to the root. Seems one have to check that the url-request explicit requested index.html or else it loops or doesn't do anything.

rewritecond %{the_request} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ / [R=301,L]

g1smd

11:47 am on May 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That code has several problems:

It does not fix the canonical domain name. The target of a redirect should contain the protocol and domain name.

It only works for the root index file, not for folders. You should also fix the problem in all folders.

You need to write

RewriteCond
and
THE_REQUEST
exactly like that.

jdMorgan

2:58 pm on May 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For example:

# Externally redirect direct client requests for "/index.html" in any directory to "/" in that same directory
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]

will redirect clinet requests for example.com/index.html to example.com/ and it will redirect example.com/foo/bar/index.html to example.com/foo/bar/

In addition to g1smd's recommendations, I'll add that it is a very good idea to leave accurate and concise comments in the code, so that you can tell what on earth it was intended to do when you look at it again in five years.

Jim

g1smd

4:44 pm on May 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As ever, the
\.html
part can be replaced with whatever suffix you need and/or given options like
\.html?
or
\.(html¦php)
too.

jdMorgan

4:50 pm on May 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



... if done in both lines! :)

Jim

snakefoot

9:18 pm on May 1, 2009 (gmt 0)

10+ Year Member



My problem is that I'm trying to perform redirect from an old site structure to a new structure (Converted from static html pages to Wordpress). I have replaced the static html pages with a 302 redirect, but I have to perform an explicit redirect to avoid sending the request to the Wordpress engine. The following redirect fixed all redirection except for index.html.

RewriteRule /tweak/ /articles/ [R=301,L]
RewriteRule /tweak/(.*) /tweak/$1 [R=301,L]

Now after adding these lines to the htaccess, then it doesn't send the index.html request to the Wordpress-engine, but performs a proper redirect.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /tweak/([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ /articles/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ / [R=301,L]

Though I would prefere if it just reached the actual index.html and did a 302 html-redirect.

jdMorgan

9:38 pm on May 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is this all of your code?

It's fairly certain that you do not *have to* use a redirect to avoid the request being rewritten to WordPress.

Your solution of using a redirect to avoid WordPress is "non-optimal" as it would likely be simple matter to exclude certain subdirectories, hostnames or even filetypes from being rewritten to WP -- The WP code itself would just need a small tweak (one additional RewriteCond) and then you could simply use an internal rewrite for translating your article URLs to filepaths.

It's often a good idea to ask about "the plan" rather than jumping in to asking about code to implement a specific plan. In addition to the actual problem of coding, there are many side-effects: Usability, retaining old inbound links and bookmarks, recovering inbound PR from obsolete URLs, and even preserving your site's "TrustRank" in search engines.

Another point is that the order of rules makes a big difference. In general, you want to put all external redirects first, in order from most-specific pattern (fewest URLs affected) to least-specific pattern (most URLs affected), and then follow them with all internal redirects, again in order from most-specific pattern to least-specific pattern.

This avoids creating situations where multiple redirects are invoked when a URL with mutltiple faults is requested, and also avoids the problem of an internal rewrite being exposed to the client by a subsequent external redirect. It can also prevent unexpected operation, such as the problem of having certain URLs rewritten to a blog or forum script when you don't want them to be.

And finally, it's a very good idea to specify the protocol (http) and the canonical domain in all external redirect rules. Again, this prevents creating opportunities for multiple "chained" or "stacked" redirects to result from a single HTTP request from the client.

Jim

snakefoot

10:24 am on May 2, 2009 (gmt 0)

10+ Year Member



The problem is there is an overlap between the old and new site structure. And the first few lines worked perfectly when I was using ISAPI rewrite on IIS. But stopped working for index.html when changing to Apache and htaccess.

Most Wordpress blogs uses the way of performing non-wordpress redirects first, and then let the rest go to the Wordpress-engine, that can generate custom 404-pages etc.

The whole reason for making this effort is to avoid giving the user the confusing 404 page.