I have some domain redirects that point to a page on my server, and they stick a trailing slash after the filename but before the query string. So this:


…becomes this:


…instead of this:


So how do I drop that middle-of-URL slash while preserving the query string? I’ve tried some .htaccess approaches but nothing seems to work. The most recent attempt was:

RewriteRule ^(.+)html/(.+)$ /$1html$2 [R=301,L]

[UPDATE: That rule didn’t work, so I disabled it, and now there are no rewrites in effect.] I’m not very experienced with mod_rewrite, as you might have guessed. Any help greatly appreciated!

  • 3
    That rewrite rule doesn't match the URL you posted. Please post the actual rewrite rules being used. – Michael Hampton Feb 13 '13 at 4:42
  • I’m not using any rewrite rules right now, as I disabled that one after it didn’t work, and there are no others (except for the domain redirect at the registrar, which appears to be where the slash comes from). – Eric A. Meyer Feb 13 '13 at 15:45

Apache's mod_rewrite does not match the query string in its RewriteRules. You don't even need QSA if you are not redirecting to a URL with a query string specified. I tested this locally on Apache 2.2.22 (OS X Lion's native version) and it worked.

RewriteRule ^(.+)html/$ /$1html [R=301,L]

EDIT: If you're putting the .htaccess in the gmap directory, you'll have to use the following in it:

RewriteEngine On
RewriteBase /eric/tools/gmap/
RewriteRule ^(.+)html/$ $1html [R=301,L]
  • Weird: that resolved to meyerweb.com/… . (Still does, as I write this.) – Eric A. Meyer Feb 13 '13 at 16:15
  • Is your .htaccess in the gmap directory? – Steven Merrill Feb 13 '13 at 16:16
  • Yes, it is. Currently the only rule in that file is the one you posted. – Eric A. Meyer Feb 13 '13 at 16:18
  • Check the updated answer for a fix. – Steven Merrill Feb 13 '13 at 16:21

Use the QSA ("Query String Append") flag instead of trying to parse it out yourself. Something like:

RewriteRule ^(.+)html/(.+)$ /$1html [R=301,L,QSA]

  • Hm, doesn’t seem to work. Here’s my .htaccess: RewriteEngine On RewriteRule ^(.+)html/(.+)$ /$1html [R=301,L,QSA] The slash still doesn’t get dropped. – Eric A. Meyer Feb 13 '13 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.