Score:2

Create "shortcut" for GET parameters redirection in Apache2

cn flag

I try to create a config in apache server that will transfer the url

foo.bar/baz/hq

to

foo.bar/baz#a=1;b=2;c=3

where baz can be anything. I tried this (see https://stackoverflow.com/a/8606198/3475778)

RewriteRule ^([A-Za-z0-9-]+)/hq$ $1#foo=bar [NC,NE,R=302]

but somehow the redirect doesnt seem to work and I do not know why.

My conf file looks like this - sorry it is a bit messy, I am not a sysadmin.

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName xxx
    Redirect    302 /   http://xxx/


SSLCertificateFile /etc/letsencrypt/live/xxx/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/xxx/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName xxx
    ErrorLog /var/log/apache2/xxx.net-error.log

    RewriteEngine on
    RewriteRule ^([A-Za-z0-9-]+)/hq$ $1#foo=bar [NC,NE,R=302]

    ProxyPass "/" "http://localhost:8000/"
    ProxyPassReverse "/" "http://localhost:8000/"
    ProxyPass "/http-bind" "http://localhost:8000/http-bind/"
    ProxyPassReverse "/http-bind" "http://localhost:8000/http-bind"
    RewriteEngine on
# Some rewrite rules in this file were disabled on your HTTPS site,
# because they have the potential to create redirection loops.

        RewriteCond %{HTTP:Upgrade} websocket [NC]
        RewriteCond %{HTTP:Connection} upgrade [NC]
        RewriteRule ^/?(.*) "ws://localhost:8000/$1" [P,L]


SSLCertificateFile /etc/letsencrypt/live/xxx/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/xxx/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
user10489 avatar
nc flag
Why is there a comment there about disabling rewrite rules due toa redirection loop?
capitalg avatar
cn flag
@user10489 that file/section was auto generated from certbot
Score:0
nc flag

The part of the url after # is not transmitted to the webserver in a GET, so it has no way of knowing if the redirect has already been done or not. This would turn into a recursive redirect with no way to break the loop.

If you want to do something like this, it needs to be done client side, possibly in javascript, where the anchor part of the URL can be accessed.

You could send someone to a link with a # in it from a server side app, but this would only be useful for the links leading to this page (including links on the same page). If you want to do it from the same page, you'd still need something client side to make it happen.

capitalg avatar
cn flag
Thanks for your response but the linked SE answer (https://stackoverflow.com/a/8606198/3475778) mentioned that redirects to `#` are possible via redirecting which is exactly what I want to do here. The redirection from above should not be recursive as only with the appendix`*/hq` you would get a redirect - this URL is accessed only via manual append to an existing URL. But somehow even if I change it to something w/o `#` i still can't get the redirection to work and I can't figure out why it does not work.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.