skip to Main Content

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”.

2

Answers


  1. Question-mark is a Regular Expression special character, which means “the preceding character is optional”. Your rule is actually matching index.htm or index.html.

    Instead, try putting the question-mark in a “character class”. This seems to be working for me:

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

    ($ to signify end-of-string, like ^ signifies start-of-string)

    See http://publib.boulder.ibm.com/httpserv/manual60/mod/mod_rewrite.html (for your version of Apache, which is not the latest)

    Note from our earlier attempts, escaping the question-mark doesn’t seem to work.

    Also, I’d push the CDN on why that question-mark is being sent. This doesn’t seem a normal pattern.

    Login or Signup to reply.
  2. 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]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search