# Buggy code
def _write_out_redirect(self, ssl_vhost, text):
    # This is the default name
    redirect_filename = "le-redirect.conf"

    # See if a more appropriate name can be applied
    if ssl_vhost.name is not None:
        # make sure servername doesn't exceed filename length restriction
        if len(ssl_vhost.name) < (255 - (len(redirect_filename) + 1)):
            redirect_filename = "le-redirect-%s.conf" % ssl_vhost.name

    redirect_filepath = os.path.join(self.conf("vhost-root"), redirect_filename)

    # Register the new file that will be created
    # Note: always register the creation before writing to ensure file will
    # be removed in case of unexpected program exit
    self.reverter.register_file_creation(False, redirect_filepath)

    # Write out file
    with open(redirect_filepath, "w") as redirect_file:
        redirect_file.write(text)
    logger.info("Created redirect file: %s", redirect_filename)

    return redirect_filepath


# Traceback (PluginError)
Cannot find a cert or key directive in /files/etc/apache2/sites-available/default-ssl.conf/IfModule/VirtualHost. VirtualHost was not modified
Encountered exception:
Traceback (most recent call last):
File "/home/bmw/src/certbot/certbot/client.py", line 439, in deploy_certificate
fullchain_path=fullchain_path)
File "/home/bmw/src/certbot/certbot-apache/certbot_apache/configurator.py", line 269, in deploy_cert
"Unable to find cert and/or key directives")
PluginError: Unable to find cert and/or key directives

Calling registered functions
Reporting to user: Unable to install the certificate
Exiting abnormally:
Traceback (most recent call last):
File "/usr/local/bin/certbot", line 11, in <module>
load_entry_point('certbot', 'console_scripts', 'certbot')()
File "/home/bmw/src/certbot/certbot/main.py", line 743, in main
return config.func(config, plugins)
File "/home/bmw/src/certbot/certbot/main.py", line 604, in run
_install_cert(config, le_client, domains, new_lineage)
File "/home/bmw/src/certbot/certbot/main.py", line 469, in _install_cert
path_provider.cert_path, path_provider.chain_path, path_provider.fullchain_path)
File "/home/bmw/src/certbot/certbot/client.py", line 439, in deploy_certificate
fullchain_path=fullchain_path)
File "/home/bmw/src/certbot/certbot-apache/certbot_apache/configurator.py", line 269, in deploy_cert
"Unable to find cert and/or key directives")
PluginError: Unable to find cert and/or key directives

# Fixed code
def _write_out_redirect(self, ssl_vhost, text):
    # This is the default name
    redirect_filename = "le-redirect.conf"

    # See if a more appropriate name can be applied
    if ssl_vhost.name is not None:
        # make sure servername doesn't exceed filename length restriction
        if len(ssl_vhost.name) < (255 - (len(redirect_filename) + 1)):
            redirect_filename = "le-redirect-%s.conf" % ssl_vhost.name

    redirect_filepath = os.path.join(self.vhostroot, redirect_filename)

    # Register the new file that will be created
    # Note: always register the creation before writing to ensure file will
    # be removed in case of unexpected program exit
    self.reverter.register_file_creation(False, redirect_filepath)

    # Write out file
    with open(redirect_filepath, "w") as redirect_file:
        redirect_file.write(text)

    # Add new include to configuration if it doesn't exist yet
    if not self.parser.parsed_in_current(redirect_filepath):
        self.parser.parse_file(redirect_filepath)

    logger.info("Created redirect file: %s", redirect_filename)

    return redirect_filepath