Apache error 404 and FilesMatch - regex

our server configuration is following:
ErrorDocument 404 http://www.domain.com/somepage
<FilesMatch "\.(gif|jpe?g|png|s?html|css|js)$">
ErrorDocument 404 default
</FilesMatch>
All "not found" pages are redirected to /somepage. If there is "not found" request for image or css or js we redirect request to standard 404 page. This works fine if image is missing in directory that exists eg
If requested file is http://www.domain.com/images/aaa/test.jgp
and exists /document_root/images/aaa/ then there is no problem and everything works as expected. Problem appears when aaa/ dir not exist .. then FilesMatch "\.(gif|jpe?g|png|s?html|css|js)$" doesnt work (is not matched) and request is send to http://www.domain.com/somepage .
Is there any way how to match not existing files (gif, jpg, css ..) in not existing directories?
Thanks

I got around this by adding the following
<LocationMatch ".+\.(jpg|png|gif)$">
ErrorDocument 404 /images/404Image.png
</LocationMatch>
Adding your extensions to the location match regex should work for you as well.

Related

Apache Location Reverseproxy URL not working

I'm trying to match a URL with multiple params and use reverse proxy to load some other content, for some reason the Location Tag isn't working.
Following is my httpd.conf file
<Location "/tracker/">
ProxyPass "http://website.com:8290/track.js"
ProxyPassReverse "http://website.com:8290/track.js"
Order allow,deny
Allow from all
</Location>
<Location "/tracker/csc-event?p=([^\s]+)&s=([^\s]+)&v=([^\s]+)&e=([^\s]+)&c=([^\s]+)&n=([^\s]+)&f=([^\s]+)&l=([^\s]+)&i=([^\s]+)&j=([^\s]+)&k=([^\s]+)&w=([^\s]+)&h=([^\s]+)&t=([^\s]+)&x=([^\s]+)">
ProxyPass "http://website.com:8290/csc-event?p=([^\s]+)&s=([^\s]+)&v=([^\s]+)&e=([^\s]+)&c=([^\s]+)&n=([^\s]+)&f=([^\s]+)&l=([^\s]+)&i=([^\s]+)&j=([^\s]+)&k=([^\s]+)&w=([^\s]+)&h=([^\s]+)&t=([^\s]+)&x=([^\s]+)"
ProxyPassReverse "http://website.com:8290/csc-event?p=([^\s]+)&s=([^\s]+)&v=([^\s]+)&e=([^\s]+)&c=([^\s]+)&n=([^\s]+)&f=([^\s]+)&l=([^\s]+)&i=([^\s]+)&j=([^\s]+)&k=([^\s]+)&w=([^\s]+)&h=([^\s]+)&t=([^\s]+)&x=([^\s]+)"
Order allow,deny
Allow from all
</Location>
When I hit: https://website.com/tracker it works fine
But not with the other location, for eg If I hit:
https://website.com/tracker/csc-event?p=0%3Ajodv6q8w%3AdO9XGf6T4SCq7kz49hEEUwNvmkqR8Wxw&s=0%3Ajodv6q8w%3AHgkFFmKSZqdBB6E2C~F8xBKTHv8CGwbl&v=0%3AJ1gADq10d3pj1JL1lh4Dd7ZlLxQyl2~_&e=0%3AJ1gADq10d3pj1JL1lh4Dd7ZlLxQyl2~_0&c=jodvqubo&n=f&f=f&l=https%3A%2F%2Fwebsite.com%2F&i=11y&j=k8&k=1&w=mj&h=i9&t=pageView&x=-e1v043
It shows page not found error. What am I doing wrong? Is there some problem with regex or some other config problem?
If you want to use regex in a location directive you should add a tilde (~), but even then Location does not match the query string. Add /tracker/csc-event as a Location and check the query string inside that directive.
Also, proxypass preverses the query string, no need to add it.

How to extract parameter value in Apache httpd.conf in Location tag?

I'm looking to access the parameters of a URL in the <Location> tag in Apache's httpd.conf file
The actual URL looks like this: https://website.com:1111/track?p=admin&s=875
I have the following code and 2 parameters in httpd.conf file
<Location "/tracker/track?p=&s=">
ProxyPass "http://website.com:1111/track?p=&s="
ProxyPassReverse "http://website.com:1111/track?p=&s="
Order allow,deny
Allow from all
</Location>
I'm not sure what to put in front of the = so that any value that comes in, gets passed on. Will a regex work here?

