1

I have a .htaccess file in place to redirect all requests that does not hit an existing file or directory to be parsed by my index.php file like this:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /

  # Our app bootstrap file is index.php
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

This is working fine, But I'd like also to add another rule to prevent any direct access to php files under a certain directory, routing those calls to the same index.php file.

Like so:

request for example.com/something.php -> ok

request for example.com/themes/(anythinggoeshere)/somefile.php -> not ok, route trough index.php

request for example.com/themes/(anythinggoeshere)/banner.png -> ok

I'm looking for a way to make those "rules" work to everything under the "themes" folder without breaking my current ones.

Thanks!

1 Answer 1

1

You can have a new rule to handle those .php requests:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /

  # handle .php requests via index.php
  RewriteCond %{REQUEST_URI} !^/themes/ [NC]
  RewriteRule ^[^/]+/.+?\.php$ index.php?/$1 [L,NC]

  # Our app bootstrap file is index.php
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! Made a few tweaks here and there to fit other needs, but I got the idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.