htaccess rewrite incorrectly appending URL with mod_wsgi config file path

I'm currently in the process of migrating domains [old_domain] -> [new_domain].

The problem arose when I came to the htaccess rules and the config file in /etc/httpd/conf/httpd.conf:


AllowOverride All

Initially I could not find where to put the above to allow .htaccess rewrite rules to work and I'm still concerned that I may be incorrectly allowing it for all.

I've tried putting it in something like but to no effect:

<Directory "/path/to/django/project/">
    AllowOverride All

Once I did get the .htaccess file rewrites working (using the following) the URLs started returning something odd.

.htaccess in django project directory

RewriteEngine On
RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

URL sent

URL received

Nearly there... but can anyone suggest why this might be happening? I don't really want to be stripping out the /apache/production.wsgi/ on the new_domain side (unless this is expected?)

Thanks in advance.

Edit: One quick fix included using django's HttpResponsePermanentRedirect. Is this bad practice?


If you want to use the rewrite directive within your django folder then you need to enable overides. Otherwise they will be ignored:

Hopefully this does the trick for the rewrite:

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} !^newdomain\.com 
RewriteRule ^(.*)$$1 [R=301,L]

Be sure to delimit the periods in the domain of you RewriteCond. In regex, . means "match all"

Need Your Help

Python: add a new mimetype

python python-2.7 python-3.x mime-types mime

How can I add a new mimetype using the add_type() function in the mimetypes package? Do I need to do something with the python file system? If I try something like: add_type('text/new', 'new') no...

Django templates: formatting control structures that should not contain spaces

python django django-templates

I have a code-formatting issue. I need to output a sequence of strings and there should be no spaces between the printed strings (since I actually print words by segments, highlighting typos), but ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.