RedirectMatch 301 in urls.py for Django

Since Django does not do .htaccess (at least on my host), I need to do a 301 RedirectMatch in the similar manner that you'd do an .htaccess RedirectMatch:
RedirectMatch 301 ^/oldfolder/(.*)$ http://sub.domain.com/newfolder/$1
The wildcard does not work in the same manner that it does for the .htaccess, so any help appreciated! It must be a permanent redirect too so that search indexes will know it got moved. What am I redirecting is images to my new CDN subdomain.
Not tested:
# Django >= 1.3
from Django.views.generic.base import RedirectView
urls = patterns('',
# some patterns here...
url(
r"^oldfolder/(?P<whatever>.*)$",
RedirectView.as_view(
url="http://sub.domain.com/newfolder/%(whatever)s",
)
),
# more patterns here
)
[edit] Re-reading your question: "What am I redirecting is images to my new CDN subdomain" - if that's about static medias (images, css etc) then they shouldn't be served by Django but by your front web server (Apache, Nginx, whatever) so assuming Apache you can use a simple RedirectMatch in your vhost config.

RedirectMatch in mod_rewrite doesn't work as expected

I'm driving crazy about this, but I'm sure it's only a small thing..
All I'm trying to do is redirecting https server traffic to another website in my virtual hosts config file:
<VirtualHost 127.0.0.1:443>
ServerName somewebsite.ch
RewriteEngine on
RedirectMatch 301 ^client$ https://someotherwebsite.com/client
RedirectMatch 301 ^/client$ https://someotherwebsite.com/client
</VirtualHost>
However, this doesn't seem to work, it looks like the page tries to load the content of the other website, but the Adress Field in the browser still shows the old URL, and the browser body is empty..
Thanks for your help.
Have you tried adding a slash at the end? Your config won't match, for example, /client/
RedirectMatch 301 ^/client/$ https://someotherwebsite.com/client
If the URL continues after client, you could do this:
RedirectMatch 301 ^/client(.*)$ https://someotherwebsite.com/client$1

How do I redirect domain.com to WWW.domain.com under Django?

How do I go about redirecting all requests for domain.com/... to www.domain.com/... with a 301 in a django site?
Obviously this can't be done in urls.py because you only get the path part of the URL in there.
I can't use mod rewrite in .htaccess, because .htaccess files do nothing under Django (I think).
I'm guessing something in middleware or apache conf?
I'm running Django on a Linux server with Plesk, using mod WSGI
The WebFaction discussion someone pointed out is correct as far as the configuration, you just have to apply it yourself rather than through a control panel.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Put in .htaccess file, or in main Apache configuration in appropriate context. If inside of a VirtualHost in main Apache configuration, your would have ServerName be www.example.com and ServerAlias be example.com to ensure that virtual host handled both requests.
If you don't have access to any Apache configuration, if need be, it can be done using a WSGI wrapper around the Django WSGI application entry point. Something like:
import django.core.handlers.wsgi
_application = django.core.handlers.wsgi.WSGIHandler()
def application(environ, start_response):
if environ['HTTP_HOST'] != 'www.example.com':
start_response('301 Redirect', [('Location', 'http://www.example.com/'),])
return []
return _application(environ, start_response)
Fixing this up to include the URL within the site and dealing with https is left as an exercise for the reader. :-)
The PREPEND_WWW setting does just that.
There is a lightweight way to do that involving VirtualHosts and mod_alias Redirect directive. You can define two VirtualHosts, one holding the redirect and another holding the site configuration:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
# real site configuration
</VirtualHost>
And that will do the job.
This also can be done with a middleware.
Some examples:
http://eikke.com/django-domain-redirect-middleware/
https://djangosnippets.org/snippets/510/
https://djangosnippets.org/snippets/434/
This is a better version of snippet-510:
class UrlRedirectMiddleware(object):
"""
This middleware lets you match a specific url and redirect the request to a
new url. You keep a tuple of (regex pattern, redirect) tuples on your site
settings, example:
URL_REDIRECTS = (
(r'(https?)://(www\.)?sample\.com/(.*)$', r'\1://example.com/\3'),
)
"""
def process_request(self, request):
full_url = request.build_absolute_uri()
for url_pattern, redirect in settings.URL_REDIRECTS:
match = re.match(url_pattern, full_url)
if match:
return HttpResponsePermanentRedirect(match.expand(redirect))