0

Sometime years ago ALL of our Apache config files changed to have whitespace all over the place (see below, no idea who did it). Since I am upgrading from 2.2 to 2.4 this makes it tricky to find and replace all the required config changes.

I thought to get rid of the whitespace in the middle but keep the whitesapce at the front. I know how to replace at the beginning, end and everywhere. I even consulted a few books (e.g. Jeff Friedl's regexp) but I cannot get my head around this - not even sure whether this is possible.

  <Directory            "THEDIRECTORY">
    <LimitExcept        GET POST>
      deny              from all
    </LimitExcept>
    Options             -Indexes -MultiViews
    AllowOverride       All
    Order               allow,deny
    Allow               from all
  </Directory>

What I would like is this:

  <Directory "THEDIRECTORY">
    <LimitExcept GET POST>
      deny from all
    </LimitExcept>
    Options -Indexes -MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>

So I can easily search for and replace the config changes for apache 2.4

I thought coming from the end

s/\s+(.*)\s+(.*)\s+$/$1$2/g

but this does not work for a number of reasons including that the number of replacements change, they are a non fixed number.

Help please, I am clueless.

Jobst
  • 529
  • 3
  • 12

2 Answers2

1

You can use this regex to match any horizontal whitespace two or more at a time and replace it with a single space.

(?<=\S)\h{2,}(?=\S)

This regex ensures, only space will be matched that is surrounded by some non-space character using \S

Regex Demo

In case \h is not supported in your regex, you can use just a space instead of \h and write your regex as,

(?<=\S) {2,}(?=\S)

Demo using space only

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
  • 1
    Full explanation about the look-ahead and look-behind found at https://stackoverflow.com/questions/2973436/regex-lookahead-lookbehind-and-atomic-groups – Jobst Apr 19 '19 at 02:10
0

Ensure your add the following line to your .htaccess or apache conf file dire tory tag if it’s not already there:

RewriteEngine On

Then below add

RewriteCond %{REQUEST_URI} \s

RewriteRule .* https://DOMAIN_NAME/ [R=301,L]

Replacing DOMAIN_NAME.

GingaWRATH
  • 98
  • 4