Forum Moderators: phranque

Message Too Old, No Replies

Unique Redirect Problem with htaccess

Having problems getting htaccess to work.

         

mikewalterz

6:15 pm on Jan 2, 2007 (gmt 0)

10+ Year Member



Hello,
Thanks for checking this post. I have a unique redirection problem with htaccess and cant figure out how to solve it.

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!

jdMorgan

6:52 pm on Jan 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your first RewriteCond is redundant, since the second one handles the case where there either is or is not a "www" in front of the user subdomain name. The regular-expressions pattern "(xyz)?" means that "xyz" is optional: It may be present or blank, but not anything else.

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]

This should work, as long as it is in .htaccess in the Web root directory (your "home page" directory) and as long as there are no other rules in that file or any other mod_alias, mod_proxy, or mod_rewrite rules in the server-level configuration files that interfere with this one.

Flush your browser cache (delete IE Temporary Internet Files) before testing any change to your server configuration code.

Jim

mikewalterz

6:59 pm on Jan 2, 2007 (gmt 0)

10+ Year Member



Hey. Thanks for the reply.

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!

mikewalterz

7:18 pm on Jan 2, 2007 (gmt 0)

10+ Year Member



Hey i got it to work thx to you :D

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!

mikewalterz

12:44 am on Jan 3, 2007 (gmt 0)

10+ Year Member



One last thing. When i try to go to:

[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!

jdMorgan

3:07 am on Jan 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It sounds like your server is configured with "UseCanonicalName on", which is not optimum for what you're trying to do. You could ask your host to turn it off, if they're responsive to such requests.

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]

Replace all broken pipe "¦" characters in the code above with solid pipes before use; Posting on this forum modifies the pipe character.

Jim

mikewalterz

4:12 am on Jan 3, 2007 (gmt 0)

10+ Year Member



Thanks for the reply.

The htaccess you provided does the same thing (if i dont put / in folders, turns them back to normal urls).

I will try contacting my host.

Thanks for all your help!

P.S. is there a way i can change UseCanonicalName myself?

jdMorgan

4:24 am on Jan 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I noticed that I injected an error in there. Fixing this might help:

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]

Replace all broken pipe "¦" characters in the code above with solid pipes before use; Posting on this forum modifies the pipe character.

Jim

mikewalterz

4:33 am on Jan 3, 2007 (gmt 0)

10+ Year Member



Nope it doesn't work for some reason. Guess i have to contact the host :D

Thanks for all your help Jim.

mikewalterz

12:35 am on Jan 5, 2007 (gmt 0)

10+ Year Member



Ok now it works but i have one more error.

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!

jdMorgan

1:31 am on Jan 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Ok now it works but i have one more error.

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

mikewalterz

2:06 am on Jan 5, 2007 (gmt 0)

10+ Year Member



Basically, I dont know why but your latest htaccess code seems to work except for one thing.

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!

jdMorgan

2:10 am on Jan 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



BTW, there was another typo in my original post. It should have been:

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteCond $1!^users/
RewriteRule (.*) /users/[b]%2[/b]/$1 [L]

Which is why you had to use two RewriteConds and said the (www\.)? did not work.

Sometimes, it pays to get some sleep... :)

Jim

mikewalterz

2:18 am on Jan 5, 2007 (gmt 0)

10+ Year Member



Um i dont think %2 gives me anything. It comes out blank whenever i try to use it. Do you have aim or some way i can get in contact with you.

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!

jdMorgan

2:54 am on Jan 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, not sure where we are, but the %2 change must correspond to the use of a single user-subdomain-matching RewriteCond:

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]

Bolding indicates corrections/changes or points to check.

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

mikewalterz

4:11 am on Jan 5, 2007 (gmt 0)

10+ Year Member



I get the same error with this other htaccess code. I dont think [user.domain.com...] is considered a folder without the / so that when it shows the content, it removes the images from it so i only get [user.domain.com...]

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]

jdMorgan

5:45 am on Jan 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Replace all broken pipe "¦" characters in the code above with solid pipes before use; Posting on this forum modifies the pipe character.

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

jdMorgan

6:11 am on Jan 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One more thing: Be sure you are flushing your browser cache (delete IE Temporary Internet files) after making any change to .htaccess. Otherwise, your browser will likely show you an old cached copy of the page.

Jim

mikewalterz

10:34 pm on Jan 5, 2007 (gmt 0)

10+ Year Member



Hello,
Yes i am still using ¦ properly.

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]