'Redirect to previous page after login from AuthFormLoginLocation [Apache2 configuration]

As i writed in topic, Im looking for way to redirect user after login via AuthForm to proper destination.

It should looks smth like that:

User typing in address bar addres domain.com/examplepage/something -> Server redirects him to domain.com/login.html (Thats working fine) -> After login server redirects him back to domain.com/examplepage/something or whatever the user had typed to address bar befor (That isnt working :( )

Pleas help, let me know about Your idea or ready solution

Below apache configuration (Apache v2.4.10 installed on Debian (Raspbian - Jessie)

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined


    <Directory "/var/www/html">
            AuthFormProvider file
            AuthType form
            AuthName "Reserved Area"
            AuthFormLoginRequiredLocation /login.html

            Session On
            SessionCookieName session path=/
            require valid-user

            AuthUserFile /etc/apache2/.htpasswd
    </Directory>

    <Location "/login.html">
            Order allow,deny
            Allow from all
            Require all granted
    </Location>

    Alias /open/ "/var/www/open/"
    <Directory "/var/www/open/">
            Order allow,deny
            Allow from all
            Require all granted
    </Directory>

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf

Tell me if You need more information

Thanks for help :)

Sorry for English ;p



Solution 1:[1]

Alright I think I've done it. 100% Apache implementation for returning user to previous page if login was successful.

Preparations:

  • I use the standalone login scheme, as described in the reference.
  • Form action for login is /dologin.
  • The area protected by login is /test/.

Now add three line (I'm only listing the lines to be added)

<Directory var/www/html/test>
  # Set a fake query parameter for redirecting if auth successful
  AuthFormLoginRequiredLocation /login.html?req=%{REQUEST_URI}
</Directory>

<Location /dologin>
  # Put this at the bottom of the block, maybe.
  # Extract request before interrupt and redirect
  SetEnvIf Referer ^.*req=(.*)&?$ req=$1
  AuthFormLoginSuccessLocation %{ENV:req}
</Location>

Done. Spent two days to figure this out. They don't tell you that different modules has different names for the same environment variables. They also don't tell you that not all environment variables are equally accessible by all modules. Yep, lots of WTF moment.

Additional references: 1, 2, 3

Solution 2:[2]

Only AuthFormLoginRequiredLocation /login.html?req=%{REQUEST_URI}?%{QUERY_STRING} is enough to get the page including get parameters.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yifeng Mu
Solution 2 jmoerdyk