Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am setting up a simple test page in Python. I only have two files: .htaccess and (Permissions for both set to 755) I get a 403 Forbidden error when trying to view the page - how can I fix this?


RewriteEngine On
AddHandler application/x-httpd-cgi .py

print "Content-type: text/html\n\n"
print "test"
share|improve this question
up vote 1 down vote accepted

Have you checked your error log? It's the first place to look.

I think your AddHandler is incorrect. The documentation says it is used like so:

Syntax: AddHandler handler-name extension [extension] ...

application/x-httpd-cgi isn't a handler, it's a MIME type.

You probably need ExecCGI on (see the docs). This is usually enabled for /cgi-bin/ but if your file's not in there you'll need something like this in your .htaccess

Options +ExecCGI
AddHandler cgi-script .py

This relies on your server allowing .htaccess files. See the AllowOverride documentation.

You don't need RewriteEngine On and DirectoryIndex just sets the file returned when you hit the containing directory (i.e., /python/) with no filename.

This solution just enables execution of .py scripts using mod_cgi. Other solutions which have suggested mod_python will also work but this is more complex and embeds a Python interpreter in the Apache processes. This isn't needed for straight execution using the system Python interpreter.

share|improve this answer

Did you make sure the directory is viewable? I've had permissions issues with directories interfere with the files they contained in the past.

Also - does the .htaccess work with any other scripts, like perl or bash?

Generally there's a default handler in the Apache configuration files for Python (handed-off to mod-python, I believe). If you remove the .htaccess, does the script run using the default handler?

share|improve this answer
The default handler would be just serving the Python file as text - something like mod_python would need to be installed and enabled explicitly. – David Z Sep 7 '09 at 7:21
fair enough - on just about every install of Apache I've seen on Linux, that has been pre-enabled :) – warren Sep 7 '09 at 7:25

I just tried this on my server, and it worked fine.

  • It did not seem necessary to CHMOD the htaccess.
  • Have you tried going to both "" and "" ? Both worked for me but maybe that DirectoryIndex line is not working properly for you? If I removed that line only / would work.

Also try adding these files to your htaccess (replacing as necessary):

AddHandler mod_python .py
PythonHandler index
PythonDebug On

On another of my systems, those lines were necessary to get the python handling to work (using mod_python).

share|improve this answer

I notice you have not mentioned where on your file system you're doing all this. If the folder you are working on does not have permissions set within the Apache config, you will get a 403 regardless of what Unix permissions are set on the file system. Apache will not serve out files physically stored in locations it has not been told it can use in the config.

This is perhaps a long-shot as you are probably working within your normal web directory, but worth testing none-the-less. Stick a plain text file in the same folder and try viewing it, if you see it then that's not the problem, if you still get a 403 have a look in the Apache config.

Other than that, I think markdrayton's suggestion is spot on.

share|improve this answer

Change your AddHandler to AddHandler mod_python .py.

I would rather edit /etc/apache2/mods-available/dir.conf and add there instead of using DirectoryIndex in your .htaccess.

Change your to:

#!/usr/bin/env python

def index(req):
    return "test"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.