开发者

URL Redirect with Python / Django / Mod-Rewrite

开发者 https://www.devze.com 2023-01-18 21:13 出处:网络
I am trying to setup a URL redirect for a Q&A site I am setting up for Boat Repairs. I want boatrepaired.com to go to www.boatrepaired.com. I am generally a php guy so I am a bit confused with pyt

I am trying to setup a URL redirect for a Q&A site I am setting up for Boat Repairs. I want boatrepaired.com to go to www.boatrepaired.com. I am generally a php guy so I am a bit confused with python etc. used by OSQA. I added this to my apache conf file...

<Directory /opt/OSQA/>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^boatrepaired\.com [NC]
RewriteRule ^(.*)$ http://www.boatrepaired.com/$1 [L,R=301]
</Directory>开发者_C百科

It somewhat works by sending boatrepaied.com to ... www.boatrepaired.com/osqa.wsgi/

If I remove off the $1 on line 5 it works perfect except it redirects everyone back to the front page. Any help would be appreciated.

Thanks,

Chris


OSQA is based on Django, and Django can automatically do this for you (I am pretty sure this is enabled for OSQA by default).

If not, add this setting to your Django settings module:

If PREPEND_WWW is True, URLs that lack a leading “www.” will be redirected to the same URL with a leading “www.”leading “www.”

And make sure the django.middleware.common.CommonMiddleware is enabled.


Why not fix this entirely in apache:

<VirtualHost ..>
    ServerName boatrepaired.com

    Redirect permanent / http://www.boatrepaired.com/ 
</VirtualHost>

<VirtualHost ..>
    ServerName www.boatrepaired.com

    ... basic wsgi / django config ...
</VirtualHost>

So separate vhosts for both domains, with permanent redirects from the "wrong" one to the correct one. It will work for all paths within the domain, not just /


If you want to use apache's rewrite, the issue that you had was related to the rewrite rule line not properly extracting the URL properly. Django requires a more complicated regular expression than the examples given on the Apaache site. For example, for wsgi you could do this to redirect http://example.com to http://www.example.com

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)django.wsgi(.*)$ http://www.example.com$2 [R=301,L]

where $2 is the second match of the regular expression, which is the part that you want to append to your www.example.com.

0

精彩评论

暂无评论...
验证码 换一张
取 消