← ServerPilot Docs

How to Use Apache to Replace Strings in Responses

If you need to replace strings in the output of your PHP scripts before they are sent to the browser, you can use Apache's string substitution features.

To have Apache perform a string replacement, add the following to the app's .htaccess file:

AddOutputFilterByType SUBSTITUTE text/html
Substitute s/sitename.example.com/sitename.com/ni

That would replace every instance of sitename.example.com with sitename.com before any page is sent to the browser. The flags at the end (ni) tell Apache to use a fixed string rather than a regular expression (n) and to do a case-insensitive search (i).

To perform more than one substitution, you only need to add more Substitute directives. You do not need to repeat the line AddOutputFilterByType SUBSTITUTE text/html. For example, two substitutions would look like:

AddOutputFilterByType SUBSTITUTE text/html
Substitute s/sitename.example.com/sitename.com/ni
Substitute s/othersite.example.com/othersite.com/ni

You can find more information in the Apache documentation for mod_substitute.

Launch your first site in 5 minutes