HTACCESS URL rewriting by ignoring existing files and folders

Asked

Viewed 1,524 times

0

When I go to pass a site in the server I put a file .htaccess with this code below:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

This code causes when typing the site:

www.dominio.com.br

Go straight to index.php of the briefcase public of Laravel.

But now a problem is happening. I have a panel that is in a folder painel, out of the folder public of Laravel, which is independent of Laravel.

But when I type:

www.dominio.com.br/panel

Does not enter.

And even if I put the briefcase painel inside the briefcase public, does not enter either. Because of the .htaccess inside the briefcase public.

How can I make this URL independent ?

2 answers

2


This occurred because you did not check whether the folder exists and another detail instead:

RewriteRule ^painel - [L]

Prefer this:

RewriteRule ^painel/ - [L]

Without the bar . htaccess will accept addresses as localhost/painel2 and supposing it is a route it will become inaccessible.

Getting back to the problem, you probably did something similar to this in your . htaccess: /a/91799/3635

First solution

Must be using similar to this:

/home
  |--- /user
         |--- /public_html
                |--- .htaccess (seu .htaccess)
                |--- /public
                        |--- index.php
                        |--- .htaccess
                |--- /app
                |--- /bootstrap
                |--- /config
                |--- /database
                |--- /painel (pasta do painel)

If you want the panel folder here to be accessible the public_html/.htaccess should look like this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ public/ [L]

Second solution

However the best thing is to stay inside public same, then the structure would be so of the folders being this (public_html is a common folder on servers, may have another name like www), see that I moved the location of the panel folder inside public:

/home
  |--- /user
         |--- /public_html
                |--- .htaccess (seu .htaccess)
                |--- /public
                        |--- /painel (pasta do painel)
                        |--- index.php
                        |--- .htaccess
                |--- /app
                |--- /bootstrap
                |--- /config
                |--- /database

So in this case the file public_html/.htaccess must contain:

RewriteEngine On

RewriteRule ^ public/ [L]

And the file public_html/public/.htaccess must contain:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Third solution

But I have to say, you can even use . htaccess to point everything to public, however the best way is to define the DocumentRoot as being the public or release the content of public in public_html and the folders of the windows will be out, something like:

/home
   |--- /user
         |--- /app
         |--- /bootstrap
         |--- /config
         |--- /database
         |--- public_html (conteúdo de public deve ficar nesta pasta)
              |--- /painel (pasta do painel)
              |--- index.php
              |--- .htaccess

1

Bro, this OS is top.

I think there’s some power that hangs around here... there are questions I ask here and soon after, a matter of minutes I solve my problem.

How do you get ?

To leave the URL 'independent' type like this:

RewriteRule ^painel - [L]

Thus remaining:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteRule ^painel - [L]
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Browser other questions tagged

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