Removing /html/ directory from the end of my URL?

January 23, 2015 3.4k views

I installed Apache2 on Ubuntu and put everything in /var/www/html for my linked domain name. Is this where its supposed to be on Apache 2? Its the only place that its been working for me...

But how do I stop my domain (typing in domain.tld) from automatically going to domain.tld/html/? The website displays ok but can't I just get it to show the contents of the html file (and their subdirectories) and show domain.tld?

I have tried some .htaccess fixes but to no avail. I also have some code in my .htaccess which redirects http:// to https:// and that is at least working now.

5 Answers

If your VirtualHost's DocumentRoot is set to /var/www, you will need to set it to /var/www/html so that http://yourdomain.tld/ maps to /var/www/html instead of /var/www.

Open your VirtualHost file in nano:

sudo nano /etc/apache2/sites-enabled/virtual-host

and set DocumentRoot to /var/www/html.

Then restart Apache:

sudo service apache2 restart
  • Thanks for the response.

    I was using 000-default.conf but now copied it out to mydomain.tld.conf. I have two virtual hosts one for 80 and one I created when I added my SSL certificate at port 443. Both have ports have now had their document root set to /var/www/html (in both mydomain.tld and 000-default) but to no avail! I have restarted apache also. Maybe it takes some time to propagate?

While for the question "Is this where it's supposed to be on Apache 2?": as you might evince from kamaln7 answer, you can place your files in the directory you prefer (don't forget to give it the proper owner and permissions) and then change your VirtualHost's configuration accordingly.

It only seems to be an issue when www. is in front of the address; otherwise it doesn't redirect. Maybe its because of the ServerName, ServerAlias combo I used?

  • It's a bit hard to figure out what's going on without much details. Can you post your virtualhosts?

    I'm guessing you have a virtualhost for domain.com which serves /var/www/html as expected, but not for www.domain.com, so Apache resorts to the default virtualhost (000-default.conf) which serves /var/www.

    Your virtualhost should look something like this:

    <VirtualHost *:80>
        ServerName domain.com
        ServerAlias www.domain.com
    
        DocumentRoot /var/www/html
        . . .
    

Here's my 000-default.conf file, (domain.tld.conf is the same because I'm not sure which one the browser is reading)

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName domain.com
        ServerAlias www.domain.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM

SSLCertificateFile /etc/apache2/ssl/ssl.crt
SSLCertificateKeyFile /etc/apache2/ssl/private.key
SSLCertificateFile /etc/apache2/ssl/ssl.crt
SSLCertificateKeyFile /etc/apache2/ssl/private.key
SSLCertificateChainFile /etc/apache2/ssl/sub.class1.server.ca.pem
    ServerAdmin webmaster@localhost
       ServerName domain.com
        ServerAlias www.domain.com
    DocumentRoot /var/www/html
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/


    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

Maybe if I put my ServerName as the IP address it would help?

Have another answer? Share your knowledge.