6 Replies - 883 Views - Last Post: 10 March 2017 - 08:19 AM

#1 cancer10  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 55
  • Joined: 12-July 08

Apache wildcard subdomain support

Posted 05 March 2017 - 01:42 AM

Hello,


I am having a hard time running my application (built on laravel 5.4) on a wildcard subdomain that I have setup on my local machine (running Apache 2.4.18 on Linux Mint 18.1)


So, I have setup a vhost (domain.app) and a subdomain (sub.domain.app) for it and my apache vhost file looks like the following:

# This is for the primary domain (domain.app)
<VirtualHost *:80>
        ServerName  domain.app

        ServerAlias www.domain.app


        ServerAdmin [email protected]
        DocumentRoot /var/www/html/test/domain.app/public

        <Directory /var/www/html/test/domain.app/public>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>

        <IfModule mod_dir.c>
            DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
        </IfModule>

</VirtualHost>

# This is for the subdomain (sub.domain.app)
<VirtualHost *:80>
                ServerName domain.app
                VirtualDocumentRoot /var/www/html/test/%0/public
                ServerAlias *.domain.app
                <Directory /var/www/html/test/sub.domain.app/public>
                        <IfModule mod_rewrite.c>
                                Options -MultiViews
                                RewriteEngine On
                                RewriteCond %{REQUEST_FILENAME} !-f
                                RewriteRule ^ index.php [L]
                        </IfModule>
                </Directory>
</VirtualHost>




This works fine for all of the following urls:




But since, I want a wildcard subdomain, meaning I do not want to hardcode sub.domain.app in the vhost but want something like anything.domain.app so I tried to replace

<Directory /var/www/html/test/sub.domain.app/public>


with

<Directory /var/www/html/test/%0/public>



I get the following results:



Please can someone help me?


Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Apache wildcard subdomain support

#2 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1769
  • View blog
  • Posts: 5,640
  • Joined: 15-January 14

Re: Apache wildcard subdomain support

Posted 06 March 2017 - 10:37 AM

According to the documentation, I don't see that "%0" is going to be expanded to something in the directory path, unless you're looking at something that I'm not:

https://httpd.apache....html#directory

It does show that you can use regular expression matching to define rules that match multiple directories, but where are you seeing the "%0" syntax?
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6538
  • View blog
  • Posts: 30,610
  • Joined: 10-May 07

Re: Apache wildcard subdomain support

Posted 06 March 2017 - 10:57 AM

If you set a specific Accesslog & Errorlog entry in your virtual hosts, when you tail the access log you should see the request for the subdomain.
Also, did you verify that Apache process properly stopped before taking the new configs? Meaning :

a.) You didn't just write the config & then it doesn't work
b.) A rouge Apache continued to run in memory upon the restart

Do a full stop, check via ps to verify that Apache is no longer running, then issue a start, tail the logs, access the site.

Lastly, your virutal hosts are using the wildcard for the entry, only listening to the port. You are certain that the subdomain of the app is looking to the same DNS? The 404 is coming from your server?

-- Edit --
Also, I will point out that using a percent sign on Linux directory naming is sort of insane. You are sure you want to do this? You are sure that the percentage is behaving the way you want it to? What happens when you issue the following:

ls -l /var/www/html/test/%0/public/

Lastly, look at the difference of lines 26 & 28; Why would the Document Root not match the entry for the Directory?
Was This Post Helpful? 0
  • +
  • -

#4 cancer10  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 55
  • Joined: 12-July 08

Re: Apache wildcard subdomain support

Posted 06 March 2017 - 10:06 PM

View PostArtificialSoldier, on 06 March 2017 - 10:37 AM, said:

According to the documentation, I don't see that "%0" is going to be expanded to something in the directory path, unless you're looking at something that I'm not:

https://httpd.apache....html#directory

It does show that you can use regular expression matching to define rules that match multiple directories, but where are you seeing the "%0" syntax?



I thought "%0" would work but it didnt.

If I use regex, how would it know which subdomain i'd want it to point to?
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1769
  • View blog
  • Posts: 5,640
  • Joined: 15-January 14

Re: Apache wildcard subdomain support

Posted 07 March 2017 - 12:03 PM

Like the documentation describes, the Directory element is just there to specify which directories the rules apply to. You use it like this:

        <Directory /var/www/html/test/domain.app/public>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>

All that does is say that those 3 directives apply to that directory. So you can use a regular expression in that directory match to apply the set of directives to any directory that matches the regular expression, that's all it's for.

For the virtualdocumentroot line, it looks like that does accept wildcards but the examples that I've seen aren't using %0, they will use %1 or %2 though. I haven't seen a description about what the wildcards represent in the virtualdocumentroot but maybe you can track that down.

http://stackoverflow...ualdocumentroot
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6538
  • View blog
  • Posts: 30,610
  • Joined: 10-May 07

Re: Apache wildcard subdomain support

Posted 07 March 2017 - 02:16 PM

If you want to use "wildcards", my suggestion is to manipulate the URL through rewrites & handle it programmatically, rather than trying to handle an unknown amount of subdomains.
Was This Post Helpful? 0
  • +
  • -

#7 cancer10  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 55
  • Joined: 12-July 08

Re: Apache wildcard subdomain support

Posted 10 March 2017 - 08:19 AM

Dear All,


Thank you everyone who cared to reply. I found the solution. All I had to do is use the following vhost code:

UseCanonicalName Off	
<VirtualHost *:80>
	ServerAdmin [email protected]
	ServerAlias *.domain.app
	VirtualDocumentRoot /var/www/html/test/%0/public
	DirectoryIndex	index.php index.htm index.html

	<Directory /var/www/html/test/*.domain.app/public/>
		AllowOverride	All
	</Directory>

	LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
	CustomLog /var/log/apache2/vhosts-access.log vcommon
	ErrorLog /var/log/apache2/vhosts-error.log
</VirtualHost>




Everything is working as I wanted it to.


Many thanks people :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1