Tips for Resolve Apache VirtualHost + mod_userdir URL Conflict with mod_rewrite

During revamp and reinstall with my server I decide to update my Drupal 7.x blog's folder under my own ~/public_html, e.g. ~/public_html/edin.no-ip.com/. BTW this also generate another issue which conflict with Apache mod_userdir setup, as my ~/public_html will able to access from http://edin.no-ip.com/~hswong3i/.

OK a quick checklist of my setup (under Debian Squeeze):

  • /home/users/hswong3i/: My $HOME directory
  • /home/users/hswong3i/public_html/: My public_html for mod_userdir
  • /home/users/hswong3i/public_html/edin.no-ip.com/: My blog's http://edin.no-ip.com/ DocumentRoot (Drupal 7.x)
  • /etc/apache2/sites-available/edin.no-ip.com: My static VirtualHost setup for domain http://edin.no-ip.com/, which point the DocumentRoot to "/home/users/hswong3i/public_html/edin.no-ip.com/", and symlink to /etc/apache2/sites-enabled/500-edin.no-ip.com for activation (Well, the content of this file is not funny so let's forget it...)
  • /etc/apache2/mods-enabled/userdir.conf: Enabled for all mod_userdir access to /home/users/*/public_html/

And my target:

  • http://edin.no-ip.com/: Direct access to my blog without anything special
  • http://edin.no-ip.com/~hswong3i: Direct access to my public_html without anything special
  • http://*/~hswong3i/edin.no-ip.com/: Will ALWAYS forward to http://edin.no-ip.com/ (Oh! See, this is the problem! We will able to access http://*/~hswong3i/edin.no-ip.com/ DIRECTLY!)

So Here is my solution:

  • Edit /home/users/hswong3i/public_html/edin.no-ip.com/.htaccess
  • Add the following lines under "RewriteEngine on": RewriteCond %{HTTP_HOST} !^edin\.no-ip\.com$ [NC] RewriteRule ^(.*)$ http://edin.no-ip.com/$1 [L,R=301] RewriteCond %{REQUEST_URI} ^/~hswong3i/edin.no-ip.com/(.*)$ RewriteRule ^ http://edin.no-ip.com/$1 [L,R=301]
  • So now test with different domain name and directory pattern.

That's it :D

Comments

admin's picture

If the server is personal local development purpose, I think userdir is not necessary and I will disable in apache conf and then no more conflict.

admin's picture

I just wanted to say that your blog has been really useful.

admin's picture

Very informative text, thanks for sharing it!

Add new comment

Restricted HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <h4> <h5> <h6>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • HTML tags will be transformed to conform to HTML standards.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.