0

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

dbreaux
  • 4,982
  • 1
  • 25
  • 64
Hi_World
  • 7
  • 1
  • 6
  • Which version of Apache? I wonder if you have access to the `QSD` option, and if that would do anything. (My IBM HTTP Server with WebSphere 8.5.5 does not have that option.) – dbreaux Nov 29 '18 at 14:53

2 Answers2

1

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]
covener
  • 17,402
  • 2
  • 31
  • 45
0

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.

dbreaux
  • 4,982
  • 1
  • 25
  • 64
  • Tried but it never be triggered. 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". – Hi_World Nov 29 '18 at 11:58
  • Yeah, I'm trying versions of this with my IHS too, and haven't yet found an incantation that does what you're requesting. – dbreaux Nov 29 '18 at 15:52
  • I am using the same version of IBM WebSphere with you, seems ihs config is not a solution. Thanks for helping. – Hi_World Nov 30 '18 at 01:38
  • @Hi_World see updated answer, which seemed to work for me. – dbreaux Dec 04 '18 at 23:28
  • The thing RewriteRule compares the first arg against doesn't have the query string in it, including the ? delimiter. This would only work if the first ? was URL-encoded and not actually the start of the query string. – covener Dec 05 '18 at 14:35
  • @covener Shrug, it did work for me, best I can tell, typing the URL directly in my browser. But thanks for your, undoubtedly better, answer. – dbreaux Dec 05 '18 at 15:55
  • Genius! Thank you guys!! – Hi_World Dec 06 '18 at 09:05