Score:1

.htaccess redirect sub-folders to the correspondent index.php

us flag

How can I redirect each request for a subfolder to the index.php in the same folder

myserver.com/folder/subfolder1/test   redirected to   folder/subfolder1/index.php
myserver.com/folder/subfolder2/test   redirected to   folder/subfolder2/index.php
...

I added a .htaccess for each sub folder like this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [L,QSA] 

I would like to redirect them with one .htaccess in the parent folder :

Folder/
        .htaccess    <-- one redirection here
        subfolder1/
                index.php
        subfolder2/
                index.php
        subfolder3/
                index.php
          ...  

        subfolder n /
                index.php

P.S. : all the sub-folders are with different names and do not follow a naming convention.

Score:0
us flag
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+)/.*$ $1/index.php [L,QSA]

I've found the answer , with this rule every folder is redirected to it index.php if the file does not exist.

Score:0
kz flag

You just need to make sure that DirectoryIndex is set correctly in the root .htaccess file. (This defaults to index.html, although many distros include index.php as well, so you often don't need to do anything!)

For example:

DirectoryIndex index.php

This tells Apache (mod_dir) to look for an index.php file in the requested directory (note the relative URL-path) when you request the directory directly.

If the DirectoryIndex document (ie. index.php) is not found then you will get a 403 Forbidden (assuming mod_autoindex is enabled and directory listings are disabled, ie. Options -Indexes).

There is nothing more you need to add. (Unless you actually want an "external redirect"?!)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [L,QSA]

You do not need to use mod_rewrite. In fact, this rule won't do anything here anyway, since the 2nd condition explicitly excludes requests to directories.

Reference:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.