Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How do I redirect:

/text1 to /

and

/test2 to /something/else

I tried putting:

rewrite ^ /test1 /
rewrite ^ /test2 /something/else

Inside the server {}

share|improve this question
    
What sort of redirect? Do you want it to match subpaths? If so, what do you want to do with them (discard or preserve)? –  womble Jun 25 '12 at 15:40
    
there are not going to be any subpaths, exact matches only. Just very simple redirection. if /test1 then goto / or if /test2 go to /thispath. Actually examples with subpaths preserved and discarded would be nice to (for me to learn by example) –  user80666 Jun 25 '12 at 15:42
    
But what do you mean by "go to"? –  womble Jun 25 '12 at 15:43
    
someone types in site.com/test1, server sends redirect header and redirects them to / . –  user80666 Jun 25 '12 at 15:47
    
What response code? –  womble Jun 25 '12 at 15:57
add comment

1 Answer

up vote 1 down vote accepted

Just get rid of the space after the ^, and add the required ;:

rewrite ^/test1$ /;
rewrite ^/test2$ /something/else;

The $ characters denote the end of the string. Get rid of them if you want /test1/blah to match as well as /test1.

Edit: To send a redirect response to the browser add either redirect (for a 302 response) or permanent (for a 301 response) as a flag to your rewrite lines:

rewrite ^/test1$ / permanent;
rewrite ^/test2$ /something/else permanent;
share|improve this answer
    
Should these go into a specific location {} or just simply under server {} ? –  user80666 Jun 25 '12 at 15:44
    
They work in either context. And I have the same clarifying question as womble - are you looking to have this send a redirection response to the user so that the URL in their address bar changes, or keep /test2 in their address bar but serve the content from /something/else? –  Shane Madden Jun 25 '12 at 15:46
    
Yes, I would like the url in browser to change to / and /somethingelse respectively –  user80666 Jun 25 '12 at 15:53
    
@user80666 See edit. –  Shane Madden Jun 25 '12 at 15:58
    
That works thank you. One problem I noticed however, is that if I change the redirection url and restart the server browser still remembers the old redirection address, almost like it's cached. Is there anything I can do to prevent browsers cacheing the redirection url? –  user80666 Jun 25 '12 at 16:15
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.