0

I am trying to configure my .htaccess file to redirect all URI requests, including folders, to index.php. Here is the structure of my root directory:

  • app
  • admin
  • public
  • index.php
  • .htaccess

My goal is to redirect requests as follows:

www.example.com/anything -> Pass! Successfully redirected to index.php, no HTTP errors. www.example.com/admin -> Not Pass! Displays HTTP error: Forbidden (You don't have permission to access this resource.)

Any other request works fine, only when I try to access a URI that contains a folder with the same name I get http forbidden error

I want to handle all URI requests as normal GET requests to index.php. How can I achieve this?

Here is my current .htaccess configuration:

<IfModule mod_rewrite.c> 
    Options -MultiViews -Indexes +FollowSymLinks 

    # Turn rewriting on 
    RewriteEngine On
    RewriteBase /

    # Redirect http
    RewriteCond %{HTTPS} !=on
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

    # Redirect requests to index.php 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ index.php/$1 [NC,L,QSA] 

    # proc/self/environ? no way!
    RewriteCond %{QUERY_STRING} proc/self/environ [OR]

    # Block out any script trying to set a mosConfig value through the URL
    RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]

    # Block out any script trying to base64_encode crap to send via URL
    RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]

    # Block out any script that includes a <script> tag in URL
    RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]

    # Block out any script trying to set a PHP GLOBALS variable via URL
    RewriteCond %{QUERY_STRING} GLOBALS(=|[|\%[0-9A-Z]{0,2}) [OR]

    # Block out any script trying to modify a _REQUEST variable via URL
    RewriteCond %{QUERY_STRING} _REQUEST(=|[|\%[0-9A-Z]{0,2})

    # Send all blocked request to homepage with 403 Forbidden error!
    RewriteRule ^(.*)$ index.php [F,L]
</IfModule>

UPDATE:

Following the reply from @arkascha. I did remove the -Indexes from Options and remove the directory condition. Although I am getting the following error when I tried to access the URI www.example.com/admin:

example.com redirected you too many times. ERR_TOO_MANY_REDIRECTS

.htaccess configuration revised:

Options -MultiViews +FollowSymLinks 

# Redirect requests to index.php 
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^(.*)$ index.php/$1 [NC,L,QSA] 
1
  • What happens with this code? Commented Jul 6 at 20:04

1 Answer 1

1

Your implementation is most likely itself responsible for the result you receive.

Let's take a closer look at your implementation:

# Redirect requests to index.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/$1 [NC,L,QSA] 

The second condition explicitly takes care to not apply that rule when the requested URL is mapped onto a folder in the server side file system. So any request that is mapped to a folder will not get rewritten to your index.phprouter file.

That means the http server tries to access the targeted resource in the file system, here the folder. That probably is where the 403 is raised. Probably because you explicitly prevented the generation of folder indexes:

Options -MultiViews -Indexes +FollowSymLinks 

That leaves the http server without an option.

5
  • So you suggested to remove -Indexes from Options and also remove the directory condition? I will try it Commented Jul 6 at 21:06
  • UPDATE: removed the -Indexes and removed the directory condition. Now, when I try to access www.example.com/admin, I get the following error: example.com redirected you too many times. ERR_TOO_MANY_REDIRECTS Commented Jul 6 at 21:12
  • You write at the start of your question: "I am trying to configure my .htaccess file to redirect all URI requests, including folders, to index.php". I understand that so that requests to /admin should get rewritten to /index.php too. But apparently that is not what you want? Maybe you want requests to paths that point to actually existing files not to get rewritten? I don't know. You never wrote anything along those lines. You will have to be more precise in that ...
    – arkascha
    Commented Jul 7 at 7:56
  • 1
    The endless rewriting loop probably comes from the first rewriting to /index.php/$1: without the condition you removed a request to /admin will get rewritten to /index.php/admin. Then, in the next rewriting loop /index.php/admin will get rewritten to /index.php/index.php/admin and so on. I wonder what you are trying to do with that rule anyway ...
    – arkascha
    Commented Jul 7 at 8:02
  • Yes thats correct, I want to rewrite all URI requests to index.php, this includes the /admin. Although in my root directory there is a folder “admin”, and for some reason is not getting rewritten to index.php. I am getting an infinite loop when try to access the URI /admin. My goal is to configure the .htaccess to ignore all folders/files and be able to access the URI as a normal GET request in index.php. Hope my question its clear. Anyways, I appreciate your explanations, i am almost there 🤞 Commented Jul 7 at 9:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.