Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

Moving From htaccess to .conf includes

Discussion in 'EasyApache' started by Anthony Parsons, Jan 15, 2017.

  1. Anthony Parsons

    Anthony Parsons Active Member

    Aug 4, 2016
    Likes Received:
    Trophy Points:
    cPanel Access Level:
    Root Administrator
    Lots of research later, the overall conclusion is that even Apache don't recommend using .htaccess when you control the server, THUS my conundrum is that I want to move away from htaccess to virtual host includes, but I don't really understand the actual include requirements.

    What I understand

    Digging through the httpd.conf file, I understand where each file is included for each virtual host. Both port 80 and 443 virtual hosts.

    I assume one should cover both angles for best practice???

    I must create the file structure using:
    mkdir -p /etc/apache2/conf.d/userdata/std/2_4/pbn001/


    mkdir -p /etc/apache2/conf.d/userdata/ssl/2_4/pbn001/

    And I put rewrite.conf into each one (replicating).

    What I don't understand

    Is it ok that the directory created and file rewrite.conf within it, are created using root?

    Do I just paste my .htaccess into it? I tried that, and it failed.

    Or do I need to still create a full structure as an include, even though its an include within the existing virtual host?


    <VirtualHost [2600:3c03:0000:0000:0000:0000:00da:b005]:80>
        <Directory /home/example/public_html/>
    # BEGIN WP Rocket v2.9.1
    # Use UTF-8 encoding for anything served text/plain or text/html
    AddDefaultCharset UTF-8
    # Force UTF-8 for a number of file formats
    <IfModule mod_mime.c>
    AddCharset UTF-8 .atom .css .js .json .rss .vtt .xml
    # FileETag None is not enough for every server.
    <IfModule mod_headers.c>
    Header unset ETag
    # Since we’re sending far-future expires, we don’t need ETags for static content.
    FileETag None
    <IfModule mod_alias.c>
    <FilesMatch "\.(html|htm|rtf|rtx|txt|xsd|xsl|xml)$">
    <IfModule mod_headers.c>
    Header set X-Powered-By "WP Rocket/2.9.1"
    Header unset Pragma
    Header append Cache-Control "public"
    Header unset Last-Modified
    <FilesMatch "\.(css|htc|js|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$">
    <IfModule mod_headers.c>
    Header unset Pragma
    Header append Cache-Control "public"
    # Expires headers (for better cache control)
    <IfModule mod_expires.c>
    ExpiresActive on
    # Perhaps better to whitelist expires rules? Perhaps.
    ExpiresDefault                          "access plus 1 month"
    # cache.appcache needs re-requests in FF 3.6 (thanks Remy ~Introducing HTML5)
    ExpiresByType text/cache-manifest       "access plus 0 seconds"
    # Your document html
    ExpiresByType text/html                 "access plus 0 seconds"
    # Data
    ExpiresByType text/xml                  "access plus 0 seconds"
    ExpiresByType application/xml           "access plus 0 seconds"
    ExpiresByType application/json          "access plus 0 seconds"
    # Feed
    ExpiresByType application/rss+xml       "access plus 1 hour"
    ExpiresByType application/atom+xml      "access plus 1 hour"
    # Favicon (cannot be renamed)
    ExpiresByType image/x-icon              "access plus 1 week"
    # Media: images, video, audio
    ExpiresByType image/gif                 "access plus 1 month"
    ExpiresByType image/png                 "access plus 1 month"
    ExpiresByType image/jpeg                "access plus 1 month"
    ExpiresByType video/ogg                 "access plus 1 month"
    ExpiresByType audio/ogg                 "access plus 1 month"
    ExpiresByType video/mp4                 "access plus 1 month"
    ExpiresByType video/webm                "access plus 1 month"
    # HTC files  (css3pie)
    ExpiresByType text/x-component          "access plus 1 month"
    # Webfonts
    ExpiresByType application/x-font-ttf    "access plus 1 month"
    ExpiresByType font/opentype             "access plus 1 month"
    ExpiresByType application/x-font-woff   "access plus 1 month"
    ExpiresByType application/x-font-woff2  "access plus 1 month"
    ExpiresByType image/svg+xml             "access plus 1 month"
    ExpiresByType application/ "access plus 1 month"
    # CSS and JavaScript
    ExpiresByType text/css                  "access plus 1 year"
    ExpiresByType application/javascript    "access plus 1 year"
    # Gzip compression
    <IfModule mod_deflate.c>
    # Active compression
    SetOutputFilter DEFLATE
    # Force deflate for mangled headers
    <IfModule mod_setenvif.c>
    <IfModule mod_headers.c>
    SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
    RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
    # Don’t compress images and other uncompressible content
    SetEnvIfNoCase Request_URI \
    \.(?:gif|jpe?g|png|rar|zip|exe|flv|mov|wma|mp3|avi|swf|mp?g|mp4|webm|webp)$ no-gzip dont-vary
    # Compress all output labeled with one of the following MIME-types
    <IfModule mod_filter.c>
    AddOutputFilterByType DEFLATE application/atom+xml \
                                     application/javascript \
                                     application/json \
                                     application/rss+xml \
                                     application/ \
                                     application/x-font-ttf \
                                     application/xhtml+xml \
                                     application/xml \
                                     font/opentype \
                                     image/svg+xml \
                                     image/x-icon \
                                     text/css \
                                     text/html \
                                     text/plain \
                                     text/x-component \
    <IfModule mod_headers.c>
    Header append Vary: Accept-Encoding
    <IfModule mod_mime.c>
    AddType text/html .html_gzip
    AddEncoding gzip .html_gzip
    <IfModule mod_setenvif.c>
    SetEnvIfNoCase Request_URI \.html_gzip$ no-gzip
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTPS} on [OR]
    RewriteCond %{SERVER_PORT} ^443$ [OR]
    RewriteCond %{HTTP:X-Forwarded-Proto} https
    RewriteRule .* - [E=WPR_SSL:-https]
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteRule .* - [E=WPR_ENC:_gzip]
    RewriteCond %{REQUEST_METHOD} GET
    RewriteCond %{QUERY_STRING} =""
    RewriteCond %{HTTP:Cookie} !(wordpress_logged_in_|wp-postpass_|wptouch_switch_toggle|comment_author_|comment_author_email_) [NC]
    RewriteCond %{REQUEST_URI} !^(.*/feed/?|/wp-json/(.*))$ [NC]
    RewriteCond %{HTTP_USER_AGENT} !^(facebookexternalhit).* [NC]
    RewriteCond "%{DOCUMENT_ROOT}/wp-content/cache/wp-rocket/%{HTTP_HOST}%{REQUEST_URI}/index%{ENV:WPR_SSL}.html%{ENV:WPR_ENC}" -f
    RewriteRule .* "/wp-content/cache/wp-rocket/%{HTTP_HOST}%{REQUEST_URI}/index%{ENV:WPR_SSL}.html%{ENV:WPR_ENC}" [L]
    # END WP Rocket
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    # END WordPress
    # BEGIN Bad Bot Blocker
    SetEnvIfNoCase User-Agent "Abonti|aggregator|AhrefsBot|asterias|BDCbot|BLEXBot|BuiltBotTough|Bullseye|BunnySlippers|ca\-crawler|CCBot|Cegbfeieh|CheeseBot|CherryPicker|CopyRightCheck|cosmos|Crescent|discobot|DittoSpyder|DotBot|Download Ninja|EasouSpider|EmailCollector|EmailSiphon|EmailWolf|EroCrawler|Exabot|ExtractorPro|Fasterfox|FeedBooster|Foobot|Genieo|grub\-client|Harvest|hloader|httplib|HTTrack|humanlinks|ieautodiscovery|InfoNaviRobot|IstellaBot|Java/1\.|JennyBot|k2spider|Kenjin Spider|Keyword Density/0\.9|larbin|LexiBot|libWeb|libwww|LinkextractorPro|linko|LinkScan/8\.1a Unix|LinkWalker|LNSpiderguy|lwp\-trivial|magpie|Mata Hari|MaxPointCrawler|MegaIndex|Microsoft URL Control|MIIxpc|Mippin|Missigua Locator|Mister PiX|MJ12bot|moget|MSIECrawler|NetAnts|NICErsPRO|Niki\-Bot|NPBot|Nutch|Offline Explorer|Openfind|panscient\.com|PHP/5\.\{|ProPowerBot/2\.14|ProWebWalker|Python\-urllib|QueryN Metasearch|RepoMonkey|RMA|SemrushBot|SISTRIX|sitecheck\.Internetseer\.com|SiteSnagger|SnapPreviewBot|Sogou|SpankBot|spanner|spbot|Spinn3r|suzuran|Szukacz/1\.4|Teleport|Telesoft|The Intraformant|TheNomad|TightTwatBot|Titan|toCrawl/UrlDispatcher|True_Robot|turingos|TurnitinBot|UbiCrawler|UnisterBot|URLy Warning|VCI|WBSearchBot|Web Downloader/6\.9|Web Image Collector|WebAuto|WebBandit|WebCopier|WebEnhancer|WebmasterWorldForumBot|WebReaper|WebSauger|Website Quester|Webster Pro|WebStripper|WebZip|Wotbox|wsr\-agent|WWW\-Collector\-E|Xenu|Zao|Zeus|ZyBORG|coccoc|Incutio|lmspider|memoryBot|SemrushBot|serf|Unknown|uptime files" bad_bot
    Deny from env=bad_bot
    # END Bad Bot Blocker
    Then repeat for SSL?

    <VirtualHost [2600:3c03:0000:0000:0000:0000:00da:b005]:443>
        <Directory /home/example/public_html/>
          Contents here as above....
      # Include "/etc/apache2/conf.d/userdata/ssl/2_4/cav02/*.conf"
    I'm confused over exactly how I structure this.

    I did the above just within the <directory> structure, disabled the .htaccess file for the site, restarted apache, and nothing.
    #1 Anthony Parsons, Jan 15, 2017
    Last edited: Jan 15, 2017
  2. cPanelMichael

    cPanelMichael Technical Support Community Manager Staff Member

    Apr 11, 2011
    Likes Received:
    Trophy Points:
    cPanel Access Level:
    Root Administrator

    We document the steps on custom VirualHost includes at:

    Modify Apache Virtual Hosts with Include Files - EasyApache 4 - cPanel Documentation

    Yes, it's fine to create this files and directories as the root user.

    You don't have to replicate the full VirualHost entry in the include file. Includes are the last directives in each virtual host. An include file's contents will override any value that exists in the primary configuration file.

    Could you provide an example of a specific .htaccess rule you are attempting to configure in the VirtualHost for a domain name?

    Thank you.
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
    Anthony Parsons likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice