Forum Moderators: phranque

Message Too Old, No Replies

Redirect /directory/ to /directory/index.php

         

HoboTraveler

9:09 am on Apr 13, 2010 (gmt 0)

10+ Year Member



Hi All,

I've been searching for a method that would Redirect /directory/ to /directory/index.php

I have multiple directories on the server

/directorytest/
/directoryfoo/
/directoryfoobar/

I need to have all of these resolve to:

/directorytest/index.php
/directoryfoo/index.php
/directoryfoobar/index.php

Is there a .htaccess rewrite rule that would work? I've found rules that do the opposite.

Thank You

g1smd

5:19 pm on Apr 13, 2010 (gmt 0)

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



Never use
example.com/folder/index.php
as the URL that people see out on the web.

Use
example.com/folder/
as the URL used out on the web.

DirectoryIndex index.php
allows those URL requests to access the index.php file to serve the content.

Redirect all direct client requests for the URL
example.com/folder/index.php
to new URL
example.com/folder/
to prevent Duplicate Content issues.

jdMorgan

6:00 pm on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you found only code to do the opposite because in fact it is almost always a mistake to use a redirect from the URL "/" to the file /index.php -- or to any other file. The threads you found were started by folks trying to un-do the damage they'd done, or at least to shorten their URLs to make them "prettier."

Also, there is no good reason to expose the fact that you use PHP on your site, or to make that 'permanent' by hard-coding it into your URLs. What if you need to switch to .asp or .jsp next year? You'd have to redirect all those URLs and take a ranking hit (usually but not always temporary) while the search engines digested the meaning of those redirects...

Instead, use the DirectoryIndex directive to declare the index file you want to serve for those directories -- or for all directories. See Apache mod_dir for information on DirectoryIndex.

DirectoryIndex index.php

is likely all you need. This is an internal URL-to-filepath rewrite, not a URL-to-URL redirect.

Jim

HoboTraveler

7:05 pm on Apr 13, 2010 (gmt 0)

10+ Year Member



Hello,

We do not have a real index.php since the index.php is stored in the database.

Opening example.com/folder/ results in a 404 because a real index.php file does not exist.

This is why I need to direct all the / from directories to index.php in the directory. Then the index.php is looked up in the database.

Is there a htaccess rule that does this?

Thanks!

jdMorgan

7:17 pm on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the name of the script file that does this database lookup? And where is the script located in the filesystem?

You *do not* want a redirect here. Further details will help to clarify that point.

Jim

HoboTraveler

11:14 am on Apr 14, 2010 (gmt 0)

10+ Year Member



The name of the script is get.php. It is located in the directory /get/get.php

I have a .htaccess rule that sends filenames to the get.php and the database lookup is done that way.

RewriteRule ^([^/\.]+)/([^/\.]+).php/?$ /get/get.php?file=$0 [NC] [L]

Other than the get.php, none of the .php files exist in the filesystem. They're all stored in the db.

This is why I need a .htaccess that would say example.com/folder/ is example.com/folder/index.php and then get.php would grab the contents.

Currently, get.php is unable to grab the contents if the user visits /example.com/folder/ because I do not know how to create a .htaccess rule that says send this type of request example.com/folder/ to get.php

Thanks

jdMorgan

1:46 pm on Apr 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Modify your original rule then, to make the "filepath" part optional:

RewriteRule ^([^/]+/([^/.]+\.php/?)?)$ /get/get.php?file=$1 [NC,L]

With this code, you may need to modify the get.php script to accept "<any-dir>/file=<blank>" and treat that as equivalent to "<any-dir>/file=index.php"

Alternately, use two rules, one to handle the index-filename-present case, and the other to handle the no-index-filename-present case, rewriting in a single step to get.php in either case rather than 'stacking' two rewriterule invocations one after the other.

Redirecting the client to "/<anydir>/index.php" when just "/<anydir>/" was requested would have exposed "index.php" as a URL to browsers and search engines, and would have clobbered your search rankings for days to months, and would have resulted in the "index.php" URLs being listed in search results.

Rewriting the request in two steps would have likely triggered a documented mod_rewrite bug [archive.apache.org] that re-injects extra "copies" of the path info into the req_rec variable, making your RewriteRule patterns fail to match. (This bug --contrary to notes in the bug report-- has never been fixed. Therefore, it is best to *never* let more than one RewriteRule apply to any single HTTP request).

Also be aware that although it may work on some servers, the $0 variable is not documented as being defined by mod_rewrite.

Jim

[edited by: jdMorgan at 1:14 pm (utc) on Apr 15, 2010]

g1smd

7:40 pm on Apr 14, 2010 (gmt 0)

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



There are several ways to get your site to associate the URL "/" with the database entry "index.php".

It can be done either by using two rules instead of one for your rewrites, or your script can test for "blank" file name and serve the content from the index.php database entry.

Either way will work. One is some changes to your .htaccess file. The other is some changes to your PHP script. Neither are particularly complicated. The PHP changes will take longer to do but will be more robust.

HoboTraveler

6:13 am on Apr 16, 2010 (gmt 0)

10+ Year Member



@jdMorgan

I updated the rule. "/<anydir>/" is still not sent to get.php

RewriteRule ^([^/]+/([^/.]+\.php/?)?)$ /get/get.php?file=$1 [NC,L]

The 403 forbidden error appears:
You don't have permission to access /folder/ on this server.

Does this appear because a real /folder/ exists but there is no real index.php?

jdMorgan

3:37 pm on Apr 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Possibly. And you probably have "-Indexes" set in your "Options" directive -- or this option is set in the default server config file.

Do you want the server to generate directory listings? Usually, most Webmasters don't do this any more.

I suggest the Options settings shown in the code snippet below for general use. However, be sure to look up each option in the Apache core documentation, and satisfy yourself that the setting is appropriate for your site.

A secondary cause of trouble is that your rule is now recursive -- The "output" filepath will match the RewriteRule's pattern, and therefore, the request will be rewritten again and again and again. After a lot of wasted CPU time, the "file" query parameter will always end up as "file=get/get.php"

So, you'll want to exclude "/get/get.php" from being rewritten.

Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine on
#
RewriteCond $1 !^get/get\.php$
RewriteRule ^([^/]+/([^/.]+\.php/?)?)$ /get/get.php?file=$1 [NC,L]

Jim