1

I have a website that is being redirected from an old host. I don't have the ability to change the redirect of the old host.

The issue is that the redirect is appending a trailing slash when it's not appropriate. Example: http://example.com/picture.jpg/ or http://example.com/page.html/.

That is causing my server to return a 404.

Ideally, I would fix the redirect from the old site but cannot.

Is there a way to remove the trailing slash at the end of the URL? This is a site that is not complex (straight simple html, no scripting languages, etc.).

I looked and couldn't find an existing solution.

MrWhite
  • 12,647
  • 4
  • 29
  • 41
Josh
  • 13
  • 2
  • Where are you using these directives? `.htaccess`? server config? `` container? Are the requested URLs always a single path depth, as in your examples? – MrWhite Jan 29 '20 at 15:17

1 Answers1

0

You could do something like the following to remove the trailing slash from any URL that ends with a file extension (.jpg, .html, .png, etc.)

RewriteEngine On
RewriteRule ^/?(.+?\.[a-z]{2,4})/$ /$1 [R=302,L]

Change the 302 (temporary) to 301 (permanent) only once you have tested that it works OK (avoid caching issues).

UPDATE: http redirected to https, and https rewrote the funny trailing slash.

If these links are all in-bound to HTTP only (not HTTPS) then it would make sense to perform this redirect only in the HTTP vHost instead and redirect straight to HTTPS as well. For example:

RewriteRule ^/(.+?\.[a-z]{2,4})/$ https://example.com/$1 [R=302,L]

(Although granted, if it is in the vHost for HTTPS then it will catch all requests.)

MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • 1
    This was the answer, though it was SLIGHTLY more complicated. I was using this within the VirtualHost definitions, and I was also redirecting http to https (which was working fine). I added the above lines to the SSL/443 VirtualHost information and it took right away. I have the https and http for this site configured in separate files. So, I added it strictly to the https configuration file and it worked great. http redirected to https, and https rewrote the funny trailing slash. Thank you so much! – Josh Jan 30 '20 at 03:23
  • If the links are inbound to the HTTP version only then it would perhaps make sense to include this redirect in the HTTP vHost instead - and redirect to HTTPS at the same time? One less redirect. (I've updated my answer.) – MrWhite Jan 30 '20 at 03:40
  • 1
    In this case, I can't **assume** they'd only be http, but it makes sense in the http rewrite rules to do both at the same time. So I put the single rewrite rule in the http configuration file and put the simpler, original rule in the https config. Thank you for all of your help! – Josh Feb 07 '20 at 19:34