Trey Trey - 3 months ago 19
Apache Configuration Question

Apache Replace two connected characters to one in url

With apache in the htaccess I would like to replace all periods in the url only when there is an underscore after it.

I want

example.com?index.php/blog/low_vs._high
to redirect to
example.com?index.php/blog/low_vs_high


Can someone tell me how to do this?
Here is what I have, not sure where to go from here or how to properly debug. Thanks.

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(.*)\._(.*)$
RewriteRule ^(.*)\.(.*)$ /$1$2 [L,R=301]

Answer Source

You can use this rule:

# execute when there are multiple occurrences of ._
RewriteCond %{REQUEST_URI} ^/(.*)\.(_.*\._.*)$
RewriteRule ^ /%1%2 [L]

# execute when there is only one occurrence of ._
RewriteCond %{REQUEST_URI} ^/(.*)\.(_.*)$
RewriteRule ^ /%1%2 [L,NE,R=301]