2

I need to be able to redirect absolutely all URLs in a directory to the index.php file in the directory. Unlike for all of the other answers I've found, this includes existing directories and existing files. I am using Apache with an .htaccess file to accomplish this. So far my .htaccess file is as follows:

 <IfModule mod_rewrite.c> RewriteEngine On RewriteRule /sub/directory/index\.php - [L] RewriteRule - /sub/directory/index\.php </IfModule> 

But for some reason it doesn't redirect anything to index.php. I've also tried:

 <IfModule mod_rewrite.c> RewriteEngine On RewriteRule /sub/directory/index\.php - [L] RewriteRule ^.*$ /sub/directory/index\.php </IfModule> 

But this gives me an inevitable "500 Error". What am I doing wrong? How can I simply redirect everything to index.php?

TIA

5 Answers 5

1

The error is with this RewriteRule

RewriteRule /sub/directory/index\.php - [L] 

See What is matched?

  • In per-directory context (Directory and .htaccess), the Pattern is matched against only a partial path, for example a request of "/app1/index.html" may result in comparison against "app1/index.html" or "index.html" depending on where the RewriteRule is defined.

and "Per-directory Rewrites":

  • The removed prefix always ends with a slash, meaning the matching occurs against a string which never has a leading slash. Therefore, a Pattern with ^/ never matches in per-directory context.

This means, the leading slash prevents the pattern from matching. Changing it to

RewriteRule ^sub/directory/index\.php - [L] 

will fix the problem.


The 500 Internal server error comes from the second rule (in combination with the non-matching first rule).

RewriteRule ^.*$ /sub/directory/index\.php 

This will rewrite any request to /sub/directory/index.php, which in turn will be rewritten again to /sub/directory/index.php, and so on until the rewrite module gives up and shows a "too many redirects" error or similar.

Sign up to request clarification or add additional context in comments.

1 Comment

You were right. That line wasn't matching, causing the issue! Thanks! Perhaps I wasn't clear in my question, but the .htaccess file is actually in the '/sub/directory/' folder. Thus, once I stripped it down to just, RewriteRule ^index\.php - [L] it worked!
0

Still have no idea why my second original .htaccess file didn't work, but the following finally got it working:

 <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_URI} !=/sub/directory/index.php RewriteRule .* /sub/directory/index.php </IfModule> 

However, I'll happily accept any answer that can well explain why my second original file kept giving me a 500 error.

Comments

0

Syntax errors, additionally re-matching the first rule after the second match (not last).

any-request -> /sub/directory/index.php -> /sub/directory/index.php - [L]

<IfModule mod_rewrite.c> RewriteEngine On # RewriteRule /sub/directory/index\.php - [L] RewriteRule ^(.*)$ /sub/directory/index.php [L] </IfModule> 

My other suggestion (index.php is the directory index file and is the first in order when requesting the directory):

<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^sub/directory/(.*)$ /sub/directory/ [R=301,L] </IfModule> 

Comments

0

Another way, you can use this code: (comment second and third line to accept even files and directory in url)

 RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA] 

Comments

0

I faced the same problem and I can fix it somehow (not in a smart way I guess). The problem is because HTML does not change to the root folder.

I can solve mine by use HTML "base tag" (you can the path to adapt with the root directory you want.

You also need to give a condition to allow this change only on the targeted page.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.