Search code examples
macosapache.htaccessxampphttpd.conf

working .htaccess configs don't work on httpd.conf


An early heads up - I'm a beginner student with Back-end programming and for now, even .htaccess URL rewrites was a huge pain to implement.

I have XAMPP Apache installed on my Mac (not XAMPP-VM) with a website folder called "Project" inside "/htdocs". So basically a website that I'm practicing with URL looks like this - "localhost/Project"

There was one .htaccess file in my "root" ("root" is the "/Project" folder) folder and another one inside a "PHP" folder (i.e. root/PHP/.htaccess). Root's .htaccess had the following configs:

Options -Indexes

ErrorDocument 403 /Project/index.php

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_URI} !(.*)Pages 
        RewriteRule ^(.*)$ Pages/$1.php [L,NC]
</IfModule>

Whilst root/PHP's .htaccess had this:

Deny from all

Everything worked and after reading a bit more about .htaccess best practices I wanted to move all of the above configs to httpd.conf, specifically the one located inside "/Applications/XAMPP/xamppfiles/apache2/conf". I moved the code to that httpd (properly?), commented out everything inside the previously mentioned .htaccess files, and here's how the httpd now looks like inside:

Alias /bitnami/ "/Applications/XAMPP/xamppfiles/apache2/htdocs/"
Alias /bitnami "/Applications/XAMPP/xamppfiles/apache2/htdocs"

<Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

<Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs/Project">
    Options -Indexes

    ErrorDocument 403 /Project/index.php
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_URI} !(.*)Pages 
        RewriteRule ^/(.*)$ /Pages/$1.php [L,NC]
    </IfModule>
</Directory>
    
<Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs/Project/PHP">
    Deny from all
</Directory>

And it doesn't work. I've tried to google a solution for a while and so far completely nothing. Just in case, I'll also mention that the goal for this "CMS" project is to "write once, install anywhere".

[EDIT] With some clarifications from @MrWhite, this is what the configs look like in xamppfiles. Also, also, Options -Indexes and /Project/PHP > Require all denied don't work as I can browse folders and access "PHP" folder from Browser. And it did not work prior to this EDIT as well.

-xamppfiles/apache2/conf/httpd.conf

Alias /bitnami/ "/Applications/XAMPP/xamppfiles/apache2/htdocs/"
Alias /bitnami "/Applications/XAMPP/xamppfiles/apache2/htdocs"

<Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Include "/Applications/XAMPP/xamppfiles/apache2/conf/httpd.conf"

-xamppfiles/apache2/conf/project.conf

<VirtualHost *:80>
    DocumentRoot "/Applications/XAMPP/xamppfiles/apache2/htdocs/Project">
    
    Options -Indexes
        
    ErrorDocument 403 /Project/index.php
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_URI} !(.*)Pages 
        RewriteRule ^(.*)$ Pages/$1.php [L,NC]
    </IfModule>
    
    <Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs/Project/PHP">
        Require all denied
    </Directory>
</VirtualHost>

I'd greatly appreciate any help.


Solution

  • I have found the issue, which was my blind mistake.

    <Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs/Project">
        Options -Indexes
        //...
    

    I was pointing to the wrong htdocs folder that contains my Project and the correct way was:

    <Directory "/Applications/XAMPP/xamppfiles/htdocs/Project">
    

    Hope this saves some time for anyone else that would come across this.