Forum Moderators: phranque
I have an htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ([^.]+).domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com$
RewriteCond %{REQUEST_URI}!^/users/
RewriteRule (.*) /users/%1/$1 [L]
It is supposed to redirect [user.domain.com...] to [domain.com...] [OR] [user.domain.com...] to [domain.com...]
I cant get it to work. After this when i try to go to [domain.com...] , it wants to access [domain.com...] (not sure why).
I an new to htaccess and if anyone can please help me with my htaccess file or something, that would be great.
Thanks!
However, you have not checked to be sure that there *is* a subdomain name other than www. If there isn't, your second condition will take "www" as the user, which might explain your problem.
So, eliminating the redundant condition, adding one to check for www.example.com, and cleaning up a few other minor things that could cause obscure problems, we get:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteCond $1 !^users/
RewriteRule (.*) /users/%1/$1 [L]
Flush your browser cache (delete IE Temporary Internet Files) before testing any change to your server configuration code.
Jim
It does fix one problem but raises another one. When i try to put in [user.domain.com...] --> it goes to [domain.com...]
No idea why.
Thanks again!
Here is the code if anyone needs it in the future:
RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.domain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.com
RewriteCond $1!^users/
RewriteRule (.*) /users/%1/$1 [L]]
For some reason? didnt work.
Thanks!
[user.domain.com...]
it works fine but when i try:
[user.domain.com...] (WITHOUT /)
it redirects to [domain.com...]
(is there a way to keep it in subdomain form instead of directory form.)
Thanks!
The redirect is a result of Apache mod_dir trying to resolve a request for an extensionless *file* named "folder". It adds a trailing slash and does a redirect. Normally, it would use whatever domain the original request was addressed to, but UseCanonicalName can override this and throw you back to your "account" domain.
The only other things you can do are to either avoid the use of "folder" URLs without trailing slashes (because, by definition, this means that they are files), or to fix the missing slash problem before mod_dir gets control of the request. The solution is not very pretty, however, because it involves duplication of code, and this can get out of hand:
RewriteEngine on
#
# Case: requested URL does not start with "users", contain "." or end with "/"
RewriteCond $1 !(^users¦\.¦/$)
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.com
# See if resulting URL resolves to an existing directory when a slash is added
RewriteCond %{DOCUMENT_ROOT}/users/$1/ -d
# If so, append a slash and rewrite to subdomain subdirectory
RewriteRule (.*) /users/%1/$1/ [L]
#
# Other cases
RewriteCond $1 !^users/
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.com
RewriteRule (.*) /users/%1/$1 [L]
Jim
RewriteEngine on
#
# Case: requested URL does not start with "users", contain "." or end with "/"
RewriteCond $1 !(^users¦\.¦/$)
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.com
# See if resulting URL resolves to an existing directory when a slash is added
RewriteCond %{DOCUMENT_ROOT}[b]/users/%1/$1/[/b] -d
# If so, append a slash and rewrite to subdomain subdirectory
RewriteRule (.*) /users/%1/$1/ [L]
#
# Other cases
RewriteCond $1 !^users/
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.com
RewriteRule (.*) /users/%1/$1 [L]
Jim
When i try to go to
[user.domain.com...]
and look at all the images, the url for the images is:
[user.domain.com...]
instead of
[user.domain.com...]
Any way to fix this?
I appreciate your help a lot!
What changed from "that doesn't work" to "works now"?
How are the links on the images created? Are they static? If so, they'll need to refer to the appropriate user subdomain unless you make provisions in the subdomain->subdirectory rewrite to "share" the image directory across all subdomains.
If they are dynamically-generated, then the script may be constructing the links improperly because we have "moved the page one level down" in the server filesystem, but not in the Web URL-space. (Sorry, this is hard to describe accurately without having something to draw pictures on.)
Jim
Here's how the system works:
A user creates an account on the server and a directory is created for him. The user can use a web browser file system to add new files and folders (such as images). Now lets say the user adds a folder called images and uploads a image, something.gif.
The subdomain is created through htaccess to go to that directory. But what happens is that when the user visits: [user.domain.com...] , the link stays the same (which means that works) but the image link would come out to be [user.domain.com...] INSTEAD OF [user.domain.com...]
I think the way htaccess works, the subdirectories dont get added properly or something. The file structure would be /home/public_html/domain/users/user/images.
Thanks again!
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteCond $1!^users/
RewriteRule (.*) /users/[b]%2[/b]/$1 [L]
Sometimes, it pays to get some sleep... :)
Jim
so if its like /users/%2/hi, it comes out /users//hi
Once solved, i could put the solution here! (it would be quicker)
--
P.S. My htaccess looks like:
RewriteEngine on
#
# Case: requested URL does not start with "users", contain "." or end with "/"
RewriteCond $1!(^users¦\.¦/$)
RewriteCond %{HTTP_HOST}!^www\.domain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.com
# See if resulting URL resolves to an existing directory when a slash is added
RewriteCond %{DOCUMENT_ROOT}/users/%1/$1/ -d
# If so, append a slash and rewrite to subdomain subdirectory
RewriteRule (.*) /users/%1/$1/ [L]
#
# Other cases
RewriteCond $1!^users/
RewriteCond %{HTTP_HOST}!^www\.domain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.com
RewriteRule (.*) /users/%2/$1 [L]
I Think it occurs because it doesn't treat [user.domain.com...] as a directory!
Thanks!
RewriteEngine on
#
# Case: requested URL does not start with "users", contain "." or end with "/"
RewriteCond $1 !(^users¦\.¦/$)
# If NOT www.domain.com
RewriteCond %{HTTP_HOST} !^www\.domain\.com
# Extract user_subdomain if present (else abort this rule)
RewriteCond %{HTTP_HOST} ^[b](www\.)?[/b]([^.]+)\.domain\.com
# See if doc_root/users/user_subdomain/requested_url_path with slash appended resolves to an existing directory
RewriteCond %{DOCUMENT_ROOT}/users/[b]%2[/b]/$1/ -d
# If so, append the slash and rewrite to subdomain subdirectory
RewriteRule (.*) /users/[b]%2[/b]/$1/ [L]
#
# Other cases
RewriteCond $1 !^users/
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ^[b](www\.)?[/b]([^.]+)\.domain\.com
RewriteRule (.*) /users/[b]%2[/b]/$1 [L]
The %2 refers to (that is, it "back-references") the contents matching the second parenthesized regular-expressions sub-pattern in the most-recently-matched RewriteCond. In the case of this revised code, %2 is now the user-subdomain string. %1 will be either "www", if present in the requested hostname, or blank.
Jim
Thanks again.
P.S. check out an example at <snip>. see the picture urls.
[edited by: jdMorgan at 5:41 am (utc) on Jan. 5, 2007]
[edit reason] No URLs, please. See TOS. [/edit]
We're still doing that on any copies from this forum, right?
You're going to have to get to the source code and the server error log to proceed. Looking at the external symptoms is only marginally useful -- Diagnostic probing is required.
Jim
Error log download (txt file): - Last 300 entries
<snip>
If you need cpanel access to fix this, i can give it to you because i trust you :D
Thanks again.
[edited by: jdMorgan at 2:30 am (utc) on Jan. 8, 2007]
[edit reason] Deleted link for privacy reasons [/edit]