I'm new to the whole apache configuration and I have a problem to which I can't seem to find a solution on google.
In short I can't hide folders from URL because it looks as if the document root character (^) starts at the beginning my XAMPP installation folder - /opt/lampp/htdocs/batulabe/
Detailed I've installed a fresh XAMPP VM on my mac without configuring much so everything is basically default. This is the current folder layout for the website:
-batulabe (the document root I intend to make, this folder is inside htdocs)
--public
---pages
----*php pages
--private
---*private, inaccessible files
I'm trying to rewrite URL's like:
http://localhost/batulabe/public/pages/varpage.php
to
http://localhost/batulabe/varpage
result I am getting
http://my_ipv4_address/opt/lampp/htdocs/batulabe/public/pages/public/pages/account.php.php
I have created an extra .conf
file called website.conf
and made Apache read it by writing one line that looks like this - Include "/opt/lampp/apache2/conf/website.conf"
. Other than that, these are the only files (.conf) that I've changed:
-httpd.conf
Alias /bitnami/ "/opt/lampp/apache2/htdocs/"
Alias /bitnami "/opt/lampp/apache2/htdocs"
<Directory "/opt/lampp/apache2/htdocs">
Options Indexes FollowSymLinks
LoadModule rewrite_module modules/mod_rewrite.so
AllowOverride All
Order allow,deny
Allow from all
</Directory>
-website.conf (the same extra configuration file I made for Apache to read)
<Directory "/opt/lampp/htdocs/batulabe">
Options -Indexes
Require all granted
ErrorDocument 403 /batulabe/index.php
</Directory>
<Directory "/opt/lampp/htdocs/batulabe/private">
<LimitExcept POST>
Require all denied
</LimitExcept>
</Directory>
To save time on restarting Apache, my rewrites are temporarily written in .htaccess
(it is stored in "/batulabe" the website root folder). Inside looks like so:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ public/pages/$1.php [R=301,L]
</IfModule>
The flag [R=301]
helped me to see why (probably?) none of the previous rewrite combinations have worked so far, which is because ^
results in http://my_ipv4_address/opt/lampp/htdocs/batulabe/
instead of http://my_ipv4_address/batulabe/
or at the very least without the website folder "/batulabe" since everything is default from installation.
Any help is greatly appreciated!
CodePudding user response:
Why do you implement an external redirection using the R=301
flag? That is not what you want, you want an internal rewrite instead. So remove that flag!
Also the RewriteCond
does not really make sense. You test if the requested path points to a physical file in your file system. But that most likely is not the case considering that you want to rewrite the requests to some other folder. Instead you would have to check if the rewritten path points to a file.
In the code snippets you posted there are some discrepancies towards where your actual DOCUMENT_ROOT
folder is defined in your configuration. Your are using different paths in different files: /opt/lampp/apache2/
, /opt/lampp/apache2/htdocs/
, /opt/lampp/htdocs/
... Fix that. It is only one of those.
And last not least you should place that distributed configuration file (".htaccess") in the actual DOCUMENT_ROOT
folder of the http host. Please check that in your configuration. Reason is that you want to move the rule into the http server's host configuration later on (which is what I understand from your phrasing "my rewrites are temporarily written" ...
This is an approach that should point you into the right direction:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/batulabe/public/pages/
RewriteRule ^/?batulabe/(.*)$ /batulabe/public/pages/$1.php [L]
In general you should prefer to place such rules in the actual http server's host configuration instead of using distributed configuration files. One of the reasons is to avoid added complexity and confusion over the different handling of paths.
It certainly is possible to implement rules in a configuration file stored inside a folder further down the hierarchy as you did. That should work:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/pages/
RewriteRule ^(.*)$ public/pages/$1.php [END]
But note that you'd have to change those rules again when moving them to the central configuration. Also you risk more unexpected interactions with other rules you implemented somewhere in the hierarchy. Which is why one should generally avoid such scheme in my experience.