EzDevInfo.com

redirect interview questions

Top redirect frequently asked interview questions

Redirecting to a relative URL in JavaScript

I have a problem: I want to redirect via JavaScript to a directory above. My code:

window.location.href = (location.href).substr(0, (location.href).lastIndexOf('folder'));

The url looks like this:

domain.com/path/folder/index.php?file=abc&test=123&lol=cool

The redirect affect just this:

domain.com/path/&test=123&lol=cool

But want to have this:

domain.com/path/

How could I do that?


Source: (StackOverflow)

How to make a redirect in PHP?

Is it possible to redirect a user to a different page through the use of PHP?

Say the user goes to www.example.com/page.php and I want to redirect them to www.example.com/index.php, how would I do so without the use of a meta refresh? Possible?

This could even protect my pages from unauthorized users.


Source: (StackOverflow)

Advertisements

How to manage a redirect request after a jQuery Ajax call

I'm using $.post() to call a servlet using Ajax and then using the resulting HTML fragment to replace a div element in the user's current page. However, if the session times out, the server sends a redirect directive to send the user to the login page. In this case, jQuery is replacing the div element with the contents of the login page, forcing the user's eyes to witness a rare scene indeed.

How can I manage a redirect directive from an Ajax call with jQuery 1.2.6?


Source: (StackOverflow)

How do I use sudo to redirect output to a location I don't have permission to write to?

I've been given sudo access on one of our development RedHat linux boxes, and I seem to find myself quite often needing to redirect output to a location I don't normally have write access to.

The trouble is, this contrived example doesn't work:

sudo ls -hal /root/ > /root/test.out

I just receive the response:

-bash: /root/test.out: Permission denied

How can I get this to work?


Source: (StackOverflow)

In the shell, what does " 2>&1 " mean?

In a Unix shell, if I want to combine stderr and stdout into the stdout stream for further manipulation, I can append the following on the end of my command:

2>&1

So, if I want to use "head" on the output from g++, I can do something like this:

g++ lots_of_errors 2>&1 | head

so I can see only the first few errors.

I always have trouble remembering this, and I constantly have to go look it up, and it is mainly because I don't fully understand the syntax of this particular trick. Can someone break this up and explain character by character what "2>&1" means?


Source: (StackOverflow)

Nginx no-www to www and www to no-www

I am using nginx on Rackspace cloud following a tutorial, searched the net and so far can't get this sorted.

I want www.mysite.com to go to mysite.com as normal in .htaccess for seo and other reasons.

My vi /etc/nginx/sites-available/www.example.com.vhost code:

