Hi Carlos,
Okay, the subdomain seemed to be a way to resolve your problem IF the subdomain actually linked directly to the subdirectory of the main domain.
My point is that you will "lose" your supporting files if they're relative linked (rather than absolute links). As the tutorial states, you can get around this with either absolute links OR the <BASE> tag.
As for the <IfModule>, it's okay to use in the httpd.conf file but, because the .htaccess must be parsed MULTIPLE times for each and every file request, it makes no sense to run a test repeatedly. Glad that you got that.
Also, understand that WordPress et al do that so amateur webmasters don't whine about having their website disabled by their code - which code Apache doesn't understand would do!
Hosts know these days that .htaccess and mod_rewrite are indispensable to webmasters so they're unlikely to disable them (and they are just as unlikely to get rid of Apache).
And if your GPS told you to take a left in the middle of a bridge ... Aw, the important thing is to UNDERSTAND what the code you use is doing for you. If you don't, why would you use it? Anyway, the RewriteBase directive was designed to undo a mod_alias redirection. In effect, it changes the root directory of your mod_rewrite code to that specified. If your .htaccess is in the DocumentRoot, the RewriteBase / becomes immediately superfluous and only slows down your serving requested files.
As for RewriteEngine on, it's to ensure that Apache's mod_rewrite is not in the comment mode, i.e., RewriteEngine off operates like /* and RewriteEngine on operates like */. It's more of a safety measure to use it (ONCE - I'm stunned at those who keep repeating RewriteEngine on like a mantra!) at the start of your mod_rewrite code section. Oh, I never said to remove RewriteEngine on - I left it in black print and used the red for "errors" or code to delete.
Okay, if ~username is a constant, great! All that much easier to make calls to your website via a subdomain. Yeah, yeah, yeah, you already said that you didn't want to do that. Anyway, the point was that the syntax of the RewriteCond statement was incorrect => 500 error! I note that you want to use the No Case flag on a {REQUEST_URI} variable so let me just remind you that 'nix servers ARE case sensitive so, if you direct the mod_rewrite to be case insensitive, you WILL end up with 404 errors, i.e., remove the [NC] flag!
Using %{REQUEST} takes all the guesswork out of whether there should be a leading / or not and the .? is a safer way to account for just the DocumentRoot (http://domain) request. Obviously, .? is zero or one of any character is required (same as (.*) without creating a duplicate Apache variable for {REQUEST_URI}) and it will ALWAYS return a true (match).
Code:
RewriteEngine on
# Only apply to a subdomain (or other colocated domain)
RewriteCond %{HTTP_HOST} !^somedomain.com\.local$ [NC]
# The No Case flag is fine here as the {HTTP_HOST} is not case sensitive
# The request is not an existing file
RewriteCond %{REQUEST_FILENAME} !-f
# The request is not an existing directory
RewriteCond %{REQUEST_FILENAME} !-d
# The request is not to a directory (or file) containing username
# This should NOT use the No Case flag!
RewriteCond %{REQUEST_URI} !username [NC]
# Redirect to {REQUEST_URI} within the ~username subdirectory
RewriteRule .? ~username/index.php%{REQUEST_URI} [R,E]
# What was the index.php doing there?
# Redirection flag is 302 status (temporary) by default so it's better to specify R=301
# Another syntax error with the Environment flag:
With the [E], or [env] flag, you can set the value of an environment variable.
Note that some environment variables may be set after the rule is run, thus
unsetting what you have set. See the Environment Variables document for
more details on how Environment variables work.
The full syntax for this flag is:
[E=VAR:VAL] [E=!VAR]
VAL may contain backreferences ($N or %N) which will be expanded.
Using the short form, [E=VAR], you can set the environment variable
named VAR to an empty value.
The form, [E=!VAR], allows to unset a previously set environment variable named VAR.
Environment variables can then be used in a variety of contexts,
including CGI programs, other RewriteRule directives, or CustomLog
directives.
BTW, I thought that E was the Environment flag but you made me look it up (because I never use it)! WHY did you believe you had to use it?
Regards,
DK
Bookmarks