skip to Main Content

I need to handle one query parameter specifically.
The url can look like the following:

https://example.com/?app=egov&map=1337
https://example.com/?app=egov&map=1337&showoptions=true
https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorld

The redirect should point to

https://example.com/contextName/resources/apps/egov/index.html?map=1337
https://example.com/contextName/resources/apps/egov/index.html?map=1337&showoptions=true
https://example.com/contextName/resources/apps/egov/index.html?map=1337&anotherparam=helloWorld

As you can see, the app-Parameter needs to be used as an URL-part; the others need to be used like traditional query-params.
I’ve figured out, how to implement the first part, where https://example.com/?app=egov points to https://example.com/contextName/resources/apps/egov/index.html using the following

RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^app=(.+)
RewriteRule ^/$ "/contextName/resources/apps/%1/index.html" [R,L,QSA]

but I’m struggeling how to realize the correct handling of the other params.

2

Answers


  1. With your shown samples, please try following htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    ####Apache documentation link https://httpd.apache.org/docs/trunk/rewrite/remapping.html for more info.
    ##Internal rewrite rule for URL https://example.com/?app=egov&map=1337
    RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(d+)$ [NC]
    RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2 [L]
    
    ##Internal rewrite rule for URL https://example.com/?app=egov&map=1337&showoptions=true
    RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(d+)&showoptions=(.*)$ [NC]
    RewriteRule ^/?$  contextName/resources/apps/%1/index.html?map=%2&showoptions=%3 [L]
    
    ##Internal rewrite rule for https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorl
    RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(d+)&showoptions=([^&]*)&anotherparam=(.*)$ [NC]
    RewriteRule ^/?$  contextName/resources/apps/%1/index.html?map=%2&anotherparam=%4 [L]
    

    All of your URLs uri part is NULL, so rules are written based on that.

    Login or Signup to reply.
  2. All these combination of query string can be handled by a single rule loke this:

    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} ^app=([w-]+)(?:&(.*))?$ [NC]
    RewriteRule ^$ contextName/resources/apps/%1/index.html?%2 [L]
    

    RewriteCond matches app query parameter and captures it in %1 while rest of the query string after & is captured in %2.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search