skip to Main Content

I’m failing in edit a .htaccess file.
I created a complete website in a php slim framework (let’s call it website S).
My client have a wordpress website (let’s call it website W) that will have links toward my website S.
I copied all the files of my website S inside a directory of the root directory of the website W.

My idea is to change the follow htaccess (created by wordpress) to allow public access to the directory without creating any problem to move inside website S.

First .htaccess

RewriteEngine On
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteCond %{QUERY_STRING} !wc-api [NC]
RewriteCond %{HTTP_HOST} ^website.pe$ [OR]
RewriteCond %{HTTP_HOST} ^www.website.pe
RewriteRule ^(.*)$ https://website.pe/$1 [R=301,L,NE]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Then, I guess I could use inside the directory where the website S would be, the typical htaccess that works properly for it.
Edited I added the structure of the website S. The index.php is inside a public directory there.

Second .htaccess

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::2$
  RewriteRule ^(.*) - [E=BASE:%1]
  
  RewriteBase /
  #RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.php [QSA,L]
</IfModule>

I try different ways but, or get the message
"Forbidden You don’t have permission to access this resource."
Or get inside but the website W stop working.

Any suggestions? Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    Yes MrWhite, I delete in the second .htaccess the RewriteBase / and now I can access a simple index.html in the directory where the slim website is that I used for testing.

    But I can't make the website S to run because inside the website S directory exist a public subdirectory with the index.php, arranged like this:

    website.pe/ (website W wordpress)
    -slimdirectory/ (website S slim)
    --.htacess
    --vendor/
    --public/
    ---css/
    ---fonts/
    ---images/
    ---js/
    ---index.php
    --bootstrap/
    ---app.php
    --etc..
    

    I am new to htaccess and don't understand how one htaccess overwrite the other one, so if I use something like you told me in my second htaccess only like this

    <IfModule mod_rewrite.c>
       # Redirect HTTP to HTTPS and www to non-www
       RewriteCond %{ENV:HTTPS} !on [NC,OR]
       RewriteCond %{HTTP_HOST} ^www. [NC]
       RewriteCond %{HTTP_HOST} ^(?:www.)?(website.pe) [NC]
       RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
    
       RewriteEngine on
       RewriteCond %{REQUEST_URI} !^public
       RewriteRule ^(.*)$ public/$1 [L]
    
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteRule ^ index.php [QSA,L]
    </IfModule>
    

    That should work... but still not


  2. <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::2$
      RewriteRule ^(.*) - [E=BASE:%1]
      
      RewriteBase /
      #RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^ index.php [QSA,L]
    </IfModule>
    

    In the config you’ve posted you would just need to remove the RewriteBase / directive from the second .htaccess file in the "slim" subdirectory. This is causing requests to your slim website to be routed back through the WordPress site in the document root (which would presumably result in a 404).

    My idea is to change the follow htaccess (created by wordpress) to allow public access to the directory without creating any problem to move inside website S

    You shouldn’t need to touch the WordPress .htaccess file in the document root. This already allows you to access website S.

    By default, the .htaccess file (or rather, the mod_rewrite directives) in the slim subdirectory is going to completely override the WordPress .htaccess file in the root.

    Consequently, you’ll need to repeat the HTTP to HTTPS redirect in the slim subdirectory (with a difference*1), before the existing directives. Presumably, the intention is to also redirect www to non-www? Although your current redirect (in the root .htaccess file) is not doing this properly.

    For example, at the top of your second .htaccess file in the slim directory add the following:

    # Redirect HTTP to HTTPS and www to non-www
    RewriteCond %{ENV:HTTPS} !on [NC,OR]
    RewriteCond %{HTTP_HOST} ^www. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www.)?(website.pe) [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
    

    *1 Note the use of the REQUEST_URI variable instead of the $1 backreference as used in the root .htaccess file. This is necessary when used in the subdirectory, otherwise, the subdirectory will be omitted from the redirected URL.

    NB: Test first with a 302 (temporary) redirect to avoid caching issues.


    UPDATE:

    But I can’t make the website S to run because inside the website S directory exist a public subdirectory with the index.php, arranged like this:

    website.pe/ (website W wordpress)
    -slimdirectory/ (website S slim)
    --vendor/
    --public/
    ---css/
    ---fonts/
    ---images/
    ---js/
    ---index.php
    --bootstrap/
    ---app.php
    --etc..
    

    This is a rather important bit of information missing from your initial question. I assume that the public subdirectory is not part of the visible URL, in which case your second .htaccess file in the slim subdirectory (the parent directory of public) should be something like this instead:

    # /slimdirectory/.htaccess
    
    RewriteEngine On
    
    # Redirect HTTP to HTTPS and www to non-www
    RewriteCond %{ENV:HTTPS} !on [NC,OR]
    RewriteCond %{HTTP_HOST} ^www. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www.)?(website.pe) [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
    
    # Calculate base directory
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::2$
    RewriteRule (.*) - [E=BASE:%1]
    
    # Slim front-controller
    RewriteRule ^public/index.php - [L]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . public/index.php [L]
    RewriteRule ^$ public/index.php [L]
    

    The QSA flag is not required.

    You do not need the <IfModule> wrapper.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search