1

I want to rewrite an url with mod_rewrite from http://www.server.com/directory/public to http://www.server.com/directory (where directory would be then directing to directory/public)

I've tried several things but I can't figure it out. Here is my configuration:

    <VirtualHost xxx.xxx.xxx.xxx:443>
            ServerAdmin [email protected]
            ServerName www.server.com
            DocumentRoot /var/www/server.com

        <Directory /var/www/server.com>
            RewriteEngine on
            RewriteCond %{REQUEST_URI} ^/directory
            RewriteRule ^(.*)$ /directory/public/ [L]
        </Directory>
    </VirtualHost>

2 Answers 2

1

Try changing your rewrite rule to this:

RewriteRule ^/directory(.*) /directory/public$1 [NC,L]

You do not need the RewriteCond

1
  • Worked flawlessly! Thanx!
    – Ezeyme
    Jan 17, 2012 at 10:07
0

Within a <Directory > context, URIs in rewrite rules do not start with a /.

This is also true for URIs in rewrite rules in .htaccess files because they are implicitly in a directory.

The relevant part of the documentation is titled "Per-directory Rewrites"

To fix this you can do one of:

  1. Move the RewriteRule and RewriteCond out of the <Directory > block.
  2. Remove the leading slash from the RewriteCond and the second part of the RewriteRule.
  3. Follow Ivmisooners' suggestion but also move the RewriteRule out of the <Directory > block.
  4. Follow Ivmisooners' suggestion but remove the leading slashes from the RewriteRule.

If it were my choice, I would use option 3.


From my reading of the documentation page linked above, it seems to suggest that you must have Options FollowSymLinks enabled for RewriteRules to work in a per-directory context. I have never tested this myself. If you have troubles getting RewriteRules to work at all inside that <Directory > block, try adding Options +FollowSymLinks.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .