Moving Joomla from subdir to root -> rewrite / redirect problem

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

Moving Joomla from subdir to root -> rewrite / redirect problem

Johannes Rohr

Dear all,

in order to have prettier URLs I have decided to move my joomla from /web/ to /, but I want old URLs to transparently redirect.

I am struggling how to do this.

First I though of something like

location /web {
  try_files $uri $uri/ /index.php?$args;
 }

but this obviously did not work as the arguments passed to index.php still contain the /web/   part  , which would have to go.

So I tried instead:

location / {
    rewrite ^/web/(.*)$ /$1;
  try_files $uri $uri/ /index.php?$args;
}

thinking that the first line would modify the URI, taking away the /web/ part so that in the next line the changed uri would be fed to the try_files command.

But this only resulted in 404s.

Can someone enlighten me on where I am going wrong with this?

Thanks a lot in advance,

Johannes


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

signature.asc (523 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Moving Joomla from subdir to root -> rewrite / redirect problem

Francis Daly
On Fri, Dec 15, 2017 at 03:25:20PM +0100, Johannes Rohr wrote:

Hi there,

I do not know joomla; and I do not have a direct answer for you.

But some parts of your question seem unclear to me.

Perhaps they are also unclear to someone who otherwise could give an answer.

If you can clarify them, maybe that other person can help.

> in order to have prettier URLs I have decided to move my joomla from
> /web/ to /, but I want old URLs to transparently redirect.

When you say "transparently redirect", can you give an example of what
you mean?

As in: when I request /web/something, should I get a http redirect so
that I next request /something; or should I get something else?

> location /web {
>   try_files $uri $uri/ /index.php?$args;
>  }
>
> but this obviously did not work as the arguments passed to index.php
> still contain the /web/   part  , which would have to go.

Without knowing joomla... why is this obvious? Why would arguments to
index.php include /web/?

And: what, specifically, did you do on the joomla side, to move it from
/web/ to /?

> So I tried instead:
>
> location / {
>     rewrite ^/web/(.*)$ /$1;
>   try_files $uri $uri/ /index.php?$args;
> }
>
> thinking that the first line would modify the URI, taking away the /web/
> part so that in the next line the changed uri would be fed to the
> try_files command.

Yes. It does not do anything about "arguments to index.php", though,
which was your reported problem with the first attempt.

(I'd probably include "break" on the rewrite line; but it might depend
on what the rest of your config says.)

> But this only resulted in 404s.

Can you show one request that you make, and describe how you would like
it to be handled?

Cheers,

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

Re: Moving Joomla from subdir to root -> rewrite / redirect problem

Johannes Rohr


Am 18.12.2017 um 20:36 schrieb Francis Daly:

> On Fri, Dec 15, 2017 at 03:25:20PM +0100, Johannes Rohr wrote:
>
> Hi there,
>
> I do not know joomla; and I do not have a direct answer for you.
>
> But some parts of your question seem unclear to me.
>
> Perhaps they are also unclear to someone who otherwise could give an answer.
>
> If you can clarify them, maybe that other person can help.
I will try my best.
>> in order to have prettier URLs I have decided to move my joomla from
>> /web/ to /, but I want old URLs to transparently redirect.
> When you say "transparently redirect", can you give an example of what
> you mean?
>
> As in: when I request /web/something, should I get a http redirect so
> that I next request /something; or should I get something else?
Yes, sure, that's precisely what I want to achive

>> location /web {
>>   try_files $uri $uri/ /index.php?$args;
>>  }
>>
>> but this obviously did not work as the arguments passed to index.php
>> still contain the /web/   part  , which would have to go.
> Without knowing joomla... why is this obvious? Why would arguments to
> index.php include /web/?
Because that is the subdir where it is currently installed. (which I
want to change)

>
> And: what, specifically, did you do on the joomla side, to move it from
> /web/ to /?

Nothing really besides moving the files, as Joomal adjusts all the
variables on the fly. And Joomla is perfectly happy with that, only the
redirect does not work

>> So I tried instead:
>>
>> location / {
>>     rewrite ^/web/(.*)$ /$1;
>>   try_files $uri $uri/ /index.php?$args;
>> }
>>
>> thinking that the first line would modify the URI, taking away the /web/
>> part so that in the next line the changed uri would be fed to the
>> try_files command.
> Yes. It does not do anything about "arguments to index.php", though,
> which was your reported problem with the first attempt.
>
> (I'd probably include "break" on the rewrite line; but it might depend
> on what the rest of your config says.)
Why? Doesn't "break" mean that processing is ended after the current
line? What I want is that the re-written url is then fed to the
try_files directive below, which calls joomla's index.php

>> But this only resulted in 404s.
> Can you show one request that you make, and describe how you would like
> it to be handled?


So now I am trying this:

     # Support Clean (aka Search Engine Friendly) URLs
        location / {
if ($request_uri ~* "^/web/(.*/)$") {
        return 301 $1;
    }

                try_files $uri $uri/ /index.php?$args;
        }


I kind of hoped this would would catch each request that contails /web/
and rewrite it into something with /web/ stripped and then feed it to
try_files directive below.

But in the nginx log I see:

2017/12/22 19:43:55 [error] 21149#21149: *19151503 FastCGI sent in
stderr: "Unable to open primary script:
/var/www/chrooted/jrweb/htdocs/www.infoe.de/web/index.php (No such file
or directory)" while reading response header from upstream, client:
65.19.138.35, server: www.infoe.de, request: "GET
/web/index.php?format=feed&type=rss HTTP/1.1", upstream:
"fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "www.infoe.de"

So, somehow, the rewrite is not happening, nginx still looks for
/web/index.php instead of /index.php

Of course, the return the browser gets is a 404

I find this topic so confusing (and reading the official nginx docs does
not make the confusion go away), would be grateful if anyone could
enlighten me!

Cheers,

Johannes


>
> Cheers,
>
> f



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

signature.asc (523 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Moving Joomla from subdir to root -> rewrite / redirect problem

Francis Daly
On Fri, Dec 22, 2017 at 07:57:16PM +0100, Johannes Rohr wrote:
> Am 18.12.2017 um 20:36 schrieb Francis Daly:
> > On Fri, Dec 15, 2017 at 03:25:20PM +0100, Johannes Rohr wrote:

Hi there,

> > As in: when I request /web/something, should I get a http redirect so
> > that I next request /something; or should I get something else?
> Yes, sure, that's precisely what I want to achive

Add

  location ^~ /web/ { rewrite /web(/.*) $1 permanent; }

to your previously-working nginx config. Report if anything now does
not work as you want it to.

The rest of this mail is background information; the above config addition
might be all that you need.

> > Without knowing joomla... why is this obvious? Why would arguments to
> > index.php include /web/?
> Because that is the subdir where it is currently installed. (which I
> want to change)

Ok. I still don't understand why arguments to index.php would include
/web/, but I guess that I don't need to understand it.

> >> location / {
> >>     rewrite ^/web/(.*)$ /$1;
> >>   try_files $uri $uri/ /index.php?$args;
> >> }
> >>
> >> thinking that the first line would modify the URI, taking away the /web/
> >> part so that in the next line the changed uri would be fed to the
> >> try_files command.

http://nginx.org/r/rewrite

and

http://nginx.org/r/location

Your "rewrite" will start with /web/something and end with /something,
and then nginx will find the correct location{} to handle /something,
which may or may not be this location{}, depending on what other
location{}s are defined.

> > (I'd probably include "break" on the rewrite line; but it might depend
> > on what the rest of your config says.)
> Why? Doesn't "break" mean that processing is ended after the current
> line? What I want is that the re-written url is then fed to the
> try_files directive below, which calls joomla's index.php

"break" says "stops processing the current set of ngx_http_rewrite_module
directives" with "If a directive is specified inside the location,
further processing of the request continues in this location.". And you
want the processing to be in this location, and not start the normal
search for the correct location for the new subrequest /something.

>         location / {
> if ($request_uri ~* "^/web/(.*/)$") {
>         return 301 $1;
>     }
>
>                 try_files $uri $uri/ /index.php?$args;
>         }
>
>
> I kind of hoped this would would catch each request that contails /web/
> and rewrite it into something with /web/ stripped and then feed it to
> try_files directive below.

If this location{} is used for /web/something, then the browser gets a
301 redirect and makes a whole new request for /something.

If this location{} is used for /something, then the try_files will be used.

> But in the nginx log I see:
>
> 2017/12/22 19:43:55 [error] 21149#21149: *19151503 FastCGI sent in
> stderr: "Unable to open primary script:
> /var/www/chrooted/jrweb/htdocs/www.infoe.de/web/index.php (No such file
> or directory)" while reading response header from upstream, client:
> 65.19.138.35, server: www.infoe.de, request: "GET
> /web/index.php?format=feed&type=rss HTTP/1.1", upstream:
> "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "www.infoe.de"
>
> So, somehow, the rewrite is not happening, nginx still looks for
> /web/index.php instead of /index.php

Is the location{} above used for the request /web/index.php?

I suspect you may have another location like

  location ~ php

that means that your "location /" will not be used for the request
/web/index.php

> I find this topic so confusing (and reading the official nginx docs does
> not make the confusion go away), would be grateful if anyone could
> enlighten me!

In nginx, one request is handled in one location.

Under certain circumstances, including "rewrite", one http request might
lead to an nginx request followed by an nginx subrequest; that subrequest,
in general, counts as a new request to nginx.

Something like "grep location nginx.conf" might show all of the
location{}s that are defined for this server{}; given that, and the
documented rules, it should be possible to see which one location a
specific request is handled in.

(Note: "rewrite"-module directives can change the logic, so it is not
necessarily as straightforward as described there.)

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx