0

I have this rule in .htaccess (located in root)

RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

and all is working fine.

Now, I would like to disable this rule only for folder etc. /testfolder. So I would like when make php POST inside /testfolder to disable rules from main .htaccess file.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
pavlenko
  • 625
  • 2
  • 11
  • 30
  • Does this answer your question? [.htaccess mod\_rewrite - how to exclude directory from rewrite rule](https://stackoverflow.com/questions/1848500/htaccess-mod-rewrite-how-to-exclude-directory-from-rewrite-rule) – CBroe Feb 22 '22 at 10:42
  • So you want to add a condition testing whether the request does NOT target that "folder". – arkascha Feb 22 '22 at 11:09
  • @arkascha Hi, yes.. For /testfolder I don't need any condition, but for all other files/folder on server, need to use conditions from .htaccess – pavlenko Feb 22 '22 at 11:16
  • You can implement a negative condition, that is easier. So the request does _not_ match that condition, then apply the following rule. You will find many many existing examples for that here on SO or in the documentation of the rewriting module. – arkascha Feb 22 '22 at 11:35

1 Answers1

0

Htaccess disable for specific folder

In the general case you can't simply "disable htaccess for a specific folder" unless you have access to the server config.

However, you can disable mod_rewrite (which is what you are using here) in a specific folder (and subfolders) by placing a .htaccess in that folder with the following:

# Disable mod_rewrite in this directory and all subdirectories
RewriteEngine Off

This will prevent mod_rewrite directives in the parent config being processed. However, directives from other modules (eg. mod_alias, mod_headers, etc.) will still be processed.

Make an exception for a specific rule

However, you only have a single rule that you don't want to apply to a particular folder. In this case you can create an additional condition (RewriteCond directive) that excludes this folder.

For example:

RewriteCond %{REQUEST_URI} !^/testfolder($|/) [NC]
RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

The ! prefix negates the expression, so it is successful when it does not match. In other words, the first condition is successful when the requested URL is not /testfolder or does not start with /testfolder/.

NB: I removed the NC flag in the RewriteRule directive since it's not required here.

However, since you want this rule to apply to all requests except this one subfolder, it would be marginally more efficient (and 1 less directive) to perform this check directly in the RewriteRule directive. For example:

RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule !^testfolder($|/) %1 [NC,R,L]

So I would like when make php POST inside /testfolder to disable rules from main .htacces file.

The rule above only applies to non-POST requests anyway (as denoted by !POST), so you shouldn't need to make an exception in this rule to begin with, so maybe there is another underlying issue?

However, if you are using extensionless URLs throughout your site (hiding the .php extension) then you shouldn't be submitting forms to the .php version of the URL. So, again, an exception should not be required here.

MrWhite
  • 43,179
  • 8
  • 60
  • 84