How to overwrite a root HTACCESS in a subfolder with another HTACCESS

Asked

Viewed 364 times

1

I have a file HTACCESS at the root of my project. However, I have a subfolder that also has a file HTACCESS. But I’m not sure how to make this code override part of the HTACCESS root.

HTACCESS root:

...
RewriteEngine On

#RewriteCond %{HTTP_HOST} !^www.
#RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteRule ^(css|js|img)/(.*)$ statics/$1/$2 [L]
...

HTACCESS subfolder:

RewriteEngine On
RewriteRule ^(css|js|img)/(.*)$ test/statics/$1/$2 [L]
  • What is the directory structure? By default htaccess will overwrite the rules in the folder and subfolders it is in, but it does not affect the level above it

  • by e:. i have an htaccess at the root and another inside a folder that is at the root. at the root I have an index.php and inside this folder q is at the root I have another index.php.

  • And what is the name of the subfolder? It is test?

  • No. But it might be. Pq tb changed the name to "Rewriterule (css|js|img)/(.*)$ test/statics/$1/$2 [L]". Just for the example. In real instead of test is the name of the folder.

3 answers

0

You can do it like this:

Structure Example:

  • App > Admin

Within App > . htaccess

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Options -Indexes

Within Admin > . htaccess

#Aqui você escreve novas regras
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php

Options -Indexes
  • Hello Fernando, that’s exactly what I did up in my example and it didn’t work!

  • You can use the technique of writing anything into the . htaccess file to confirm that it is being applied to the site or system. Ex: write TEST in any line of the file and see if it will generate an error if it does not generate your . htaccess is not being applied. This indicates that the server module (apache) may not be enabled so you already have a path to search.

  • I did the test you mentioned and the result was, it’s reading both files, because when I write anything on any of them, I get an internal error from the server. So the two files are being read by the server, but for some reason the second does not overwrite the first in its rules.

0

Usually in the subfolders I use so, add the folder name in Rewritebase:

RewriteEngine On
RewriteBase /nomedapasta/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

And I put it inside the folder in question. In your case it would look like this:

...
RewriteEngine On
RewriteBase /nomedapasta/

#RewriteCond %{HTTP_HOST} !^www.
#RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteRule ^(css|js|img)/(.*)$ statics/$1/$2 [L]
...

0

You can only overwrite rules that are not flags [L]:

The flag QSA, informs that only QUERY_STRING can access them.
The flag NC (nocase), is to ignore case sensitive (high/low box).
The flag L (last) is a flag to indicate that the current rule must be applied immediately, without considering other rules (i.e., it becomes independent and prioritized).
The flag R (redirect) causes an HTTP redirect (when the domain or server is written to the browser URL).

The rules work from the top down. Only when the rule has already been applied will the next rule be read.

What you can do is create a condition before for each case:

RewriteCond %{HTTP_HOST} = seusite.com
...aqui viria a regra sob a condição acima

Browser other questions tagged

You are not signed in. Login or sign up in order to post.