Home > Software engineering >  htaccess change subfolder root with subfolder
htaccess change subfolder root with subfolder

Time:08-02

I have a website example.com with this structure:

public_html/
    .htaccess
    index.php
    project/
        .htaccess
        public/
            index.php

the content of public_html/.htaccess is:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

the content of public_html/project/.htaccess is:

RewriteEngine On
RewriteRule !^public/ /public%{REQUEST_URI} [L]

Actual Result

I'm able to see public_html/index.php when url is example.com

I'm not able to see public_html/project/public/index.php when url is example.com/project. Error 404

Expected Result

I want to be able to see public_html/index.php when url is example.com

and i want to see public_html/project/public/index.php when url is example.com/project

Additionl info

If I put the code from public_html/project/.htaccess to the root's htaccess file, it works.

example:

public_html/
    .htaccess
    index.php
    project/
        index.php

[public_html/.htaccess]

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

RewriteRule !^project/ /project%{REQUEST_URI} [L]

If the url is example.com I'm able to see public_html/project/index.php

CodePudding user response:

Content of public_html/.htaccess should be:

DirectoryIndex index.php
RewriteEngine On

RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R,L]

Content of public_html/project/.htaccess should be:

RewriteEngine On

RewriteRule ^$ public/index.php [L]

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

Make sure to test it from a new browser to avoid old cache.

  • Related