Check if a URL is still active or Permanently Moved - coldfusion

Is there a way to check if a URL still active or it returns a 301 Redirect code using chhttp?

You've answered your own question - use CFHTTP.
<cfhttp method="head" url="http://www.google.com" result="myResult">
<cfdump var="#myResult#">
You'll see that myResult struct contains "Responseheader" struct with "Status_Code" field in it. It will contain the numeric status code.
If you don't need numeric you can just use myResult.Statuscode - returns something like "200 OK".
If the url will be incorrect or there will be some issues with reaching the target, the "Responseheader" will be empty.
Addendum after the comment:
If you want to know the redirect location after 301 use CFHTTP with trace or options as method.
<cfhttp method="trace" url="http://www.google.com" result="myResult">
The result should contain the "Status_Code" as above as well as "Location" - the url to redirect to.

You have to use the redirect attribute (e.g. redirect="false") in cfhttp tag. Then you get the original response and can figure out what http code was sent back to your request.

Related

Trouble with redirecting to an absolute_uri

I'm attempting to redirect to a full url
current url is an absolute_uri
currenturl = request.GET.get('currenturl') #lets say this resolves to http://www.google.com/
return redirect(currenturl)
This sends me to an 404 error page as it's trying to resolve the following url
http://localhost:8000/accounts/profile/calendar/delete/15/'http://www.google.com/'
I was passing the absolute uri in the template into the currenturl with single quotes around it. I had to remove the single quotes.

Django url dispatcher page not found

i am trying to define a url pattern in django urls.py like
url(r'^networking$','mysite1.networking.views.networking'),
when i am typing http://myhost.com/networking in my address bar to go to networking page
i am getting 404 error and a slash '/' automatically added to the address bar like
http://myhost.com/networking/
help me out what i am doing wrong?
You probably aren't including your urlconf correctly. The behavior you're seeing is because of APPEND_SLASH is set to True by default when Django can't resolve the url.
Either set Append_Slash to false which is true by default or use your url description like given below which redirect url with slash to desired view.
url(r'^networking/$','mysite1.networking.views.networking'),
Seems your Apache server or some Django middleware is adding trailing slashes. You can either correct that, or the better way is you can use the following url pattern:
url(r'^networking/?$','mysite1.networking.views.networking'),

How to organize URLs in django for views handling GET data and parsing URL?

I have a view that displays some movie data. I thought that it might be a good idea to have a view handle a an URL like movie-id/1234 to search for movie id 1234. Furthermore I would like to be able to enter the ID into a form and send that to a server and search for it. To do that I created a second entry in the urls.py file shown below.
urlpatterns = patterns('',
url(r'movie-id/(?P<movie_id>.+?)/$', 'movieMan.views.detailMovie'),
url(r'movie-id/$', 'movieMan.views.detailMovie', name='movieMan.detailMovie.post'),
)
So if I want to pass data to my view either via a URL or a GET or POST request I have to enter two urls or is there a more elegant way? In the view's code I am then checking if there is any GET data in the incoming request.
To make the second url usable with the template engine, where I wanted to specify the view's url using the {% url movieMan.detailMovie.post %} syntax I had to introduce a name attribute on this url to distinguish between these two.
I am not sure if I am thinking too complicated here. I am now asking myself what is the first url entry good for? Is there a way to get the URL of a movie directly? When do these kinds of URLs come into play and how would they be generated in the template ?
Furthermore I would like to be able to enter the ID into a form and
send that to a server and search for it.
Is this actually a search? Because if you know the ID, and the ID is a part of the URL, you could just have a textbox where the user can write in the ID, and you do a redirect with javascript to the 'correct' URL. If the ID doesn't exist, the view should return a Http404.
If you mean an actual search, i.e. the user submitting a query string, you'll need some kind of list/result view, in which case you'll be generating all the links to the specific results, which you will be sure are correct.
I don't think there is a more elegant way.
I did almost the same thing:
url( r'^movies/search/((?P<query_string>[^/]+)/)?$', 'mediadb.views.search_movies' ),
The url pattern matches urls with or without a search parameter.
In the view-function, you will have to check whether the parameter was defined in the url or in the query string.

Get the original path in django

I have a question: how to get the current path of the url. Let's say, I have 3 navigation bars, about , blog and contact page. In each page, I have facebook, twitter and a manual email a friend button. When I clicked the email a friend button, and the current URL is www.example.com/about, the current URL is now already www.example.com/emailafriend. How can I get the www.example/about? Also in blog and contact. Please help me. Thanks.
How does your email a friend button work? Is it a django view that takes the current URL and emails it? If so, you don't want the "current" URL, which, as you note, is actually the email a friend URL. What you want to do is pass the URL you want to share as a URL parameter, ie:
/share?url=http://www.example.com/blog
Adding more info based on comments:
When I was referencing URL above, I was not referring to your django URL configuration. Let's take a step back.
On your About page you have a link to email a friend, right? That link is probably generated in your template, but it's the same on every page. Something like:
Email a friend
Instead of this, try this:
Email a friend
Now you need to make your email_a_friend view handle this. It can get the url via
request.get('url', '').
Some additional information:
You might want to escape the {{ request.get_full_path }} function so that it's escaped and URL safe, then you'll have to unescape it in your view. Once you get the URL back to your view, you can do as you please with it.
{{ request.get_full_path|urlencode }}
Try using Relative URLs like for example From www.example.com/about to get to www.example.com/email use /email. Using relative urls is the simplest solution .
Take a look at this.
Absolute vs relative URLs
It sounds like your want to get the referring URL (the URL that sent you to the current page). That is available to you in the request object, although it is not 100% reliable:
request.META['HTTP_REFERER']
See the documentation on HttpRequest objects for more information.

Append value from cookie to URL in Django

I have a value the user sets via a cookie and I want this value to be appended onto the URL after the user sets it. How can I enable this? Any ideas are welcome as I'm not so sure how to go about this.
This seems like an ineffective solution, but what if you write a view to check for the existance of the cookie, if it doesn't exist let them set it. If it does exist, HttpRedirect them to url + cookie-value. And add a line in your urls.py to match those urls.