regexapachehttpmod-rewriteibmhttpserver

RewriteRule to remove superfluous single "?" in URL


I am using IBM HTTP server configuration file to rewrite a URL redirected from CDN.

For some reason the URL comes with a superfluous single question mark even when there are no any query string. For example:

/index.html?

I'm in the process of making the 301 redirect for this. I want to remove the single "?" from the url but keep it if there is any query string.

Here's what I tried but it doesn't work:

RewriteRule ^/index.html? http://localhost/index.html [L,R=301]

update: I tried this rule with correct regular expression but it never be triggered either.

RewriteRule ^/index.html\?$ http://localhost/index.html [L,R=301]

I tried to write another rule to rewrite "index.html" to "test.html" and I input "index.html?" in browser, it redirected me to "test.html?" but not "index.html".


Solution

  • You need to use a trick since RewriteRule implicitly matches against just the path component of the URL. The trick is looking at the unparsed original request line:

    RewriteEngine ON
    # literal ? followed by un-encoded space.  
    RewriteCond %{THE_REQUEST} "\? "
    # Ironically the ? here means drop any query string.
    RewriteRule ^/index.html /index.html? [R=301]