Redirect Question for Directory Structure Change

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Redirect Question for Directory Structure Change

zakirenish
I'm looking for help with a permanent site-wide redirect.

I have a website that I moved to a new server.
For some crazy reason the old server had an odd structure that I am
changing.
The URL root  for the old site is http://threedaystubble.com/e/
I have changed it to be http://threedaystubble.com/

Example:
threedaystubble.com/e/gallery.html --- redirects to ---->
threedaystubble.com/gallery.com

There are many other folders and html files that need this site-wide
redirect.

Of course anyone can access the site when linking to the root, but there are
many links out there referring to the old directory structure under /e/.
I'm looking for assistance writing a redirect so all those inbound links out
there on the internet work again.

I tried a couple of ideas, but they didn't work,
I thought this location directive inside a server block was best, but it
didn't work.

location = /e {
       return 310 $scheme://threedaystubble.com;
}

Any help would be greatly appreciated.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,289363,289363#msg-289363

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

RE: Redirect Question for Directory Structure Change

Reinis Rozitis
It is a bit unclear if you want only a single rewrite or are there multiple different directory mappings/redirects.

> I tried a couple of ideas, but they didn't work, I thought this location directive
> inside a server block was best, but it didn't work.
>
> location = /e {
>        return 310 $scheme://threedaystubble.com; }

This means that only '/e' will be redirected and not '/e/site.html' (and not even '/e/') and it won't preserve the request uri.

This should work:

location ~ ^/e/(.*) { return 310 $scheme://threedaystubble.com/$1; }


Or you could do it via rewrite in the server {} block:

rewrite ^/e/(.*)  /$1 last;

(you can replace the 'last' to 'permanent' if you want the client to know about the structure change and get the 301 redirect).




p.s if there are multiple redirect locations (besides /e) an easy way is to group them together via map directive.

rr


_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: RE: Redirect Question for Directory Structure Change

zakirenish
Thank you Reinis.

I really appreciate your help and your patience.
I am trying to learn this, so seeing what how it works is very useful.

To be clear, hopefully, I need all the (multiple) subdirectories of
threedaystubble.com/e/ in existing inbound links to refer to the new
structure threedaystubble.com/~

>p.s if there are multiple redirect locations (besides /e) an easy way is to
group them together via map directive.

All the redirect locations are in /e/.
In fact the path to everything is the same except I removed the /e/.
So, maybe I don't need to use the map directive, right?

This didn't work:

location ~ ^/e/(.*) { return 310 $scheme://threedaystubble.com/$1; }

This seems to work:

<rewrite ^/e/(.*) /$1 permanent;

Thank you, now I want to understand it.
I read that using return was less processor intensive, so I was hoping that
that one would have worked.

Somehow the rewrite seems wrong.
I expected the browser to show the new location, but when I see the address
in the browser using the rewrite above it shows threedaystubble/e/~ even
though the files are not really in that location.
I could have just as easily put the files in a /e/ as they were, but I don't
like that structure for various reasons, hence the change.
The inbound links work, which is great, and if the new location is being
propagated (i.e. new inbound links won't be created in the future that refer
to /e/) then it's okay.

What do you think? Is what I want to do clear?

Thanks again.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,289363,289369#msg-289369

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: RE: Redirect Question for Directory Structure Change

zakirenish
I was wrong...

>This seems to work:
>>rewrite ^/e/(.*) /$1 permanent;

It only works for the first level...
'threedaystubble.com/Gallery.html' works but other links from that page that
got deeper into the file structure do not!

So, maybe map directive is needed after all...

I'm planing to try that next, but I want to make sure that a more simple
'return' redirect won't work in my case.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,289363,289372#msg-289372

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

RE: RE: Redirect Question for Directory Structure Change

Reinis Rozitis
> I was wrong...
>
> >This seems to work:
> >>rewrite ^/e/(.*) /$1 permanent;
>
> It only works for the first level...
> 'threedaystubble.com/Gallery.html' works but other links from that page that
> got deeper into the file structure do not!

What do you mean by "got deeper" can you give a sample url that doesn't work?
Like what the uri is currently and what you need it to be?

Also a more complete nginx configuration might help to pinpoint the problem (because the order of regular expression location matching matters etc).

rr

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: RE: RE: Redirect Question for Directory Structure Change

zakirenish
Thank you Reinis!

Please bear with me...
It seems that I'm getting different results than I described earlier...

In fact it is now working for the most part...
The errors are limited to certain files in Chrome on the Mac, but not in
Safari or Firefox.

>What do you mean by "got deeper" can you give a sample url that doesn't
work?
>Like what the uri is currently and what you need it to be?

Different browsers show different results... Yes, I'm check in browsers.
I'm not good with logs yet.

using your suggested rewrite:
>rewrite ^/e/(.*) /$1 permanent;

This example is for one slide in a slideshow of posters...

In Safari on a Mac/iPad/iPhone:
http://www.threedaystubble.com/e/Gallery_Posters.html#4
the expected result is:
http://www.threedaystubble.com/Gallery_Posters.html#4

In Chrome on a Mac (only?)
http://www.threedaystubble.com/e/Gallery_Posters.html#4
the erroneous result is:
http://www.threedaystubble.com:8080/Gallery_Posters.html#4

This error is odd because I'm not using port 8080 at this time.

The same error is also true for these files:
http://www.threedaystubble.com/Gallery_Posters.html
and all of the posters in the associated slideshow

http://www.threedaystubble.com/e/Gallery_Album.html
and all of the art in the slideshow

I would think the slideshow widget is the cause, but these other two slide
shows in the gallery work correctly.

In Chrome on the Mac
http://www.threedaystubble.com/e/Gallery_Photos.html
the expected result is:
http://www.threedaystubble.com/Gallery_Photos.html

http://www.threedaystubble.com/e/Gallery_Other.html
the expected result is:
http://www.threedaystubble.com/Gallery_Other.html

Do you think that this might be related to nginx or the redirect?

If not, I don't want to bother you about it.
The fact that it rewrites threedaystubble.com:8080/~
makes me think it might be related.

Odd, however, that is is just happening in Chrome.
Safari and Firefox are okay.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,289363,289376#msg-289376

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

RE: RE: RE: Redirect Question for Directory Structure Change

Reinis Rozitis
> Please bear with me...
> It seems that I'm getting different results than I described earlier...
>
> In fact it is now working for the most part...
> The errors are limited to certain files in Chrome on the Mac, but not in Safari
> or Firefox.

You should clean cache (or set to never cache) for each testing session/config change as browsers tend to cache the redirects very heavily (especially for static content).

A better way as Francis suggested is to use a curl or other tools (like wget -S ..) which don't cache anything and you'll always get the actual behavior.

rr

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: RE: RE: RE: Redirect Question for Directory Structure Change

zakirenish
>You should clean cache (or set to never cache) for each testing
session/config change as browsers tend to cache the redirects very heavily
(especially for static content).

>A better way as Francis suggested is to use a curl or other tools (like
wget -S ..) which don't cache anything and you'll always get the actual
behavior.

Thanks, I will work on it.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,289363,289384#msg-289384

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Redirect Question for Directory Structure Change

J.R.
In reply to this post by zakirenish
You really should use a custom named capture group as the default "$1"
(and $2, $3, $4...) can cause erroneous output if there is any other
capturing going on in your configuration files...

i.e.

    location ~ ^/e/(?<x1>.*) {
        return 301 /$x1$is_args$args;
    }

As someone else mentioned, be sure to disable caching when doing
testing... In Chrome / Firefox, you can press 'F12' for the developer
tools, then choose the 'network' tab and check the box 'disable
cache'. When you are done testing, just press F12 again...
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: RE: RE: RE: Redirect Question for Directory Structure Change

zakirenish
In reply to this post by zakirenish
Reinis,

Thank you so much for all your help!

I have succeeded in getting things set up for now.
I couldn't have done it with out you guys.

I have learned a lot form you and Francis and I am very grateful.
Thank you for being patient with my ignorance.

Sorry to both of you for wasting your time with my caching blunders.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,289363,289388#msg-289388

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx