how to copy url without "www" to the url with "www"? - django

I'm hosting a Django project, let's say the name is www.example.com, when an user search for example.com/subname to redirect to www.example.com/subname. Unfortunalety, right now it's redirecting to www.example.com without adding the "subname" part to my url.
I have my domain name on infomaniak which redirects example.com to www.example.com, the hosting is on pythonanywhere where it handles my Django project.
How can I achieve the case where what's put after domain name url is kept and used to redirect to the one with www. ?
(For my Django project I'm not using apache nor nginx, so where is the htaccess file if the changes has to be made there ?)
UPDATE for Udlei Nati's answer :
UPDATE 2 : for Astik Anand : This is where the actual redirection is made. (site.com redirect to another site which was the old version of the one on www. (that's why I'm trying to override it.))

Related

How to redirect a url with same folder name?

I am trying to redirect a url with below reference page:
Configuring a webpage redirect - Amazon Simple Storage Service
It is working for different folder names, but it is not working in same folder name. for example, I have to redirect the page from grid/row/ to grid/row/row. I am failing with too many redirects issue.
So please any one guide me to resolve this issue?
If I go to rid/row/ url, then it will be switch to grid/row/row/ url page.

Regex - redirect from website.com to website.com/us and append substring

Can anyone help me correct my regex?
My hosting provides IP geolocation and I want to redirect US clients to another sub-directory of my website.
The website runs on a Nginx server and the hosting provides an interface where one can add redirection rules. The interface consists of Domain, Redirect from, Redirect To, Traffic from (country) and HTTP status code 301 or 302.
Example:
for all non-US clients website.com/blog/article/really-good-book
only for US clients website.com/us/blog/article/really-good-book
I currently have:
Redirect from ^/(?!us/)(.*)$
Redirect to /us/$1
This currently redirects me to website.com/us/index.php and nothing else. So the redirect is applied, it only appends index.php instead of blog/article/really-good-book.
Thank you for your help.
Eventually I found my own solution:
Redirect from ^(?!(/us|/index.php|/wp-admin|/wp-login.php))(.*)$
Redirect to /us/$1
I also added a rule for my wordpress backend, otherwise it would always redirect me back to website.com/us while trying to access website.com/us/wp-admin.

Redirect domain to .com in django in url.py

I have a website in Django, hosted in Heroku.
I have 2 domains, registered in different places:
mysite.com and mysite.com.br
I don't want to be penalized by Google for having 2 domains with the same website, therefor I would like to redirect everyone who enters mysite.com.br to mysite.com.
I entered in the DNS mysite.com.br(Not hosted anywhere) CNAME mysite.com(hosted in Heroku), which makes the user actually access the Heroku content, but the url keeps the .BR ....
So the Heroku support told me to do the redirection in the application. In this case, what's the best practice to redirect? I would imagine to do that in the url.py, but how can I do that if the "path" doesnt read the domain?
Thanks.
You can't do this in the URLs. I would write some middleware to check the host via request.get_host and redirect if you're not on the canonical one.
In fact it looks like someone has already written this: django-enforce-host.

Django/Heroku hide heroku url by subdomains name

I'm currently deploying some django app on Heroku.
I bought a domain name on Gandi, and made a subdomain redirecting (temporary redirection) on my heroku server. Currently, the subdomain is redirecting to the machine, this is working.
mysubdomain.mydomain.com --> something.heroku.com
However, when I navigate throught my website, I still have on my address bar the heroku domain name in front of my page (something.heroku.com/detailpage/slugforexample)
I would like to hide the heroku domain name by my subdomain name (to have something like subdomain.domain.com/detailpage/slugforexample)
I set the Gandi configuration (temporary redirection to heroku server)
I set on heroku domains:add with my subdomain.
Where do I have to configure the redirection ? Is it in heroku, wsgi, django conf ?
You shouldn't use Web Forwarding to achieve what you want. You should add a CNAME entry on GANDI for mysubdomain on your mydomain.com domain with a value of something.heroku.com. You should then add a custom domain to your application via heroku domains:add mysubdomain.mydomain.com - it looks like you've already done this last bit.
This means you will be able to navigate to mysubdomain.mydomain.com and the address bar will rename as this.

Mapping a naked URL to the equivalent www URL

I'm currently using dotcloud hosting for an application and I'm redirecting my naked url using my domain registrar namecheap (through "#" hostname and "*" hostname as a URL direct- and then a cname for www to dotcloud)
This works well for re-directing people back to main www.
My issue is I want to map the visitor to the equivalent naked domain url they were trying to visit. E.g. http://example.com/websitepage1 to http://www.example.com/websitepage1. Right now visiting any page on the site minus the "www" will just redirect you to the homepage.
Any ideas or suggestions?? I'd prefer not to switch out of dotcloud hosting for the moment but will do so if it's impossible to get this to work another way.
Thanks!
UPDATE
The urls that need to be redirected are created dynamically so it's not impossible to hard code them. The DNS doesn't have to be namecheap. I can put the nameservers else where if there is an option that allows redirect of naked urls to the www equivalent. Any ideas appreciated!
You need to add rewrite rules to nginx.conf:
server {
server_name hostname.org;
rewrite ^(.*) http://www.hostname.org$1 permanent;
}
You can specify custom rewrite rules (and actually almost any Nginx setting) by creating a file named nginx.conf at the top of your app. http://docs.dotcloud.com/0.9/guides/nginx/
Given their documentation (0), it seems possible to do the following 301/302 redirects using Namecheap's forwarding options:
Setup a naked domain redirect from example.com to www.example.com
Setup a naked domain redirect with a url from example.com/url/ towww.example.com/url/`
I don't see in the documentation where it says they support a wildcard option, so for every url you'd like to support, it seems you may have to create a forwarding entry.
(0): http://www.namecheap.com/support/knowledgebase/article.aspx/385/77/how-do-i-setup-url-forwarding-for-a-domain