I am building an application tshirtshop I have following configuration in


<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/tshirtshop
        <Directory /var/www/tshirtshop>
                Options Indexes FollowSymLinks
                AllowOverride All
                Order allow,deny
                allow from all
        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

and following in .htaccess file in location


<IfModule mod_rewrite.c>
# Enable mod_rewrite
RewriteEngine On
# Specify the folder in which the application resides.
# Use / if the application is in the root.
RewriteBase /tshirtshop
#RewriteBase /
# Rewrite to correct domain to avoid canonicalization problems
# RewriteCond %{HTTP_HOST} !^www\.example\.com
# RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# Rewrite URLs ending in /index.php or /index.html to /
RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php|html?)\ HTTP
RewriteRule ^(.*)index\.(php|html?)$ $1 [R=301,L]
# Rewrite category pages
RewriteRule ^.*-d([0-9]+)/.*-c([0-9]+)/page-([0-9]+)/?$ index.php?DepartmentId=$1&CategoryId=$2&Page=$3 [L]
RewriteRule ^.*-d([0-9]+)/.*-c([0-9]+)/?$ index.php?DepartmentId=$1&CategoryId=$2 [L]
# Rewrite department pages
RewriteRule ^.*-d([0-9]+)/page-([0-9]+)/?$ index.php?DepartmentId=$1&Page=$2 [L]
RewriteRule ^.*-d([0-9]+)/?$ index.php?DepartmentId=$1 [L]
# Rewrite subpages of the home page
RewriteRule ^page-([0-9]+)/?$ index.php?Page=$1 [L]
# Rewrite product details pages
RewriteRule ^.*-p([0-9]+)/?$ index.php?ProductId=$1 [L]

the site is working on localhost and is working as if there is no .htaccess rule specified i.e. if I were to view a page as


then I get a 404 Error but if I view the same page as


then I can view it.

sudo apache2ctl -M
Loaded Modules:
 core_module (static)
 log_config_module (static)
 logio_module (static)
 mpm_prefork_module (static)
 http_module (static)
 so_module (static)
 alias_module (shared)
 auth_basic_module (shared)
 authn_file_module (shared)
 authz_default_module (shared)
 authz_groupfile_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 cgi_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 mime_module (shared)
 negotiation_module (shared)
 php5_module (shared)
 reqtimeout_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 status_module (shared)
Syntax OK

What is the mistake if any one can point out in above configuration, or else I need to check any thing else?

  • Will you accept your solution if that works? – Anwar Sep 14 '16 at 13:45

I have been able to solve problem, what happens is when you install Ubuntu and use apache2 in it there is a default website which is activated as soon as apache2 is installed in ubuntu

so that default website which is active is


what happens is I have 2 files in sites-enabled directory


when Ubuntu's Apache loads the vhosts from sites-enabled directory

it loads the vhost configurations in alphabetical order so 000-default was loaded before tshirtshop and in 000-default there is a line which is following

DocumentRoot /var/www
<Directory /var/www/>
            AllowOverride None

Reasons my configuration was not working 1) Note the AllowOverride None in 000-default was getting priority over AllowOverride all in tshirtshop (alphabetical loading of vhosts) so this was the reason my .htaccess file was not loaded since the 000-default site configuration was loaded before reading tshirtshop vhost and it already carried a line AllowOverride None 2) In my /etc/apache2/sites-enabled/tshirshop file I wrote

DocumentRoot /var/www/tshirtshop

and was accessing the application as


so I changed the vhost tshirtshop's DocumentRoot

DocumentRoot /var/www/tshirtshop


 DocumentRoot /var/www

and disabled the default site of Ubuntu's apache

sudo a2dissite default

Now my configuration of /etc/apache2/sites-enabled/tshirtshop is being read and applied to.So now my .htaccess file is being respected.

| improve this answer | |

I'm not sure whether this is an Ubuntu related question and should be asked here.

But here is the problem I see. You are accessing the site via http://localhost/tshirtshop, while your /etc/apache2/sites-enabled/tshirtshop file's documentRoot and directory have /var/www/tshirtshop. This means that this file is not used as the config for localhost - according to those values site should be accessed from just http://localhost not http://localhost/tshirtshop.

This could happen if the file was not created in the /etc/apache2/sites-available folder and then enabled in the terminal (which would create a symbolic link to sites-enabled) after the default was disabled.

I don't know the enable and disable commands, since I use webmin to handle the setup of local test sites for me. And then edit my /etc/hosts file to be able to access on something else closer to the real url rather than localhost.

But back to the problem, using the wrong URL causes your rewriteRule to rewrite to hhtp://localhost/index.php?DepartmentId=2 which would be correct if the site was accessed via just http://localhost and not http://localhost/tshirtshop. You can now see why you get a 404 error.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.