Ogre
Ogre

Reputation: 167

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:

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
</Directory>

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} !^old_domain.com/$ [NC]
RewriteRule ^(.*)$ http://new_domain.com/$1 [L,R=301]

URL sent

http://old_domain.com/news/

URL received

http://new_domain.com/apache/production.wsgi/news/

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?

Upvotes: 0

Views: 992

Answers (1)

Roja Buck
Roja Buck

Reputation: 2354

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

http://httpd.apache.org/docs/current/mod/core.html#allowoverride

Hopefully this does the trick for the rewrite:

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

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

Upvotes: 2

Related Questions