Forum Moderators: phranque

Message Too Old, No Replies

Preventing a string of 301 redirects

         

ocon

11:16 am on Oct 16, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hello,

I use multiple 301 redirects on my server.

I have one to redirect my domain without www to the www version ( "domain.com" => "www.domain.com" ),
and I also have one for the root to redirect users to my site's main page ( "/" => "/wiki/Main_Page")

My concern is that I'm creating a string of 301 redirects. If I type in domain.com, I get transferred to www.domain.com and then on that page I get transferred once more to www.domain.com/Main_Page.

I guess I could create a redirect for this special case, but I'm wondering if there is a more appropriate way of telling my server not to redirect the traffic until all the rules have taken place?

I'm using nginx, and my conf file looks like:

server {
listen 80;
server_name domain.com;
rewrite ^/(.*) http://www.domain.com/$1 permanent;}

server {
listen 80;
server_name www.domain.com;

location / {
...
rewrite ^/$ /w/index.php last;}

location ~ /wiki/ {
rewrite ^/([^?]*)(?:\?(.*))? /w/index.php?title=$1&$2 last;}

...}


Thanks for any help.

jdMorgan

4:06 pm on Oct 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Put the external URL-to-URL redirects in order from most-specific URL and conditions to least-specific. Follow them with all internal URL-to-filepath rewrites, again in order from most- to least-specific.

This general advice is really tailored to Apache .htaccess and config files, and not to Nginx. I assume that its config functions are similar. For those using PHP, external redirects are straightforward, while the equivalent of an internal rewrite is a direct file "require" (read in the file and output its contents).

In your scenario, and assuming that you really want or need to reveal "www.domain.com/wiki/Main_Page" as a URL to HTTP clients (browsers and search engines) instead of just rewriting requests for the URL "www.domain.com/" to the filepath "/wiki/Main_Page" silently, your first redirect should be from domain.com/wiki/Main_Page" to "www.domain.com/wiki/Main_Page". This redirect should be the first one because it is specific to a single "page" URL. Follow this with any other single-page- or single-object-specific redirects. The last redirect should be the generic non-www to www- redirect.

Following all external redirects, do the internal URL-to-filepath rewrites, again in order from most-specific to least specific.

To clarify, a most-specific redirect or rewrite affects a single protocol, hostname, port, URL-path, query string, and URL-fragment, while a least-specific redirect or rewrite may affect many or even all of the URL-paths on the site.

By putting the directives in this order, all "problems" with any given requested URL are fixed all in one step by the first directive that matches that request. This avoids both chained redirects and the exposure of internally-rewritten filepaths to clients as URLs.

[added] Note the distinction I've made here between external URL-to-URL client redirects and internal URL-to-filepath rewrites. This distinction is critical to "getting it right."

If you want to check your work, use the "Live HTTP Headers" add-on for Firefox and Mozilla-based browsers, or one of the other similar add-ons for Firefox and other browsers. Beware of Web-based server headers checkers, as many of them will only show you the "final" redirect in a chain of redirects. [/added]

Probably an overly-long description for a simple answer... :)

Jim