Forum Moderators: phranque

Message Too Old, No Replies

Permanent redirect to index.php in a directory

How to do it correctly?

         

fargo1999

9:20 pm on Jan 20, 2009 (gmt 0)



Could someone help me with this: I'd like to forward 301 request to the .index.php file in a directory. I tried to do this below, but it didn't work... :¦. In other words, when someone clicks on this link: [domain.com...] he should be transferred permanently to: [domain.com...]

------

RewriteCond %{REQUEST_URI} ^$
RewriteCond %{HTTP_HOST} ^domain.com/Directory/$
RewriteRule ^$ [domain.com...] [L,R=301]

g1smd

11:08 pm on Jan 20, 2009 (gmt 0)

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



The
HTTP_HOST
variable holds exactly the domain name, and not any server filepath information.

You should be redirecting the other way, from "index" to "/". Search engines prefer the shorter of the two URLs.

Redirect to strip the index filename off the URL, otherwise you might end up with a problem like this: [webmasterworld.com...]

Ensure you add

DirectoryIndex index.php
so that requests for "/" are internally rewritten to the correct index file in the server.

fargo1999

11:23 pm on Jan 20, 2009 (gmt 0)



Thanks, but I already have indexed as /directory/index.php and all internal links lead to the index.php too... So I'd like to keep it this way. On the Internet I only found how to redirect from root to index -- but how to do it the other way? :)

g1smd

11:54 pm on Jan 20, 2009 (gmt 0)

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



The other way is listed in this forum at least three or four times every week. :)

See the other thread (listed above) for the ever deeper mire when doing it wrong.

jdMorgan

2:43 am on Jan 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll answer the question as posed, although I concur with g1smd's advice on using "/" as the index URL. The only circumstances under which I would recommend *not* changing to do that are if the business is currently in trouble and can't afford to change anything, or if the business plan is a short one, with no plans to continue beyond the next year. If the business is healthy and has a long-term business plan, then I'd advise fixing this and all other canonicalization problems now, so that you won't have a series of 'emergencies' and have to fix them one at a time.

Let's put it this way, Do you search at www.google.com/, or at www.google.com/index.py?

However, the correct code for the problem as stated, for use in example.com/.htaccess, would be:


RewriteRule ^Directory/$ http://www.example.com/Directory/index.php [L,R=301]

The RewriteCond testing the %{REQUEST_URI} wasn't correct, and wasn't needed anyway.

The RewriteCond testing %{HTTP_HOST} isn't needed unless you have multiple subdomains or domains, and only want to apply this redirect to some of them.

Jim

fargo1999

1:58 am on Jan 23, 2009 (gmt 0)



Thanks Jim, you are the expert in this I think :)