3

I have configured a virtual host in my local machine and every thing is working fine. Now I want to use SEO friendly urls. To achieve this I have used the .htaccess file.

My virtual host configuration is:

<VirtualHost *:80>

    DocumentRoot "C:/xampp/htdocs/ypp"
    ServerName ypp.com
    ServerAlias www.ypp.com
    ##ErrorLog "logs/dummy-host2.localhost-error.log"
    ##CustomLog "logs/dummy-host2.localhost-access.log" combined
</VirtualHost>

and my .htaccess file has:

AllowOverride All
RewriteEngine On 
RewriteBase /ypp/ 
RewriteRule ^/browse$ /browse.php 
RewriteRule ^/browse/([a-z]+)$ /browse.php?cat=$1 
RewriteRule ^/browse/([a-z]+)/([a-z]+)$ /browse.php?cat=$1&subcat=$2 

The above .htaccess setting is not working. After that I modified my virtual host setting and it is working. The new virtual host setting is:

<VirtualHost *:80>

    RewriteEngine On
    RewriteRule ^/browse$ /browse.php
    RewriteRule ^/browse/([a-z]+)$ /browse.php?cat=$1
    RewriteRule ^/browse/([a-z]+)/([a-z]+)$ /browse.php?cat=$1&subcat=$2
    ServerAdmin [email protected]
        DocumentRoot "C:/xampp/htdocs/ypp"
    ServerName ypp.com
    ServerAlias www.ypp.com
    ##ErrorLog "logs/dummy-host2.localhost-error.log"
    ##CustomLog "logs/dummy-host2.localhost-access.log" combined
    <Directory "C:/xampp/htdocs/ypp">
        AllowOverride All
    </Directory>
</VirtualHost>

Please let me know where I am going wrong in the .htacess file for url rewriting. I do not want to use the settings in virtual host, since for every change I have restart apache.

3
  • @parth: not a programming question. Voting to migrate to ServerFault. Commented May 15, 2011 at 18:51
  • is it really AllowOverride All RewriteEngine On or AllowOverride All (new line) RewriteEngine On
    – Daniel Ruf
    Commented May 15, 2011 at 18:59
  • Yep for every change you restart apache but this way apache will never have to check each time the htaccess when someone will read a page. Htaccess files in your case are far less efficient than htaccess. Think about that when going in prod. environment Commented Nov 20, 2011 at 20:48

1 Answer 1

4

You must put AllowOverride All in your <Directory/> section of the server configuration.

The only Apache configuration files that can allow options are the ones directly in the server (main file, virtualhost, etc.).

The .htaccess file can only remove override rights.

That is:

<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs/ypp"
    ServerName ypp.com
    ServerAlias www.ypp.com
    ##ErrorLog "logs/dummy-host2.localhost-error.log"
    ##CustomLog "logs/dummy-host2.localhost-access.log" combined
    <Directory "C:/xampp/htdocs/ypp">
        AllowOverride All
    </Directory>
</VirtualHost>

and the .htaccess file :

RewriteEngine On 
RewriteBase /ypp/ 
RewriteRule ^/browse$ /browse.php 
RewriteRule ^/browse/([a-z]+)$ /browse.php?cat=$1 
RewriteRule ^/browse/([a-z]+)/([a-z]+)$ /browse.php?cat=$1&subcat=$2 
1
  • 1
    You are generally correct, except I believe mod_rewrite only requires AllowOverride FileInfo, so you should set it to that in the vhost config instead of All unless you really need to allow all directives in .htaccess. It's best to follow the principle of least privilege. Commented Dec 21, 2011 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.