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.
2
Answers
I have found the issue, which was my blind mistake.
I was pointing to the wrong
htdocs
folder that contains my Project and the correct way was:Hope this saves some time for anyone else that would come across this.
You shouldn’t have changed anything here. You are moving the directives from
.htaccess
to a<Directory>
section (in the server config). These are both directory contexts and work the same way. (You are incorrectly thinking this is a server or virtualhost context, although the target URL-path would still be wrong.)By introducing a slash at the start of the URL-path in the
RewriteRule
pattern (first argument) the rule will never match. If it did… the additional slash you’ve introduced at the start of the substitution string (second argument) would incorrectly rewrite the request to/Pages
in the document root, not/Project/Pages
as would seem to be the intention.Note that if you are moving the
.htaccess
config to<Directory>
containers in the server config then you should probably disable.htaccess
overrides altogether, since a.htaccess
file that contains mod_rewrite directives will completely override the<Directory>
container in the server config. For example:Note also that
Order
,Allow
andDeny
are Apache 2.2 directives and formerly deprecated on Apache 2.4. On 2.4 you should be using the equivalentRequire
directive instead. For example:Extra:
Look into creating additional
<VirtualHost>
containers for each project, store these in separate files andinclude
them in the main server config, rather than modifying the main server config directly. This will allow you to host multiple (separate) websites on the same webserver.