Not able to fix .htaccess for Mezzio

Hello Everyone, I am currently trying to use .htaccess file for one of my Mezzio project, but unfortunately not working. However there is another project that runs on Php Slim framework and the .htaccess is working there. So I am assuming that the server has nothing to with this. I figured out to use $basePath = $request->getAttribute(\Blast\BaseUrl\BaseUrlMiddleware::BASE_PATH); for RedirectResponse from handler.

My current structure is

  • htdocs
    • Mezzio
      • .htaccess
      • public
        • .htaccess

the .htaccess inside mezzio

RewriteEngine On
RewriteRule (.*) ./public/$1

`.htaccess’ inside public

RewriteEngine On
# The following rule allows authentication to work with fast-cgi
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

Any help will be highly appreciated

Why are you using the .htaccess files?

My suggestion: Set up your server / hosting package so that your domain will ends in public. (Hence the name “public”). Then no extra middleware is needed.

Thanks for the reply @froschdesign what i am trying to understand is my .htaccess file is being detected as writing some garbage inside .htaccess file is throwing 500 internal server error.
so i have to tweak the httpd.conf file for this specific project named mezzio and point to the public folder is this what you are saying. can you please elaborate a little more into this
Thanks in advance

I’m sorry I mean why are you using two .htaccess files? Only the .htaccess file in the public folder is relevant.

@froschdesign i removed the .htaccess file outside public folder now i am getting
Forbidden - by rule. You do not have permission to access /demo-app/ on this server.

Update the DocumentRoot of your virtual host for this application and use the public folder as target.

@froschdesign i am still not convinced by the fact that i need to edit the config files that is already working for all other projects to get this working.
All i want is to omit the public/index.php from my url’s . i looked into this https://docs.mezzio.dev/mezzio/v3/cookbook/using-a-base-path/ but i am not running it from subdirectory .

I’m sorry, but I don’t know your setup, your directory structure or any of your other applications.
/demo-app/ is not included in your first post and I don’t know where it comes from. And if I understand your first post correctly then your are running the Mezzio from a subdirectory, it’s public.

Please keep in mind: No one here is a magician or wizard and we can not see your desktop. :wink:

i am sorry i started with mezzio as the application name and then named it demo-app . it resides inside htdocs only. the directory structure is the same as the first post and htdocs is the root so i am hoping that its not from subdirectory.
@froschdesign thanks a lot for the quick reply