Forum Moderators: phranque

Message Too Old, No Replies

.htaccess subfolder rewrite, add trailing slash and preserve '%'

         

metal450

3:10 am on Mar 25, 2016 (gmt 0)

10+ Year Member



Hi all,

This question builds on this thread ([webmasterworld.com ]), which is now closed for replies.

I'm trying to move the files for a given subdomain into a subfolder. I'd like to rewrite the new path with .htaccess, while not exposing the "true" server path, even when a folder URL is entered without a trailing slash. I went with the following solution, adapted from the thread linked above:


# Externally redirect to add trailing slash to extensionless URLs if missing
RewriteCond %{HTTP_HOST} ^test.example.com$ [NC]
RewriteRule ^(([^/]+/)*[^./]+)$ http://test.example.com/$1/ [L,R=301]

# Internally rewrite all requests to subfolder
RewriteCond %{HTTP_HOST} ^test.example.com
RewriteRule ^(.*)$ /subfolder/$1 [L,NC,QSA]

This works great - with one exception. If I enter a URL like:
http://test.example.com/my-script?xdm_e=https%3A%2F%2Fjk.test

it gets redirected to
http://test.example.com/my-script/?xdm_e=https%253A%252F%252Fjk.test

Note in addition to adding the trailing slash (as desired), percent symbols have also been expanded to "%25". This is what I'd like to avoid. After lots of googling, I thought this might have something to do with the "B" flag, but adding that to the RewriteRule just made things worse ("my-script" got changed to "my%252dscript", and the percents were still altered).

Any pointers would be greatly appreciated - I've been tinkering with this for hours, but am pretty unfamiliar with mod_rewrite.

metal450

3:59 am on Mar 25, 2016 (gmt 0)

10+ Year Member



Figured it out shortly after writing up this post. For anyone else who finds this, the answer was the [NE] flag:


RewriteCond %{HTTP_HOST} ^test.example.com$ [NC]
RewriteRule ^(([^/]+/)*[^./]+)$ http://test.example.com/$1/ [L,NE,R=301]

lucy24

6:20 am on Mar 25, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for posting. It wouldn't have occurred to me that [NE] also applies to the query string ... although it does explain the occasional request with nested queries ending up as %25252525. In fact I think my log-wrangling function includes a %25 > % and repeat until it rinses clean for this very reason.