Mod-Rewrite loading files behind the DocumentRoot - .htaccess

I'm using .htaccess and mod_rewrite to point to files that reside behind the DocumentRoot. My folder structure looks like this:
home/
webroot/
other_files/
I have a .htaccess file in webroot with the following content:
RewriteEngine on
RewriteRule ^(.*)$ /home/other_files/$1
If I try to access http://example.com/file.html I receive the following error:
The requested URL /home/other_files/file.html was not found on this server.
Is it even possible to load files that are behind the DocumentRoot? If so, can someone point me in the right direction?

I believe you need to add a section with
<Directory "/home/other_files">
(options)
</Directory>
to your server configuration before apache will be able to serve anything from it. For an example, my DocumentRoot is /var/www but there is this section in the default available site:
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
You could then rewrite a URL to go to /doc/ and the server would know where to get the files from.

Just so you know why that rule doesn't work:
The reason that it isn't able to rewrite to /home/other_files/file.html is that mod_rewrite is parsing the path as /home/webroot/home/other_files/file.html since from mod_rewrite's point of view the preceding slash is equivalent to your document root of /home/webroot.
Ryan Ahearn's suggestion is a decent one, and is likely the route you want to go.

The credit goes to Ryan Aheam, but I'm going to spell it out. I'm a beginner and even with Ryan's answer I had to experiment with a few things to get the syntax right.
I wanted my DocumentRoot to be my cakephp directory. But then I had a Mantis Bug tracker that was just regular PHP and so not in the cakephp directory. The the files below I have the following working.
http://www.example.com : served by /var/www/cakephp
http://www.example.com/mantisbt : served by /var/www/html/mantisbt
File /etc/httpd/conf/httpd.conf
Alias /mantisbt/ "/var/www/html/mantisbt/"
<Directory "/var/www/html/">
AllowOverride All
</Directory>
<VirtualHost *:80>
ServerAdmin me#my_email.com
DocumentRoot /var/www/cakephp
ServerName my_website.com
<Directory /var/www/cakephp/>
AllowOverride All
</Directory>
</VirtualHost>
File /var/www/cakephp/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^mantisbt/?$ /mantisbt/ [NC,L]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>

Related

Getting 404 on a specific path /pricing with Apache server [migrated]

