Reputation: 6792
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
Upvotes: 2
Views: 4808
Reputation: 1
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.
Upvotes: 0
Reputation: 74108
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.
Upvotes: 1
Reputation: 73
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]
Upvotes: 0
Reputation:
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>
Upvotes: 0
Reputation: 6792
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.
Upvotes: 0