Redirecting old PHP links to Django urls - django

I am getting a lot of search engine referral links for my previous PHP developed site that has now been migrated over to Django. I made a url redirect for the old php links like search.php?name=john+smith to the same view for my django search url as shown here:
urls.py
url(r'^search.php/$', profile_search, name='search'),
url(r'^search/$', profile_search, name='search'),
Will Google eventually update those old links if I redirect through urls.py or do I need to make a 301 redirect? If so how would I do this with django and nginx?

I would do this at nginx level - this is much more efficient than having Django handle it. Assuming the Django view expects the same query arguments, you can do this in your nginx server block:
location = /search.php {
return 301 http://$server_name/search/$is_args$args;
}
This will redirect all requests for search.php to /search/, preserving any query arguments.
A 301 response is definitely the correct approach - you don't want to serve duplicate content on different URLs.

Unless you have HttpResponseRedirect in your profile_search method, you don't actually have any sort of redirect here at. But what you really want to use is HttpResponsePermanentRedirect
def profile_search(request):
return HttpResponsePermanentRedirect('/somether/url/?based_on_request_params')

Related

Moved legacy classic asp web site to django app but having trouble with django URL redirect app - old URLs with parameters get 500 error

I moved a 20 year old classic asp web site to a django app recently. Google Search Console is showing 500 errors for tons of very old URLS with parameters like /somefile.asp?ID=1234&name=some-name. Most of these URLS I don't even care about - they are really old but would rather they result in 404 than 500. Some do relate to newer content and in theory I could redirect to new django pages but when I try to redirect them using the django redirect app, the site shows 500 errors - like the ? either stops the pages from ever getting to the redirect middleware or the redirect app doesn't like the "?". How can I redirect these urls so I don't get 500 errors? Is there a way in a view to say:
if the full url path is x
redirect to full url path y
I have looked all over stack overflow and elsewhere and can't find this exact problem or a way to write a view like this. I did try the solution here (exactly) because it seems closest to my issue, but it did not work for me for this exact problem: Django's redirects app doesn't work with URL parameters
An example would be /profiles/?adid=134&v=857
The error with debug true is:
DoesNotExist at /profiles/
BusinessCategory matching query does not exist....
So it is trying to match this URL:
path('<slug:slug>/', views.view_category_list, name='view_category_list'),
before it ever gets to the /?
Relevant view code:
def view_category_list(request, slug):
try:
category_detail = BusinessCategory.objects.get(slug=slug)
except BusinessCategory.DoesNotExist:
category_detail = None
if category_detail:
--code here is all working fine so removed it to get to the issue--
else:
raise Http404
I hoped raising the 404 would call up the redirect app to do its job at redirecting to a specific url but it doesn't - it just shows the custom 404 when debug is False. Anyway, the 404 is better than the 500 for SEO purposes but I still would prefer being able to actually redirect some of the URLs to relevant content.

How to redirect old url to new url django 1.9

I am new in python django 1.9 and tried to redirect using urls.py file,
Here I explain actually what I tried.
Old : www.abc.com/c.aaa.html , www.abc.com/p.aaa.html and www.abc.com/books2
just redirect to home page like www.abc.com
url(r'/c.*$', include('apps.urls'),name='home'),
url(r'/p.*$', include('apps.urls'),name='home')
Try to use django's base RedirectView. For example you could do this:
url(r'/c.aaa.html$', RedirectView.as_view(url='/'), name='go-to-home'),
Check this and this documentation materials for information on how to do redirects in django.

redirect rule for nginx?

I am running my django app using nginx. I want to write a redirect rule such that
if user hit the url http://example.com/django/nginx/ then it redirect it to
http://example.com/django/#!/nginx/. I want o know the regex for it.
Thanks
You'll want to handle this on the client side (through Javascript, most likely), not through nginx.
From what I understand, the point of # in URLs (as per the spec) is that the portion that comes after # doesn't reach the server.
Also, see this question for some info on JS libraries for working with hash-bang urls: Are there any javascript libraries for working with hashbang/shebang (#!) urls?
Given you example I'm assuming that you are working with URLs in the form "http://1/2/3/" only, so nothing going beyond 3. Where you want to separate 2 and 3 with "/#!/". If that is the case you can try the following.
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
('^django/(?P<ajax_section>\w+)/$', redirect_to, {'url': '/django/#!/%(ajax_section)s/'}),
)
The above assumes that 2("django") in the URL will be fixed. If that is not the case you will have to try and make it a parameter as well.

Unable to make a domain redirection in Django from example.com to www.example.com

I get the following message when I browse to example.com
500 - Internal Server Error
I get my 404 error message when I browse to www.example.com which indicates my site is alive.
How can you make a domain redirection without .htaccess by Django from example.com to www.example.com?
My urls.py
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^{{ project_name }}/', include('{{ project_name }}.foo.urls')),
(r'^home/', include('index.html')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^admin/(.*)', admin.site.root),
)
[edit]
I have the following answer from Djangohosting:
Remove the website proxy for [example.com] and add [example.com] in the aliases section of the [www.example.com] website.
I am not completely sure what it means.
How can you remove the website proxy for example.com?
Where can you find the aliases section of the website www.example.com?
I don't know if it solves all your problems but if you want to do it via django, try a middleware
from django.http import HttpResponseRedirect
class WWWRedirectMiddleware(object):
def process_request(self, request):
if not request.META['HTTP_HOST'].startswith('www.'):
return HttpResponseRedirect('http://www.example.com')
Remember that it must be executed before any other middleware.
Based on this one, didn't test it.
Checking out PREPEND_WWW setting might help.
The 500 Internal Server error is likely caused by this line:
(r'^home/', include('index.html')),
The include() function is a way to include other URL config files, not static HTML files. You'd have to either serve index.html as a static file at the given URL (/home), or write a view that simply renders the file.
I get the following message when I browse to example.com
500 - Internal Server Error
I get my 404 error message when I browse to www.example.com which indicates my site is alive.
It is likely the other way around. The 500 message means that your site is active, but giving an error. What you have to understand is that the example.com/www.example.com part of the url serves to find which server to connect to. By the time your Django application gets called, this part has already been completed, so there os no way to do this from Django.
Instead, you want to set up www as a subdomain of example.com and make them point the same place. See the first example here: virtualhosts examples
UPDATE I just noticed that you stated that your 404 was showing up, so ignore the first two sentences. Either way, the solution would likely be the same. :-)

How to redirect a http request with apache / django

I've made a simple site in Django. The urls I use are http::/www.example.com/nl/ and http://www.example.com/fr/.
My Django urls.py has the following line:
(r'^(?Pnl|fr)/', 'example.views.index'),
In example.views.index I check the language parameter. If it's 'nl' I show a template. If it's 'fr', I show a different template.
This worked great. Now the customer made two different urls:
http://www.dutch.com/ and http://www.french.com/
And finally I'll ask the question:
Is there a way for me to use the new urls without changing my django code? I assume I can tell apache to present the http://www.example.com/nl/ page when the user goes to http://www.dutch.com/. But how do I do this? And will django still be able to get the 'language' parameter from the url?
Thanks in advance for any answers.
If you can use .htaccess files on http://www.dutch.com that you can use apache's redirect directive like so
redirectMatch 301 ^(.*)$ http://www.example.com/nl/
This will redirect all requests sent to dutch.com to example.com/nl
You could also use
redirect 301 /index.html http://www.example.com/nl/
This will redirect only "index.html" on dutch.com to example.com/nl/ (note that the first parameter is a path and can't be an URL - no http://www)