Reputation: 1492
WORKS FINE: www.exampledomain.co.uk{/with-any-url}
GIVES 404 ERROR: exampledomain.co.uk{/with-any-url}
I am having an issue with a site running on linux/apache2 whereby 'www.' urls are working fine, however with the 'www.' removed a 404 is returned for every url. The domain shows no errors in a dnsstuff.com check, with the www and regular domain pointing to the same IP address.
When I use cmd to ping the domain and the www. domain, both resolve to the same IP and respond.
I implemented this code in a .htaccess file in the web root, which serves to prepend the 'www.' onto non-www requests:
Options +FollowSymlinks
Options All -Indexes
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
However, this isn't working either. The exact rewrite rules are tested and function on another site to enforce the 'www.' to be prefixed.
This is the vhosts conf file in use:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName www.exampledomain.co.uk
ServerAlias exampledomain.co.uk
DirectoryIndex index.php index.html
DocumentRoot /home/exampleuser/public/example.co.uk/public_html
<Directory /home/exampleuser/public/example.co.uk/public_html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
LogLevel warn
ErrorLog /home/exampleuser/public/example.co.uk/log/error.log
CustomLog /home/exampleuser/public/example.co.uk/log/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName exampledomain.co.uk
Redirect permanent / http://www.exampledomain.co.uk/
</VirtualHost>
The server name and alias have been defined in reverse order to what is shown here (with apache restarts in-between changes) and the second vhosts entry for the 'Redirect permanent' has been added later, all to no effect.
What could be causing this behaviour? Anything else I should check? Have searched through so much material to find a solution but can't see anything wrong or missing. Any help will be greatly appreciated!
Upvotes: 2
Views: 821
Reputation: 1492
Today I noticed that from a previous testing server instance there was another vhosts conf file in /etc/apache2/sites-enabled/ which was left behind and named example.co.uk.conf - the directory name used for the site that the main exampledomain.co.uk.conf file was pointing towards is /home/exampleuser/public/example.co.uk/public_html so I checked inside and the file contained an empty vhosts declaration:
<VirtualHost *:80>
</VirtualHost>
When this <VirtualHost>
section was commented out with hashes or removed, or the conf file was renamed to something other than example.co.uk.conf, the non-www. requests were then being routed into the correct web root just as with the www. requests and the problem was resolved.
However, I am not clear about why this example.co.uk.conf is chosen for requests to exampledomain.co.uk and not for those to www.exampledomain.co.uk, so if anyone can extend this answer please edit or comment. Thanks!
Upvotes: 0
Reputation: 1
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Upvotes: 0