Redirect to custom 404-page doesn't work on localhost (Apache) - apache

I have a .htaccess-file that has the directives as listed below. The first three lines are used to provide custom error pages for 403, 404 and 429.
ErrorDocument 404 /404.php
ErrorDocument 403 /403.php
ErrorDocument 429 /429.php
DirectoryIndex maintenance.html index.php index.html index.htm home.html
#Rewrite everything to https
<If "%{HTTP_HOST} != 'localhost'">
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</If>
<Else>
# local configuration
# nothing special here...
</Else>
This works perfectly fine when I publish to my website. When trying to open a non-existing page such as mysite.com/wrongpage.php the custom 404.php page is showed.
But when I try to do the same thing on localhost when entering localhost/wrongpage.php. It simply shows the browsers default 404 message. The 404.php page exists, but isn't used as expected.
I am not very good at how to work with the .htaccess file, but perhaps you can help me understand why it won't work on localhost?

This suggests you've not enabled .htaccess overrides on your local development server. .htaccess files are essentially disabled so the directives are not doing anything.
You need to set the AllowOverride directive in the appropriate <Directory> container in the server config (or <VirtualHost> container). By "appropriate" I mean the specific <Directory> container that targets your document root directory, not the entire server (which should remain AllowOverride None).
For example:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Require all granted
# Enable .htaccess overrides
AllowOverride All
</Directory>
</VirtualHost>
And restart Apache.
Reference:
https://httpd.apache.org/docs/2.4/mod/core.html#allowoverride

Related

How to get mod_rewrite working for requests of images which are located in aliased directories?

I have xampp with apache 2.4.9 on windows 10.
In httpd-vhosts.conf there is a definition of a virtual host with alias:
<VirtualHost *:80>
DocumentRoot "I:/xampp/htdocs/mysite"
ServerName dev.mysite
Alias "/sites/default/files/myimages" "C:/anotherdir/myimages"
<Directory "C:/anotherdir/myimages">
Require all granted
</Directory>
</VirtualHost>
In httpd.conf the LogLevel is set:
LogLevel warn mod_rewrite.c:trace8
The .htaccess located in the virtual host's root directory contains only these lines and nothing else:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^$
RewriteRule .*\.(gif|jpg|png)$ /404.html [L]
</IfModule>
When I type into the browser address bar a request for an image which is on a normal (not aliased) path, e.g. http://dev.mysite/sites/default/files/content/image1.jpg, I get the content of 404.html file on the screen and a few lines in error.log resulting from mod_rewrite. That's expected.
But when I type in the same browser address bar a request for an image which is on the aliased path, e.g. http://dev.mysite/sites/default/files/myimages/image2.jpg, I get the real image and there are no new lines in error.log. That's bad, I'd like to see the 404.html as well.
So how to use mod_rewrite for files on aliased directories?
As you have said you have added a .htaccess inside the virtualhost documentroot, but an alias path is pointing to another path somewhere else out of the scope of that directory.
The key to all these are, different physical paths, and alias is a virtual path inside another path, and you don't even have AllowOverride all in that path, so it should never be used.
So first, do not use .htaccess because you don't need it.
And then , define the rewriterule in virtualhost context and you will see it will work.
<VirtualHost *:80>
DocumentRoot "I:/xampp/htdocs/mysite"
ServerName dev.mysite
Alias "/sites/default/files/myimages" "C:/anotherdir/myimages"
<Directory "C:/anotherdir/myimages">
Require all granted
</Directory>
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^$
RewriteRule \.(gif|jpg|png)$ /404.html [L]
</VirtualHost>

Redirecting all ip to maintenance page except some ips

When releasing new versions of my Symfony project, I need to redirect all external ip addresses to a maintenance.html page (which lives into symfony web directory) using apache.
Here is the virtualhost file configuration I've created for this purpose but:
When I go from an external ip I get 403 Forbidden
When I go from localhost I get the same 403 Forbidden
virtualhost file
<VirtualHost *:80>
ServerName myservername
ServerAlias www.myservername
ServerAdmin webmaster#localhost
DocumentRoot /path/to/my/symfony/project/web
RewriteEngine on
# if request not coming from localhost
RewriteCond %{REMOTE_ADDR} !127.0.0.1
# if request not maintenance.html page
RewriteCond %{REMOTE_URI} !/maintenance.html [NC]
# if request not image
RewriteCond %{REMOTE_URI} !\.(jpe?g?|png|gif|ico) [NC]
# if request not css file
RewriteCond %{REMOTE_URI} !\.(css) [NC]
# Redirect to maintenance.html page
RewriteRule .* /maintenance.html [R=302,L]
<Directory /path/to/my/symfony/project/web>
# Ignore .htaccess files
AllowOverride None
</Directory>
ErrorLog ${APACHE_LOG_DIR}/maintenance-error.log
CustomLog ${APACHE_LOG_DIR}/maintenance-access.log combined
</VirtualHost>
What am I doing wrong?
Technologies versions
apache2 -v 2.4.7 (needs to work on 2.2 too)
mod_rewrite doesn’t know a %{REMOTE_URI} variable. Probably you mean %{REQUEST_URI}.

