0

I have a blog made in django in a VPS. The blog is working fine but to access it I have to write the url example.com/blog/

What I'm trying is to make an automatic redirection so when a user enters example.com/ it automatically redirects to example.com/blog/

The project is set under apache.

This is my the configuration in myproject/urls.py:

urlpatterns = [
url(r'^blog/', include('blog.urls', namespace="blog")),
url(r'^admin/', include(admin.site.urls)),
]

This is the configuration of myproject/blog/urls.py that right now is formed by a post list and a post detail:

urlpatterns = patterns('',
# Index
url(r'^(?P<page>\d+)?/?$', ListView.as_view(
    model=Post,
    paginate_by=5,
    ),
    name='index'
    ),
# Individual posts
url(r'^(?P<pub_date__year>\d{4})/(?P<slug>[a-zA-Z0-9-]+)/?$', DetailView.as_view(
    model=Post,
    ),
    name='post'
    ),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

I already tried to add a .htaccess with different configurations but it's not working.

Is there a way to redirect from django?

hoaphumanoid
  • 977
  • 1
  • 9
  • 25
  • write one view for example.com/ and redirect to /blog – Geo Jacob Nov 30 '15 at 09:31
  • Why have you set it under /blog in the first place? Why not host it at / ? – Daniel Roseman Nov 30 '15 at 09:31
  • @GeoJacob thanks for your reply. I'm not sure how I can do this, could you please set a code example – hoaphumanoid Nov 30 '15 at 09:35
  • @DanielRoseman that is a good question. The answer is because I would prefer the url to appear as example.com/blog/. However if I still have problems about this I might change it to the root. – hoaphumanoid Nov 30 '15 at 09:36
  • 2
    [Redirect to named url int urls.py](http://stackoverflow.com/questions/15706489/redirect-to-named-url-pattern-directly-from-urls-py-in-django) – CubeRZ Nov 30 '15 at 09:39
  • 1
    You don't say how you're hosting your site, but seems likely this would be best done directly in the Apache conf. – Daniel Roseman Nov 30 '15 at 09:44
  • @DanielRoseman I host the site with apache. I tried to redirect with .htaccess but it's not working. If I don't find the solution I'll do what you suggested of changing everything to root – hoaphumanoid Nov 30 '15 at 12:16
  • .htaccess needs to be specifically enabled. You should do it directly in the virtualhost configuration: see for example [this guide](https://www.digitalocean.com/community/tutorials/how-to-create-temporary-and-permanent-redirects-with-apache-and-nginx). – Daniel Roseman Nov 30 '15 at 12:25

1 Answers1

0

If you're sure that you want that / to redirect to /blog/ for that you can create a temporary index view and redirect the response to /blog/

url(r'^', temp_index, name='index'),

def temp_index(request):
    return HttpResponseRedirect('/blog/')

If you want that / must show /blog then replace

url(r'^blog/', include('blog.urls', namespace="blog")), with url(r'^', include('blog.urls', namespace="blog")),

Tarun Behal
  • 908
  • 6
  • 11
  • It's not working. It says that the page isn't redirecting properly. It looks that there is a conflict between the root page and the /blog/ page. The redirection works however. When I put / it goes to /blog/ – hoaphumanoid Nov 30 '15 at 12:00