1.15. Cleaner URIs with a Virtual Host

Having index.php in the URI is ugly. We can clean that up by using an Apache virtual host and applying some mod_rewrite rules.

In your Apache httpd.conf file, or other appropriate configuration files, turn on virtual host support, and then set up a virtual host for your Solar system. The virtual host should point to SYSTEM/docroot as the document root for the virtual host.

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName subdomain.example.com
    DocumentRoot SYSTEM/docroot
</VirtualHost>

The standard Solar system docroot directory already contains a .htaccess file that turns on mod_rewrite. The rules therein look something like this:

<IfModule rewrite_module>
    # turn on rewriting
    RewriteEngine On
    
    # hint the Solar_Uri_Action class as to the base path
    SetEnv SOLAR_URI_ACTION_PATH /
    
    # turn empty requests into requests for "index.html",
    # keeping the query string intact
    RewriteRule ^$ index.html [QSA]
    
    # for all files not found in the file system,
    # reroute to "index.php" bootstrap script,
    # keeping the query string intact.
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

[Note] Note

You may need to set AllowOverride All in the <Directory> section of your Apache configuration file for the SYSTEM directory.

After you restart Apache (or otherwise refresh the service with the new configuration), you should be able to browse to the same app using the new virtual host, without an index.php in the URI. For example, whereas previously we used http://localhost/index.php/blog, you should now be able to go to http://subdomain.example.com/blog.

[Note] Faking A Domain

You can "fake" a domain by adding an entry to your /etc/hosts file. For example, if you add the line ...

127.0.0.1    example.local

... you will be able to browse to "http://example.local" as if it was a real domain. You can then set your virtual host entry to use ServerName example.local You can add any number of fake domains this way.



Local