Rewrite module in . htaccess for subfolders

Asked

Viewed 875 times

3

I have an application in AngularJs with html5 enabled, which makes me need to use the module rewrite apache so there is a correct URL conversion.

I have no problem using this when accessing the application with index from the root, ie if I access www.meusite.com.br and browse the menus, refresh the page, etc. everything works correctly.

The problem starts when I need to access a new area of app that starts in a subfolder, for example my administrative area. It gets all stored in a subfolder called adm being accessed like this: www.meusite.com.br/adm

In this Adm subfolder, I have a new index, new subfolders, etc. A new area of app focused only on administration and this is the part where I have problems using the module rewrite with html5, because when updating the page it no longer finds the path, in case it tries to start from the root index.

This is my file .htaccess located at the root of app.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
</IfModule>

In theory I know I would need a rule that points to the subfolder as well, but I don’t know how to create this rule and also I haven’t been able to find anything so far.

1 answer

3


You will have to create another .htacess, save it in the folder adm and change the second and last line:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /adm
    RewriteRule ^index.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /adm/index.html [L]
</IfModule>

In this case, the "root" will be pointed to the /adm (second line) and when entering the root, will look for the index.html (last line).

  • but this will not 'overwrite' what was set in the other . htaccess ? Why I need to access the two areas equally. Or this htaccess would be to put inside the sub folder 'Adm' ?

  • This you create inside the subfolder. In this case will not interfere with each other.

  • Still, the first access is ok, but when I refresh the page it goes back to the index located at the root, not in the folder 'Adm'

  • That, I put the one of your answer inside the Adm folder and kept the one of my question in the root folder.

  • I changed the last line, see if it’s right now.

  • 1

    Perfect, thank you very much! I had tried something similar to this, but I don’t understand the correct logic of htaccess files, now it’s become clearer how it works too, vlw!

Show 1 more comment

Browser other questions tagged

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