Get these Rewrite rules to work in Apache Directives?

Discussion in 'Installation/Configuration' started by bch, Jul 26, 2018.

Tags:
  1. bch

    bch Member

    Hi, I use these rules in .htaccess and they work perfect.
    However, they do not work in Options / Apache Directives.
    Code:
    <IfModule mod_rewrite.c>
      
        # Start Rewrite Engine
            RewriteEngine On
            RewriteBase /
            Options +FollowSymLinks
      
        # Remove trailing slash
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)/$ /$1 [L]
      
        # Remove .php extension
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME}\.php -f
            RewriteRule ^([^\.]+)$ $1.php [L]
      
        # Remove .html extension
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME}\.html -f
            RewriteRule ^([^\.]+)$ $1.html [L]
          
    </IfModule>
    
    Can you help me getting them to work? I use Ubuntu 18.04.
    Thanks! :)
     
    Last edited: Jul 28, 2018
  2. bch

    bch Member

    Ok, I finally got them to work, but now my problem is that they only work with Mod-PHP.
    Code:
    <Directory {DOCROOT}>
       
        <IfModule mod_core.c>
           
            AllowOverride All
            Options +FollowSymLinks
               
        </IfModule>
       
        <IfModule mod_rewrite.c>
               
            # Start Rewrite Engine
                RewriteEngine On
                RewriteBase /
               
            # Remove trailing slash
                RewriteCond %{REQUEST_URI} !-d
                RewriteRule ^(.*)/$ /$1 [R,L]
               
            # Remove need for ".php"
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteCond %{REQUEST_FILENAME}\.php -f
                RewriteRule ^([^\.]+)$ $1.php [L]
               
            # Remove need for ".html"
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteCond %{REQUEST_FILENAME}\.html -f
                RewriteRule ^([^\.]+)$ $1.html [L]
               
        </IfModule>
       
    </Directory>
    
     
    Last edited: Jul 28, 2018
  3. till

    till Super Moderator Staff Member ISPConfig Developer

    Theey work with mod_php only as you limited them to work with mod_php only. You have to remove the IfModiule lines.
     
  4. bch

    bch Member

    Unfortunately they still don't work if I remove the IfModule lines.
    I use vhost.conf.master from master branch and did a resync afterwards.
     
  5. till

    till Super Moderator Staff Member ISPConfig Developer

    Remove them from vhost.conf.master and add them in the apache directives field of the website.
     
  6. bch

    bch Member

  7. till

    till Super Moderator Staff Member ISPConfig Developer

    But you can not use a placeholder {DOCROOT} in vhost.conf.master. It simply does not exist there. BTW, normally one puts rewrite rules just in a .htaccess file on apache servers.

    Create a file named .htaccess in the web folder of the website and there you add:

    Code:
    AllowOverride All
            Options +FollowSymLinks
             
             
            # Start Rewrite Engine
                RewriteEngine On
                RewriteBase /
             
            # Remove trailing slash
                RewriteCond %{REQUEST_URI} !-d
                RewriteRule ^(.*)/$ /$1 [R,L]
             
            # Remove need for ".php"
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteCond %{REQUEST_FILENAME}\.php -f
                RewriteRule ^([^\.]+)$ $1.php [L]
             
            # Remove need for ".html"
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteCond %{REQUEST_FILENAME}\.html -f
                RewriteRule ^([^\.]+)$ $1.html [L]
     
  8. bch

    bch Member

    I have not put anything custom in vhost.conf.master. I mention I use the master branch version because there could be changes in it that causes the issue. It is on the 3.1.13 milestone to backport it.

    I already have success with using .htaccess as I wrote in the OP. I just try to optimize my server for speed, as .htaccess adds overhead and I have a lot of sites with this configuration.
     
    Last edited: Jul 28, 2018
  9. bch

    bch Member

    Hi @till and thank you for trying to help me.
    I have almost solved the issue. It had nothing to do with the php-modes.
    First of all I think these parts of vhost.conf.master
    Code:
    <If "-f %{DOCUMENT_ROOT} . '/' . %{REQUEST_URI}">
    should be replaced with
    Code:
    <If "-f %{SCRIPT_FILENAME}">
    This article here is describing it in more detail:
    http://lost.l-w.ca/0x05/apache-mod_proxy_fcgi-and-php-fpm/

    It makes it possible to archieve my goal with the following:
    Code:
    # Start Rewrite Engine
        RewriteEngine On
    
    # Remove trailing slash
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)/$ $1 [R,L]
    
    # Remove need for ".html"
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}\.html -f
        RewriteRule ^(.*)$ $1.html [L]
    
    # Remove need for ".php"
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}\.php -f
        RewriteRule ^(.*)$ $1.php [L]
    
    The issue then is that these specific rewrite rules affects aliases on the same domain so fx. example.com/phpmyadmin will cause redirect loops.

    To overcome this, the solution is to wrap the rewite rules in a
    Code:
    <Directory /var/www/clients/clientX/webX/web>
    and not the {docroot} snippet
    Code:
    <Directory /var/www/domain.com/web>
    like this
    Code:
    <Directory /var/www/clients/clientX/webX/web>
     
        # Start Rewrite Engine
            RewriteEngine On
            RewriteBase /
     
        # Remove trailing slash
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)/$ $1 [R,L]
     
        # Remove need for ".html"
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME}\.html -f
            RewriteRule ^(.*)$ $1.html [L]
     
        # Remove need for ".php"
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME}\.php -f
            RewriteRule ^(.*)$ $1.php [L]
    
    </Directory>
    This also solves the problem where now, you cannot put for example "Options -Indexes" in Directives field, it does not have any effect.
    Currently on 18.04 (at least) directory indexes are shown as default - just go to /error/ on a webpage.

    It would be very nice if a snippet could be created for the real docroot path and not the symlinked one.
     
    Last edited: Jul 31, 2018
    till likes this.
  10. bch

    bch Member

    How could I create a new Snippet variable with the value of the real docroot path?
    In the vhost, the document root is /var/www/clients/clientX/webX/web, but the {docroot} snippet is /var/www/domain.com/web
    <Directory %{DOCUMENT_ROOT}> also gives /var/www/domain.com/web
    +FollowSymLinks in <Directory> doesn't work either.
     
    Last edited: Jul 31, 2018
  11. till

    till Super Moderator Staff Member ISPConfig Developer

    Use {DOCROOT_CLIENT} instead of {DOCROOT}
     
  12. bch

    bch Member

    That's amazing, thank you Till :)
    May I ask you, where are these snippets created in the code?
     
  13. till

    till Super Moderator Staff Member ISPConfig Developer

    In the apache and nginx plugin. To find the place in the file, search for the placeholder.
     
  14. bch

    bch Member

    Thank you, I found it in apache2_plugin.inc.php
     

Share This Page