Score:0

Apache2 wildcard rewrite

cn flag

I have a php app on Apache2 that is supposed to treat subdomains on the code level.

My goal is to redirect any wildcard subdomain to the main domain's index.php. I do not use .htaccess.

I searched Stackoverflow and also googled for the solution, and could not find one. The virtual host looks like this:

<VirtualHost *:80>
      ServerAdmin admin@website.com
      ServerName website.com
      ServerAlias *.website.com
      DocumentRoot /var/www/html/site_admin/public_html
      # Directory path where code exists
        <Directory /var/www/html/site_admin/public_html>
                Options Indexes FollowSymLinks
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/website.com-error.log
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel error
        CustomLog ${APACHE_LOG_DIR}/website.com.log combined
        RewriteEngine On
        #If the domain (any domain) is not exactly website.com...
        RewriteCond %{HTTP_HOST} !^website\.com$ [NC]
        RewriteRule (.*) http://(.*).website.com [L,R=301,QSA]
        RewriteRule ^.*$ /index.php [NC,L]
</VirtualHost>

When I access the main domain, I can see the website content. However, if I access sub.website.com, I get redirected to %28.%2A%29.website.com

Please advice

Thanks ahead

Igal

Score:0
in flag

If the PHP code is handling the subdomain rules by itself, there's no need to overcomplicate the Apache configuration. This is what I do for my PHP projects that support multiple addresses:

<VirtualHost *:80>
        ServerAdmin your@email.addy
        DocumentRoot /var/www/project/public

        ServerName website.com
        ServerAlias *.website.com *.* *.*.*
        DirectoryIndex index.php index.html

        ErrorLog ${APACHE_LOG_DIR}/project-error.log
        CustomLog ${APACHE_LOG_DIR}/project-access.log combined
</VirtualHost>

<Directory /var/www/project/public>
        Options FollowSymLinks
        AllowOverride All

        Order Allow,Deny
        Allow From All
</Directory>

Note the multiple wildcards in the ServerAlias line. This will allow the server to respond to all traffic, even if the domain is not known by the application (which then allows for a PHP-based redirect to other capture process).

I generally keep rewrite rules out of the Apache configuration an instead put them in an .htaccess file, which I understand you do not currently use. Experience has shown that keeping rewrites out of the configuration file results in fewer "weird situations".

This is how I configure the .htaccess file that is used for projects with the Apache configuration found above:

Options +MultiViews +FollowSymLinks
Options -Indexes

DirectoryIndex index.php

RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php

Header always edit Set-Cookie (.*) "$1; SameSite=None; Secure"

<filesMatch "\.(ico|css|js|pdf|jpg|jpeg|png|gif)$">
    Header set Cache-Control "max-age=1209600, public"
</FilesMatch>

<FilesMatch "\.(htaccess|htpasswd|inc|log|sql|user|token)$">
        Order Allow,Deny
        Deny from all
</FilesMatch>

<ifModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/xml text/css text/plain
    AddOutputFilterByType DEFLATE image/svg+xml application/xhtml+xml application/xml
    AddOutputFilterByType DEFLATE application/rdf+xml application/rss+xml application/atom+xml
    AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript application/json
    AddOutputFilterByType DEFLATE application/x-font-ttf application/x-font-otf
    AddOutputFilterByType DEFLATE font/truetype font/opentype
</ifModule>

With this, Apache will pass all traffic to the PHP-based site and it's up to that code to perform redirects, access control, and URL translations

igalsc avatar
cn flag
thank you, @matigo . I tried this config, but it does not work. if I access website.com i get a response. If I load subdomain.website.com I get 404
in flag
How does your PHP handle the domains? Is the 404 from Apache or the PHP code? I cannot debug your issue; I can only provide solutions that have worked for me over the years with the many servers and systems I am responsible for
igalsc avatar
cn flag
thank you, @matigo ! the subdomains are to be handled on the php level, and it looks like we are failing with 404 by the application, not apache. I will test your config on another server, with simple html files
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.