2

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.

3 Answers3

1

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 /

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    This made it redirect to (https://www.digitalmarketer.com/content-marketing-strategy/) but I added in `RewriteRule ^.*$ /digital-marketing/$0 [R=301,L,NE]` and that seems to be working! Thanks! – Monica Szabo Dec 04 '17 at 18:17
0

You can write a wildcard expression to remove trailing slashes. The below will match any HTTP or HTTPS URL that trails in a forward slash, and remove all trailing forward slashes from that URL:

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

And more using 301 redirects, see more here: Best Practice: 301 Redirect HTTP to HTTPS (Standard Domain)

Good luck!

Lee Warnock
  • 714
  • 7
  • 8
  • Hmmm this caused (https://www.digitalmarketer.com/digital-marketing/content-marketing-strategy//) to be redirected to (https://www.digitalmarketer.com/websites/digitalmarketer.com/public_html/digital-marketing/content-marketing-strategy). Maybe it's a placement issue though, I'll try moving it around. – Monica Szabo Dec 04 '17 at 17:57
0

I see nothing in the way that the rule is written that would make it not rewrite. However you have multiple rules with the L flag that might stop processing on the rewrite at an earlier point than you are looking for. From the documentation

The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. (https://httpd.apache.org/docs/current/rewrite/flags.html).

You can try this page out http://htaccess.mwl.be/ to test all your rules together. You might have to rewrite them a bit to work with that page, it's not aware of the level your .htaccess file is at so you will have to rewrite all your rules to trigger from the root for example: RewriteRule ^digital\-marketing/content\-marketing\-strategy//$ /digital-marketing/content-marketing-strategy/? [L,R=301]

belshire
  • 51
  • 4