Redirect 301 problem

Hi,

I recently changed some pages from shtml to php. Google is still indexing the previous shtml files so I’m redirecting the relevant pages.

To redirect people to the new pages I’m using htaccess file with the following kind of thing:


...
Redirect 301 /home.shtml http://www.somedomain.com/home.php
Redirect 301 /index.shtml http://www.somedomain.com/index.php
...

Where index.php is a sort of landing page.

This works fine but there are 2 (linked) problems:

  1. I noticed that when a user goes to www.somedomain.com it redirects to www.somedomain.com/index.php. While this still gets people to the right page, I would prefer it if the URL remained without the /index.php appended, as this shows up in search engines and also in the browser. So it’s not a huge problem, but one that I’d like to avoid.

  2. So to avoid the first problem I just removed the rule for index.php. However, now when you point a browser to www.somedomain.com it redirects to /home.php!! It seems like the redirect rule for index is being hi-jacked by the home rule!

Is this strange, or am I missing something here?! :confused:

Any help much appreciated!! :slight_smile:

Hi Stomme,

The DirectoryIndex directive did the trick. Thanks!
Added the following to the .htaccess file:

DirectoryIndex index.php

Without the above directive, using the following code resulted in a endless loop:

Redirect 301 /index.shtml http://www.example.com

I’m guessing that the server config had .shtml listed before .php, which lead to the above resulting in a loop (?) and home.shtml was deemed the DirectoryIndex before index.php (??)

A complete stab in the dark but perhaps, for example, extensions are in an outer loop when the server searches for a suitable DirectoryIndex (htm, html, shtml, php,…), while names are in an inner loop (index,home,…). Not sure, of course, but that would create the behaviour I was seeing, i.e. that the following directive would make index.php be ignored:

Redirect 301 /home.shtml http://www.example.com/home.php

Um, what’s the difference again between home and index?

Yes, the index/home distinction is a bit confusing: In this instance index.php is a landing or splash page, while home.php is the home page with actual useful info on it. It’s something I’m not keen on, but the client still wants it that way :slight_smile:

Ok, clear as mud, I’m sure. Anyway, thanks for that. Have a good’un!

I’m guessing that the server config had .shtml listed before .php, which lead to the above resulting in a loop (?) and home.shtml was deemed the DirectoryIndex before index.php (??)

Who knows? But as it says on the DirectoryIndex page I linked to, “index.html” is usually the default.

It’s something I’m not keen on, but the client still wants it that way

As a web developer, I feel your pain. Good luck.

Um, what’s the difference again between home and index? I’m confused because I personally tend to assume one is the other… obviously in your case it’s something different set up. I’ll answer the index.php one because I think I understand that one.

Assuming you want domain.com to send people to the file known as index.php, then you’ll want to state “index.php” as your DirectoryIndex.

http://httpd.apache.org/docs/2.1/mod/mod_dir.html#directoryindex
DirectoryIndex is part of the core mod_dir, so you can use it, and it can be set for the whole server or if you have various separate VirtualHosts, each one can haz its own DI leading to its own file.

So once you have that statement (it’s usually set near the top of your config file, long before any rewrites or anything), anyone going to example.com/ sees only example.com/ but gets the file at example.com/index.php

(in the docs, note the mention of the trailing slash on the hostname)

I’m not sure what you want to happen with home, so I’m skipping that.

Anyway, now for the redirect:


Redirect 301 /index.shtml http://www.example.com/

That ought to send those asking for http://www.example.com/index.shtml to http://www.example.com/, which then means Apache looks at the DirectoryIndex line, sees your “default file” is index.php, and shows that.

I wish I could tell more about the home thing. Are you also trying to make home.shtml go to http://www.example.com/home ? (instead of home.php?)