Saltar al contenido

~ / archivo / stackoverflow / 50338741

htaccess to redirect to folder, ignoring subdomain

StackOverflow · .htaccess, http-redirect · ayudé
  1. Brian Brown PhD pregunta 2018-05-14 20:45

    I'm having trouble creating .htaccess rules to do this domain name is ico.[any domain name] files are all icon files (.ico, .gif, .png, .svg) [nothing else served here, ever].
    And I need the icons to be served (permanent redirects) up from folders based on the root domain, i.e.:
    https://ico.example0.com/favicon.ico to https://ico.example0.com/www.example0.com/favicon.ico
    https://ico.example1.com/apple-touch-icon.png to https://ico.example1.com/www.example1.com/apple-touch-icon.png
    https://ico.example2.com/favicon.gif to https://ico.example0.com/www.example2.com/favicon.gif

    Can this be done?
    Thanks!
    -Brian

    • mikekehrli 2018-05-14 20:57 I'm not clear on what you're trying to do. Where are the files you are trying to serve up? By that I mean what is the directory structure on the host computer where the files are. Using .htaccess to redirect is pretty straightforward, but what you have asked for assumes an odd directory structure. Maybe not. But please show what you are trying to do.
    • Brian Brown PhD 2018-05-14 20:59 the directory structure is: ico.[anydomain.tld/www.[anydomain,tld]/favicon.ico Its kind of a generic CDN for icon files. Thanks!
  2. Miguel Ortiz yo respuesta 2018-05-18 18:15

    Yup, you can do that.

    You'll need to create directories for each domain in your %DOCUMENT_ROOT% (for this I've created www.google.com and www.facebook.com:

    root@alberta:/var/www/html# ls -ltr
    total 32
    -rw-r--r--  1 www-data www-data 10701 Apr 16 11:01 index.html
    drwxr-xr-x 15 www-data www-data 12288 Apr 16 14:52 mantis
    drwxr-xr-x  2 www-data www-data 4096 May 18 15:02 www.google.com
    drwxr-xr-x  2 www-data www-data 4096 May 18 15:04 www.facebook.com
    

    Add a rule to redirect using as base the %HTTP_HOST% (which is the domain from where the request comes). I've added the rule in my default virtualhost /etc/apache2/sites-available/000-default.conf:

        RewriteEngine on
        RewriteRule ^/?(.*) %{DOCUMENT_ROOT}/%{HTTP_HOST}/$1 [L]
    

    Which results in this configuration:

    <VirtualHost *:80>
            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/html
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
            RewriteEngine on
            RewriteRule ^/?(.*) %{DOCUMENT_ROOT}/%{HTTP_HOST}/$1 [L]
            Include conf-available/serve-cgi-bin.conf
    </VirtualHost>
    

    I've emulated the request using CURL and it works (The parameter --header Host: tells apache which domain I am):

    root@alberta:/etc/apache2/sites-available# curl --verbose --header 'Host: www.google.com' 'localhost'
    

    And the answer is the document html in my www.google.com directory:

    < HTTP/1.1 200 OK
    ...
    <p>ok google.com</p>
    

    The same with facebook:

    root@alberta:/etc/apache2/sites-available# curl --verbose --header 'Host: www.facebook.com' 'localhost'
    

    And the answer is the document in my www.facebook.com folder:

    < HTTP/1.1 200 OK
    ...
    <p>my facebook.com</p>
    

    Cheers!

    • Brian Brown PhD 2020-02-24 17:08 RewriteRule ^/?(.*) %{DOCUMENT_ROOT}/%{HTTP_HOST}/$1 [L] Throws a '500' error.

← todos los hilos de StackOverflow