How to define a subfolder as the root of an HTACCESS application?

Asked

Viewed 962 times

1

I have a certain PHP application written with the Silex framework. I set up the structure in a way where only the folder public would be the folder where I would point the Apache to read it.

My structure is more or less this:

projeto/
    app/
    resources/
    public/
        css/
        js/
        img/
        index.php
    vendor/

So, on my Apache Virtualhost, I would have to point to projeto/public, because this would be the root of my site.

However the blessed of my server is Shared host, so I can not make appointment at all to folder public.

On this Shared host server, the reading is done from the folder public_html, and there’s no chance of me putting the folder public.

I intend to use the folder public_html to be the root by adding the files that are inside my folder projeto (local development).

public_html/
    app/
    resources/
    public/
        css/
        js/
        img/
        index.php
    vendor/

I intend to use the structure. I know there is a way to direct everything that is accessed at the root to public/index.php

I’m doing like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ public/index.php [L]

I didn’t use !-d purposely, because I don’t want anyone to access app and look at the directory. I also have a route system within index.php who could read app/ as an application path (and not as a folder).

The problem is that if I access app/routes.php for example, an error page will appear, because this file should not be accessed, but only the folder public.

I want to know if in .htaccess there is some way to redirect everything absolutely to a specific folder.

I want my .htaccess make the apache recognize public_html/public as a root, instead of public_html.

Because I want access to public/css/default.css be rewritten to css/default.css. And I don’t want other folders like vendor, resources and app is accessible.

I mean, I want you to always public_html/public is accessed as root.

You can do it?

I tried to make RewriteRule ^ public/index.php [L] without the RewriteCond {REQUEST_FILENAME} !-f and ended up giving an internal error (due to recursion generated).

  • I think I already asked that question...

1 answer

-2

Although you believe it would be easier to perform through the dashboard of your hosting plan, you could try using the following htacess.

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,R=301]

This will redirect all requests to the public folder, but honestly not the best alternative.

  • It didn’t work. It redirected instead of rewriting.

  • I’ll see if my panel has the option you mentioned

Browser other questions tagged

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