request matching RewriteRule returns permission denied - apache

I am experiencing a bizarre apache error. I'd like to rewrite all requests from /api/media/fi/le/path.jpg to /media/fi/le/path.jpg
This are rewrite rules setup in virtual host:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /api/v1/* /src/api/v1/index.php [L]
RewriteRule /api/media/(.*) /media/$1 [L]
If I point the browser to /media/fi/le/path.jpg, the image gets server successfully. If I go to /api/media/fi/le/path.jpg it complains that I do not have permission to access the resource. I must have permission, because I can access the file if no rewrites are done. How should I rewrite the last RewriteRule so that it would serve the images?
I added logging directive LogLevel alert rewrite:trace6 to see what the apache is doing under the covers. This are the results:
init rewrite engine with requested uri /api/media/32/ee/0e60731bcb220c7c7b.jpg
applying pattern '/api/v1/*' to uri '/api/media/32/ee/0e60731bcb220c7c7b.jpg'
applying pattern '/api/media/(.*)' to uri '/api/media/32/ee/0e60731bcb220c7c7b.jpg'
rewrite '/api/media/32/ee/0e60731bcb220c7c7b.jpg' -> '/media/32/ee/0e60731bcb220c7c7b.jpg'
local path result: /media/32/ee/0e60731bcb220c7c7b.jpg
go-ahead with /media/32/ee/0e60731bcb220c7c7b.jpg [OK]
I can visit the url mydomain.com/media/32/ee/0e60731bcb220c7c7b.jpg manually and the image gets server with no problem. What am I missing?
Complete .conf file for virtual host:
<IfModule mod_ssl.c>
<VirtualHost *:443>
DocumentRoot /home/user/project/src
ServerName project.com
ErrorLog logs/project-error_log
CustomLog logs/project-access_log common
DirectoryIndex index.php
<Directory /home/user/project/src>
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>
LogLevel alert rewrite:trace6
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /api/v1/* /api/v1/index.php
RewriteRule /api/media/(.*) /media/$1 [L]
SSLCertificateFile /etc/letsencrypt/live/project.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/project.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateChainFile /etc/letsencrypt/live/project.com/chain.pem
</VirtualHost>
</IfModule>

There is weird behaviour with rewrite engine.
RewriteRule /api/media/(.*) /media/$1 [L]
Will always return forbidden. After mv media images and changing the above directive to RewriteRule /api/media/(.*) /images/$1 [L], permission error is gone.

Related

laravel time out error

i have a project running in ubuntu-apache2 with laravel 5.3. The problem is that laravel works on some users but on others it doesn't. When you make the url request the page stays on hold, and you can´t see anything. It's strange because i have users that are using the app normally.
I saw the apache log and i can see the 302 request but not the answer. I change in site-avaible conf the document root to the root path and a I can see the indexes, so i think the problem is in the .htacces file, although I have not made any changes. This is my .conf ->
ServerAdmin serveradmin#serveradmin.com
ServerName localhost
ServerAlias theurlofmypage
DocumentRoot /var/www/html/extranet/public
<Directory "/var/www/html/extranet/public">
AllowOverride All
Options FollowSymLinks Indexes
Order allow,deny
Allow from all
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
And this is the .htacces file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
Can anyone help me??
A 302 is most likely a Laravel response, not an apache thing seeing you redirect with a 301 as far as I can tell.
I had an instance where this drove me nuts too. It happend because I had putten a redirect() somewhere in the code and some requests triggerd it. It redirected back to itself to a part with a wrong namespace which didn't fly.
All I can recommend is temporarily disable middleware and Requests (validation). If that doesn't work try to find all your abort's and redirect()'s.

Apache causes 403 forbidden even when using DocumentRoot that is working with other domain

wget http://maydomain.example
--2017-04-06 18:21:12-- https://mydomain.example
Resolving mydomain.example (mydomain.example)... IP
Connecting to mydomain.example (mydomain.example)|IP|:443... connected.
HTTP request sent, awaiting response... 403 Forbidden
2017-04-06 18:21:12 ERROR 403: Forbidden.
But other domain (at http://OTHER.example) is working, so I changed DocumentRoot to OTHER and (after restart apache2) the error was the same!
<VirtualHost *:80>
ServerAdmin suporte#mydomain.example
ServerName mydomain.example
ServerAlias www.mydomain.example
DocumentRoot /var/www/html/OTHER.example
# DocumentRoot /var/www/html/mydomain.example
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
#?? Require all granted
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.oficial.news [OR]
RewriteCond %{SERVER_NAME} =oficial.news
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>
The files /etc/apache2/sites-available/mydomain.example.conf and /etc/apache2/sites-available/OTHER.example.conf diffs only by filenames and the domain names into its confs.
Notes
May be... rewrite or .access at parent folder?
more /var/www/html/.htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
It was the configuration of the virtual host... See the Rewrite* lines, I removed all,
# REMOVED THIS:
# RewriteEngine on
# RewriteCond %{SERVER_NAME} =www.oficial.news [OR]
# RewriteCond %{SERVER_NAME} =oficial.news
# RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
now is working (!).
Thanks #Nomad and #MatĂ­asNavarroCarter for clues and checklists.
I notice that you're using some Debian derivative (maybe Ubuntu 16.04) to work for your dev enviroment. I've done this many times.
What causes your error is probably that the var/www/html folder is "rooted". That means, root owns it, and Apache (www-data user) doesn't have the enough permissions to access that content. There are several solutions to this. You can choose what fits your needs the most. I'm going to post my preferred one first:
Work with Virtual Hosts: Working in localhost may be good for some. I prefer to use virtualhosts ended in .dev. Then, I point the document root to a folders project I have, more accessible than the localhost. I created a script to easily do all that.
Change the permissions of /var/www/html: Chown that folder to be of the www-data user and www-group, and chmod it 777. Warning: make sure when you upload it to your production server that permissions are ok.
Change Apache user to root: Of all these, this is my least recommended, but it works.

Deploy a CakePHP website fails. An apache2 config issue?

I'm deploying a CakePHP (1.3.2) website for the first time. It was hosted on an older server. I received the project as a zip file. I managed to install it on my localhost and made the changes I needed.
Now I have to deploy it to a new server, but I face a problem.
The routing doesn't seem to work. I guess it's an .htaccess issue.
When I access the root folder, it redirects me to /login but then I have a 404:
The requested URL /login was not found on this server.
My 3 main .htaccess files (/, /app and /app/webroot) are the following. (CakePHP is installed at the root of my virtual host)
Root
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
/app
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
/app/webroot
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
mod_rewrite seems to be activated on my server as it responds with this when I try to add it:
Module rewrite already enabled
But when I try something simple like that on top of my root .htaccess, it doesn't do anything:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^.*$ test.php
</IfModule>
(I was following this guide: https://docs.bolt.cm/howto/making-sure-htaccess-works)
Among a lot of things, I also tried to add that to all my .htaccess:
Option Indexes
But it didn't help.
Here is my website conf file too:
<VirtualHost xx.x.xx.xx:80>
ServerAdmin xxx#company.com
ServerName xxx.company.com
DocumentRoot /var/www/xxx.company.com
DirectoryIndex index.html index.php
php_value error_log "/var/log/apache2/xxx.company.com-phperror.log"
php_flag register_globals off
<Directory "/var/www/xxx.company.com">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/xxx.company.com-error.log
CustomLog /var/log/apache2/xxx.company.com-access.log common
</VirtualHost>
(I added the Directory section that was not here in the first place)
After a lot of trials, I still haven't found anything that seems to solve my problem.
As I'm definitely not used to work on server side, It might be a simple thing that you will immediately spot. I hope so.
Thanks
I finally got it to work. Here are the two mistakes I made:
I had my document root set cake's root folder instead of the webroot folder. I added /app/webroot after DocumentRoot /var/www/xxx.company.com in my .conf file.
Also, I was using apache's reload function, which is actually not properly reloading. Using service apache2 restart instead does the job.

Apache mod_rewrite not happening on localhost

I can't seem to get my head around this. I just pulled down my website files from my server to work on them offline. However, my rewrite conditions are no longer working as expected.
I've been googleing for the last 3 hours and keep coming to these solutions:
put garbage in the .htaccess file to make sure it's being read. I did that and got a 500 error so it is.
Make sure mod_rewrite is enabled, and I make sure it was listed in php_info(). That's not the problem.
Other than that, I can't figure this thing out.
Here's my .htaccess. All I want to do is remove index.php from my URLs:
# Rewrite url no index.php
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule . index.php
Here's my virtual host config at the moment:
<VirtualHost *:80>
ServerAdmin xxxx#gmail.com
DocumentRoot "c:/wamp/www/myapp/public_html/"
<Directory "c:/wamp/www/myapp/public_html/">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ServerName myapp.local
ErrorLog "logs/myapp.local"
CustomLog "logs/myapp.local" common
</VirtualHost>
I'd also like to make it known that rewrite rules seem to work a bit when they're in the virtual host config. So something like this:
<VirtualHost *:80>
ServerAdmin xxx#gmail.com
DocumentRoot "c:/wamp/www/myapp/public_html/"
<Directory "c:/wamp/www/myapp/public_html/">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# ...other settings...
</Directory>
ServerName myapp.local
ErrorLog "logs/myapp.local"
CustomLog "logs/myapp.local" common
</VirtualHost>
I had other problems with this though since I have nested applications (one's at / and the other at /app2/). I also seemed to have problems with the !-f condition being ignored and it would rewrite the URLs of my images and css.
Does anyone have any ideas on how to fix this? Thanks in advance!
You should use your code like this to remove index.php from the URL.
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /index\.php(.*)\ [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [L,QSA]
And then in your head section of your html add this to fix CSS problem.
<base href="http://myapp.local" />

mod_rewrite: multiple domains on one server, mod_rewrite refuses to work on one

I have a server that serves several domains from a single IP address using Apache's Virtual Host shenanigans. Three of the sites are required to redirect to www if it's omitted from the URL.
I have the following rule in the .htaccess file of each domain:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This works for two of the three, but the third completely fails to comply. I know that the .htaccess is being hit because the framework requires all hits to be routed through index.php... and that is happening correctly. So, it's not permissions, and the .htaccess is identical (more or less) on each domain. I even looked into caching (even though that doesn't make any sense... desperation gives way to insanity!)
Please help me if you have any clue what is going on.
As requested, here's the complete vhost config, and .htaccess file...
vhost configuration:
<VirtualHost *:80>
ServerAdmin me#gmail.com
DocumentRoot /var/www/example.com
ServerName www.example.com
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/example.com>
Options FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
.htaccess file:
# BEGIN example.com
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [S=40]
####################################################
# If requested URL-path plus ".php" exists as a file
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# Rewrite to append ".php" to extensionless URL-path
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]
####################################################
# redirect to RoutingHandler
RewriteRule ^.*$ /index.php [NC,L]
</IfModule>
# END example.com
Bear in mind, there are two other domains set up in an identical manner... and they both work with zero issues.
I know this is an old question, but google brought me here while looking for answers on the same problem.
After searching through apache's documentation I found:
AllowOverride Directive
When this directive is set to None, then .htaccess files are completely ignored. In this case, the server will not even attempt to read .htaccess files in the filesystem.
So I changed "AllowOverride None" to "AllowOverride All" in the <Directory /var/www/example.com> section and it worked.