Search code examples
apache.htaccesshttp-redirect

Apache Redirect // to no //


An email went out with the wrong link (https://www.digitalmarketer.com/digital-marketing/content-marketing-strategy//) and we need to redirect the // to (https://www.digitalmarketer.com/digital-marketing/content-marketing-strategy/) but no matter what I try, the redirect isn't working.

They also want it to be redirected to always have https:///www at the beginning and to never have index.html at the end, so already in the .htaccess file I have:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteRule ^content\-marketing\-strategy/index\.html$ /digital-marketing/content-marketing-strategy/? [L,R=301]

I've tried adding a new RewriteRule, but this won't work:

RewriteRule ^content\-marketing\-strategy//$ /digital-marketing/content-marketing-strategy/? [L,R=301]

I'm very new to Apache and redirects so any help is much appreciated! Thank you!

Edit: Of note, this is in an .htaccess file inside of the digital-marketing folder (https://www.digitalmarketer.com/digital-marketing/.htaccess) which was done so all the above rules would only apply to the digital-marketing folder.


Solution

  • You can use insert rule at the end of your other rules to strip multiple // into /:

    RewriteCond %{THE_REQUEST} //
    RewriteRule ^.*$ /digital-marketing/$0 [R=301,L,NE]
    

    Apache automatically strips down multiple // into one inside the pattern for RewriteRule thus captured value $0 will have all // converted into /