Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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 webmaster@server.com
            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>
share|improve this question

2 Answers 2

up vote 1 down vote accepted

Try changing your rewrite rule to this:

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

You do not need the RewriteCond

share|improve this answer
    
Worked flawlessly! Thanx! –  Ezeyme Jan 17 '12 at 10:07

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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