apache add trailing slash redirects to different ServerName

In apache 2.2.22, I have multiple local servers setup such that localhostN refers to a server which I can access via local.myserver.com (for different "myserver"s). The problem is that one such server is doing a strange redirect when adding the automatic slash (I'm guessing via mod_dir), where accessing:
http://local.myserver.com/noslashdir
sends me to
http://localhost5/noslashdir/
instead of to
http://local.myserver.com/noslashdir/
I have not been able to override this behavior in the .htaccess file. Here are the relevant details:
/noslashdir/.htaccess:
DirectoryIndex index.html
/.htaccess:
SetEnvIf HTTPS on http_proto=s
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^noslashdir/(.*)$ /noslashdir/$1.html [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^noslashdir/(.+)\.html$ http%{http_proto}://%{HTTP_HOST}/noslashdir/$1 [L,R=302]
/etc/httpd/httpd.conf
<VirtualHost 127.0.0.5>
ServerName localhost5
DocumentRoot /var/www/myserver.com/
DirectoryIndex index.php index.html index.htm index.shtml
<Directory "/var/www/myserver.com">
Options Indexes +Includes FollowSymLinks
AllowOverride All
Allow from all
Order allow,deny
</Directory>
ScriptAlias /cgi-bin/ "/var/www/myserver.com/cgi-bin/"
</VirtualHost>
/etc/hosts
127.0.0.5 local.myserver.com localhost5
I have tried RewriteRules, DirectorySlash Off, etc, but nothing has worked so far. All other desired redirects work fine.
Apache is using the ServerName you have set for your VirtualHost when creating “self-referential URLs”.
http://httpd.apache.org/docs/2.2/en/mod/core.html#servername:
“The ServerName directive sets the request scheme, hostname and port that the server uses to identify itself. This is used when creating redirection URLs.”
You should set UseCanonicalName to Off:
“In many situations Apache must construct a self-referential URL -- that is, a URL that refers back to the same server. With UseCanonicalName On Apache will use the hostname and port specified in the ServerName directive to construct the canonical name for the server. This name is used in all self-referential URLs, […]
With UseCanonicalName Off Apache will form self-referential URLs using the hostname and port supplied by the client if any are supplied […]”

apache vhosts ignores rewriterule

I have the following vhosts file:
directoryindex .index.php index.html index.php
options -indexes -multiviews +followsymlinks
<directory /Volumes/dev1/http>
allowoverride all
</directory>
<virtualhost *:80>
documentroot "/Volumes/dev1/http"
rewriteengine on
rewriterule ^.*$ /msg.php [l]
</virtualhost>
There are other virtual hosts, but the one from above is the "catch-all". The problem is that the rewriterule is totally ignored.
Going to site.com gives a 403 error and site.com/test just returns a 404 Not Found page instead of serving the msg.php file.
The server is a OS X 10.8 Server running Apache 2.2.24
it seems that you are in the wrong dir.
For what reason ever Apple left the .conf files for the virtual host under this directory.
/Library/Server/Web/Config/apache2/sites

File not found with VirtualHost and mod_rewrite

I'm bulding a RESTful api based on Tonic.
On my developer machine and our stage server we use virtual hosts.
Tonic uses a .htaccess file to translate the incomming calls to it's dispatcher.php file. This works fine on servers without VirtualHosts enabled.
However if i enable VirtualHosts i get a file not found even thought the path and name to the file is correct.
Here is the VirtualHost setup on my developer machine.
<VirtualHost *:80>
ServerAdmin admin#xxxxxxxxxxxx
ServerAlias *.dev.xxxxx
VirtualDocumentRoot /home/xxxxxxxx/workspace/%1
<Directory /home/xxxxxxxx/workspace/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
And Tonic's .htacces located in a folder called rest in the project root:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !dispatch\.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* dispatch.php [L,QSA]
</IfModule>
A call to http://project.dev.xxxxx/rest/ gives:
Not Found
The requested URL /home/xxxxxxxx/workspace/project/rest/dispatch.php was
not found on this server.
Apache/2.2.22 (Ubuntu) Server at xxxxxxx Port 80
It appears as though you're misusing VirtualDocumentRoot. Try changing it to:
DocumentRoot /home/xxxxxxxx/workspace/project/rest
Also, here's a good explanation on the VirtualDynamicRoot: Dynamically configured mass virtual hosting
Hope that helps.