Show file if exists otherwise redirect, but only for specific directory

Discussion in 'Apache' started by thefolenangel, Apr 24, 2020.

  1. #1
    Hi, I have a website that exists to forward traffic to the English version of the main website. Now I would like to forward every traffic from that site, except requests to specific directory.

    Logic should be something like:
    1. if not https, redirect to https
    2. if request is for .well-known challenges, let to current server
    3. if request is for file with location "language", let to current server
      1. if file exists show file
      2. else show default file
    4. every other request redirect to main-website.dk/en
    I have written some logic, obviously it fails, so I would appreciate some feedback/help
    
    <VirtualHost *:80>
        ServerName site.com
        ServerAlias www.site.com
        DocumentRoot /var/www/site-com
    
        <Directory /var/www/site-com>
            Options -Indexes +FollowSymLinks
            AllowOverride All
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/site.com-error.log
        CustomLog ${APACHE_LOG_DIR}/site.com-access.log combined
    
    
    RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge(/[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-]*)?$
    RewriteRule  ^/(.*)  https://%{SERVER_NAME}/$1  [last,redirect=301]
    </VirtualHost>
    
    Code (markup):
    
    
    RewriteEngine on
    
    RewriteOptions inherit
    RewriteBase /
    
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
    
    RewriteCond %{DOCUMENT_ROOT}/language/%{REQUEST_URI} -f
    RewriteRule (.*) /language/$1 [S=2]
    
    RewriteCond !%{DOCUMENT_ROOT}/language/%{REQUEST_URI} -f
    RewriteRule (.*) /language/default.json [S=1]
    
    RewriteCond %{HTTP_HOST} ^site\.com [OR]
    RewriteCond %{HTTP_HOST} ^www\.site\.com
    RewriteRule ^(.*)$ https://main-website.dk/en/ [L]
    
    
    Code (markup):
    the /var/www/site-com directory contains only language directory with files and .httaccess
     
    Last edited by a moderator: Apr 24, 2020
    thefolenangel, Apr 24, 2020 IP