server {
       listen 80;
       server_name www.example.com example.com;
       root /var/www/www.example.com/web;

       if ($http_host != "www.example.com") {
                 rewrite ^ http://example.com$request_uri permanent;
       }

I have also tried

server {
       listen 80;
       server_name example.com;
       root /var/www/www.example.com/web;

       if ($http_host != "www.example.com") {
                 rewrite ^ http://example.com$request_uri permanent;
       }

I also tried. Both the second attempts give redirect loop errors.

if ($host = 'www.example.com' ) {
rewrite ^ http://example.com$uri permanent;
}

My DNS is setup as standard:

site.com 192.192.6.8 A type at 300 seconds
www.site.com 192.192.6.8 A type at 300 seconds

(example IPs and folders have been used for examples and to help people in future). I use Ubuntu 11.


Source: (StackOverflow)

.htaccess rewrite to redirect root URL to subdirectory

Trying to get

www.example.com

to go directly to

www.example.com/store

I have tried multiple bits of code and none work. Please help!

What I've tried:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^(.+)\www.example\.com$
RewriteRule ^/(.*)$ /samle/%1/$1 [L]

What am I doing wrong?


Source: (StackOverflow)

redirect COPY of stdout to log file from within bash script itself

I know how to redirect stdout to a file:

exec > foo.log
echo test

this will put the 'test' into the foo.log file.

Now I want to redirect the output into the log file AND keep it on stdout

i.e. it can be done trivially from outside the script:

script | tee foo.log

but I want to do it from the inside

I tried

exec | tee foo.log

but it didn't work


Source: (StackOverflow)

How to redirect to previous page in Ruby On Rails?

I have a page that lists all of the projects that has sortable headers and pagination.

path:
/projects?order=asc&page=3&sort=code

I choose to edit one of the projects

path:
projects/436/edit

When I click save on that page, it calls the projects controller / update method. After I update the code I want to redirect to the path that I was on before I clicked edit a specific project. In other words, I want to be on the same page with the same sorting.

I saw link_to(:back) and thought that :back may work in redirect_to(:back), but that's a no go.

puts YAML::dump(:back) 
yields the following:
:back 

Any ideas on How I could get this to work. It seems like a problem that would be easily solved, but I'm new to RoR.


Source: (StackOverflow)

Python + Django page redirect

How do I accomplish a simple redirect (e.g. cflocation in ColdFusion, or header(location:http://) in django)?


Source: (StackOverflow)

Heroku/GoDaddy: send naked domain to www [closed]

I am trying to figure out how to get the naked domain for my website to redirect to the www domain. I am using Heroku and have the domain from GoDaddy. Because of Heroku, my A records are already set up as:

@: 75.101.145.87

@: 75.101.163.44

@: 174.129.25.170

And my www CNAME points to proxy.heroku.com.

I've been looking all over the internet, but can't find a simple, free answer for how I can do this redirect. Some answers discuss using paid services, which I don't want to do, and others talk about going in and modifying Heroku settings, but then don't really give much explanation. Right now, if you go to my naked domain, it's just Heroku saying that no such app exists, while if you go to my www domain it's my app.


Source: (StackOverflow)

apache redirect from non www to www

I have a mortgage calculator site that doesn't seem to redirect from mookal.com to www.mookal.com
My apache config is as follows:

RewriteEngine On
### re-direct to www
RewriteCond %{http_host} !^www.mookal.com [nc]
RewriteRule ^(.*)$ http://www.mookal.com/$1 [r=301,nc] 

What am I missing?


Source: (StackOverflow)

How can I redirect HTTP requests made from an iPad?

Since on an iPad we cannot edit the hosts file (without jailbreaking), how can we arbitrarily redirect web traffic to another url?

This would be important for something such as developing a website that uses a Virtual Host configuration where you want to redirect to a development machine.

(This is related to this question: Can I edit an iPad's host file?)


Source: (StackOverflow)

URL Fragment and 302 redirects

It's well known that the URL fragment (the part after the #) is not sent to the server. I do wonder though how fragments work when a server redirect (via HTTP status 302 and Location: header) is involved.

My question is really two-fold:

A: If the original URL had a fragment (/original.php#foo), and a redirect is made to /new.php, does the fragment part of the original URL simply get lost? Or does it sometimes get applied to the new URL? Will the new URL ever be /new.php#foo in this case?

B: Regardless of the original URL, if the server redirects to a new URL with a fragment (/new.php#foo), will the fragment get "honored"? Or does the server really have no business interfering with the fragment at all - and will the browser therefore ignore it by simply going to /new.php?


Source: (StackOverflow)

How to redirect a url in NGINX

I need to redirect every http://test.com request to http://www.test.com. How can this be done.

In the server block I tried adding

 rewrite ^/(.*) http://www.test.com/$1 permanent;

but in browser it says

  The page isn't redirecting properly

  Firefox has detected that the server is redirecting the request for 
  this address in a way that will never complete.

my server block looks like

 server {
            listen       80;
            server_name  test.com;
            client_max_body_size   10M;
            client_body_buffer_size   128k;

            root       /home/test/test/public;
            passenger_enabled on;
            rails_env production;

            #rewrite ^/(.*) http://www.test.com/$1 permanent;
            #rewrite ^(.*)$ $scheme://www.test.com$1;

            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                    root   html;
            }
    }

Source: (StackOverflow)