This question was migrated from Stack Overflow because it can be answered on Server Fault.
Migrated 2 days ago.
I've set up an Apache server on Ubuntu 20.04
The site loads fine when I load the home page first (https://leadzilla.ai) and after that when I click on the pricing button and it takes me to https://leadzilla.ai/pricing and the that page loads fine as well.
But when I go directly to https://leadzilla.ai/pricing in the browser, I get a 404
Here is what I have in /etc/apache2/sites-available/leadzilla.ai.conf
<VirtualHost *:80>
DocumentRoot /var/www/leadzilla.ai
ServerName leadzilla.ai
ServerAlias www.leadzilla.ai
<Directory /var/www/leadzilla.ai>
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Allow from all
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =leadzilla.ai [OR]
RewriteCond %{SERVER_NAME} =www.leadzilla.ai
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
I have this config in /etc/apache2/sites-available/leadzilla.ai-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
DocumentRoot /var/www/leadzilla.ai
ServerName leadzilla.ai
ServerAlias www.leadzilla.ai
<Directory /var/www/leadzilla.ai>
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Allow from all
#Deny from all
#Allow from 127.0.0.1
#Allow from ::1
</Directory>
<Directory /var/www/leadzilla.ai/blog>
AllowOverride All
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/leadzilla.ai/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/leadzilla.ai/privkey.pem
</VirtualHost>
</IfModule>
I have Wordpress on https://leadzilla.ai/blog so that has to be taken care of as well.
Here is what I have tried:
RewriteRule ^pricing$ pricing.html [NC]
I added it before the other rewrite rule but that doesn't seem to be working. Any ideas?
[EDIT]
This is solved now. The issue was a Next.js config, not an Apache config.
I put in exportTrailingSlash: true in my module.exports and it worked
I find this to be curious behavior. But if you have a RewriteRule in the <VirtualHost *:443>, then you should also have RewriteEngine On.
Are there any symbolic links in your directory at all. Anything like foo -> foo.html?
Are there any directories like /var/www/html/pricing/ in your directory structure?
Also, remember, that all of your traffic ends up on HTTPS, which means that only the <VirtualHost *:443> is in play. The other virtual host entry only is used long enough to redirect from HTTP to HTTPS. Any rewrite rules for the :80 VirtualHost do not apply on HTTPS.

How to set up host and v-host properly for 2 different folders?

Hi i have zero knowledge about setting up domain and stuff, all i did was search the net. And i think i do not get the correct keyword from my search since i just can't find the correct answer for my problem.
now i need your help guys. please, i want them to work those 2 domains and their respective DocumentRoots
this is my host file setup
127.0.0.1 mydomain.com
127.0.0.1 mydomain.net
this is my vhost file setup
<VirtualHost *:80>
DocumentRoot "/opt/lampp/htdocs/mydomain-com"
ServerName mydomain.com
<Directory "/opt/lampp/htdocs/mydomain-com">
Options All
AllowOverride All
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/opt/lampp/htdocs/mydomain-net"
ServerName mydomain.net
<Directory "/opt/lampp/htdocs/mydomain-net">
Options All
AllowOverride All
</Directory>
</VirtualHost>
but when i go to mydomain.net it goes to mydomain.com it seems only mydomain.com is working
this is my htaccess file for both
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1
I don't know the reason but, when i reinstall xampp and include core files now its working just fine.
I assume the previous developer played around the config files and messed it up. geez.
without proper investigation of the real problem, this really is not the correct answer but i would like to close this since i'm all good now.

Laravel Setup - "AllowOverride all" is causing a 403

Trying to get a test Laravel project running on the server. If I navigate to the sites URL I am presented with the Laravel welcome screen but none of the routes are working.
To fix this I have changed the .htaccess file as specified by the laravel website.
The problem is in order for the .htaccess to be utilised I have to "AllowOverride all" in my virtualhost file which is causing a 403 to be presented instead of the welcome page.
The error I'm getting is:
Forbidden
You don't have permission to access / on this server.
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request
Just for testing purposes I have given the entire laravel folder 777 to make sure there isn't a a file permission issue.
Any help would be greatly appreciated. Thanks
Content of my .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Content of my VirtaulHosts:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/testProject1/public
DirectoryIndex index.php
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<Directory> must refer to an absolute path, whereas <Location> can refer to a path relative to the document root.
As such, change:
<Directory />
to:
<Directory /var/www/testProject1/public>
And don't forget to reboot Apache.

Hiding "index.php" at url in "YII" when using "https" does not work

here i am stuck with a small problem. I have a site built in Yii framework. It runs without any problem with http protocol (the index.php in url is hidden and all urls work fine). I have hosted this in amazon ec2 services. i have the following lines in my .htaccess file
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
but when i use https to browse the site (note: i have it all configured) , the home page loads normally. but the other urls does not work and requires /index.php/ to be added. what am i missing here?
dying for answers.
thanks in advance.
To get mod_rewrite working not only mod_rewrite must be installed, but also check in the Apache directory config (/etc/apache2/sites-enabled/000-default on Ubuntu installed via apt-get) if the rule "AllowOverride None" exists in your project's directory config. If so, change it to "AllowOverride All".
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
**AllowOverride All**
mod_rewrite can be installed by remove comment from this line
#LoadModule rewrite_module modules/mod_rewrite.so
After a long search for the .htaccess configurations, main file configurations, the solution was found in /etc/apache2/sites-enabled directory , in the default-ssl file. (thanks to Hemc's answer)
this is what i did. this may help some others:
first move to the apache's sites-enabled directory
cd /etc/apache2/sites-enabled
then open default-ssl file
sudo vi default-ssl
then change AllowOverride None to AllowOverride All
Actually this is in the top of my file and it works fine .ie. my index.php is properly hidden and urls work fine :)
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
And finally after saving and quitting, dont forget to restart your apache
sudo service apache2 restart
thanks

How to get MAMP to read .htaccess files

I am trying to get the .htaccess working in MAMP. The content of the .htaccess is a simple redirect line, but the entire .htaccess file seems to have no effect, even when I change it to contain invalid data.
Is there any settings within MAMP I need to change to enable .htaccess files?
In httpd.conf on /Applications/MAMP/conf/apache, find:
<Directory />
Options Indexes FollowSymLinks
AllowOverride None
</Directory>
Replace None with All.
Restart MAMP servers.
Go to httpd.conf on /Applications/MAMP/conf/apache and see if the LoadModule rewrite_module modules/mod_rewrite.so line is un-commented (without the # at the beginning)
and change these
from ...
<VirtualHost *:80>
ServerName ...
DocumentRoot /....
</VirtualHost>
To this:
<VirtualHost *:80>
ServerAdmin ...
ServerName ...
DocumentRoot ...
<Directory ...>
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory ...>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
I'm using MAMP (downloaded today) and had this problem also. The issue is with this version of the MAMP stack's default httpd.conf directive around line 370. Look at httpd.conf down at around line 370 and you will find:
<Directory "/Applications/MAMP/bin/mamp">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
You need to change: AllowOverride None
To: AllowOverride All
If you have MAMP PRO you can set up a host like mysite.local, then add some options from the 'Advanced' panel in the main window. Just switch on the options 'Indexes' and 'MultiViews'. 'Includes' and 'FollowSymLinks' should already be checked.
The problem I was having with the rewrite is that some .htaccess files for Codeigniter, etc come with
RewriteBase /
Which doesn't seem to work in MAMP...at least for me.
I have MAMP v 6.6.2 (year 2022), and I was trying to make working php friendly URLs on my localhost Apache, by adding '.htaccess' file to my website root directory ("localhost/mywebsite/.htaccess"):
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]
Which didn't work. But I tested it on cPanel hosting on a real domain and it worked fine.
So I have tried to change httpd.conf with these advises:
https://stackoverflow.com/a/21411933/3936149 - didn't help
https://stackoverflow.com/a/7670598/3936149 - didn't help
https://stackoverflow.com/a/19204983/3936149 - didn't help
In the end I came up to an advise:
https://stackoverflow.com/a/16106140/3936149 - and it worked!
I simply commented out the second line of the code in my '.htaccess' (located at "localhost/mywebsite/.htaccess") file and that's it:
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]
After it started to work I reverted all the changes in httpd.conf file, I have done above, and it was still working.

Resources