Forum Moderators: phranque

Message Too Old, No Replies

.htaccess: Redirection help.

When I go to e.g. http://example.mysite.com/, it takes the content from htt

         

wanze

12:22 am on Mar 16, 2005 (gmt 0)

10+ Year Member



Hello world. I found this .htaccces 'script' in here, and I love it, but I want to change something.

When I go to e.g. [example.mysite.com...] it takes the content from [mysite.com...] but I want to take it from [mysite.com...]

My .htaccess:


RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.mysite\.com(:80)?<>/([^/]*) [NC]
RewriteCond %1<>%3!^(.*)<>\1$ [NC]
RewriteRule ^(.*) /%1/$1 [L]

I've tried to change the last line, "RewriteRule ^(.*) /%1/$1 [L]" to "RewriteRule ^(.*) /sub/%1/$1 [L]", which I hoped would solve my problem, but it doesn't.

Hope somebody knows what to do. Thank you in advance.

Regards,
wanze

jdMorgan

12:29 am on Mar 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



wanze,

Welcome to WebmasterWorld!

You'll need to add "/sub" to both the RewriteRule and the third RewriteCond in order for this to work.

Jim

sitz

1:01 am on Mar 16, 2005 (gmt 0)

10+ Year Member



jd;

OOC, where did you add the /sub/ in the RewriteCond? In my attempts, I wound up creating an infinite loop (or a match failure, depending on exactly what I did), and had to add:


RewriteCond %{REQUEST_URI}! ^/sub/

...between the second and third RewriteCond's to make this work. What did I miss?

jdMorgan

2:27 am on Mar 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this as the third RewriteCond in the first example you posted -- It's the correct way to prevent the loop (for this code), similar to the RewriteCond you added.

RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.mysite\.com(:80)?<>[b]/sub[/b]/([^/]*) [NC]

Jim

wanze

6:09 am on Mar 16, 2005 (gmt 0)

10+ Year Member



Now I got:

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.mysite\.com(:80)?<>/sub/([^/]*) [NC]
RewriteCond %1<>%3!^(.*)<>\1$ [NC]
RewriteRule ^(.*) /sub/%1/$1 [L]

But when I e.g. go to [example.mysite.com...] it just says: "You don't have permission to access / on this server"

wanze

2:48 pm on Mar 16, 2005 (gmt 0)

10+ Year Member



Actually, if I could 'move' the root into a directory named www, that would be even better. The point is just so people can't access the subdomains from e.g. [mysite.com...] I removed the line: "RewriteCond %{HTTP_HOST}!^www\. [NC]", which worked. But how do I do the same thing when people doesn't use [mysite.com...] but [mysite.com...]

I simply want [mysite.com...] and [mysite.com...] to take the content from [mysite.com...] so it actually is impossible to access the REAL root(http://www.mysite.com/).

jdMorgan

4:37 pm on Mar 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I hope I'm understanding what you need... Subdomains cease to have any real meaning once they're resolved to a directory in httpd.conf, so you have to think in terms of directory structures after that point, which confuses discussions like this a bit.

If the only subdomain you want to support is "www", then a much simpler solution can be used. Something like this in your Web root .htaccess file should do it:


# Force www canonical hostname
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com [R=301,L]
#
# Rewrite all requests to www subdirectory, but prevent rewrite looping
RewriteCond $1 !^www/
RewriteRule (.*) /www/$1 [L]
#
# Forbid direct-request access to /www subfolder
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /www/
RewriteRule .* - [F]

First, we redirect any requests for the non-www domain to the www domain. This will help "standardize" all links and references to your site, and prevent duplicate-content problems as well.

Next, we rewrite any request for your Web root directory to the /www subdirectory.

However, you then have the problem that someone may link directly to "www.example.com/www/", which is an unnecessarily long, ugly URL, and which presents a duplicate-content problem, since the same pages will also appear at "www.example.com/". So the last rule forbids any external direct request for the /www subdirectory URL, but does not affect the internal rewrite.

If your server seems to be ignoring the rewrite to the subdirectory, then you may need to set RewriteOptions inherit in .htaccess in the /www subdirectory.

Jim

wanze

4:54 pm on Mar 16, 2005 (gmt 0)

10+ Year Member



I haven't got access to the Apache configurations.

So, I'll explain it again.

When I log in on my FTP, I'm in my root.
If I upload hello.txt to / on the FTP, it'll be at [mysite.com...] and [mysite.com...]

I want everything in subdirectories.
e.g:

Thanks for your time! :)

wanze

10:22 pm on Mar 16, 2005 (gmt 0)

10+ Year Member



uhm, I hate www, so I actually wanted to remove it later on, which means [mysite.com...] should take content from /www/, and not just redirect to [mysite.com...]

WWW sucks - [no-org...]
_____
EDIT:

I didn't mention it, because I thought you just would make something which would do the same as the WWW-thing did, so it wouldn't be as difficult to understand.

jdMorgan

4:43 am on Mar 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rather than loving www, I just accept that it does have advantages, such as the one mentioned in msg#8 of this thread [webmasterworld.com].

Did the original code work OK other than your specific reported problem? (I'm trying to make sure you've got POSIX 1003.2 support before going on, since this will make a major difference in what might need to be done).

For now, and if you still hate www after considering the post I cited, simply reverse the domain names in the first rule:

# Force www canonical hostname
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule (.*) http://example.com [R=301,L]

We'll take care of the rest after verifying that your server will support the POSIX 1003.2 atomic back-reference (the "\1$" in the original code) which is required to prevent looping on arbitrary subdomain to subdirectory rewrites.

Jim

wanze

6:21 am on Mar 17, 2005 (gmt 0)

10+ Year Member



Yes. Now it redirects [mysite.com...] to [mysite.com...]

wanze

3:39 pm on Mar 17, 2005 (gmt 0)

10+ Year Member



But still, when somebody goes to [mysite.com...] I want them to get the content from /www/.

wanze

3:26 pm on Mar 18, 2005 (gmt 0)

10+ Year Member



jdMorgan has left me. :(

Caterham

3:40 pm on Mar 20, 2005 (gmt 0)

10+ Year Member



jdMorgan has left me. :(

answers might take some time in forums... this is not instant messaging ;)

RewriteEngine On
#
# external redirect from www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule ^(.*) http://example.com/$1 [R=301,L]
#
# Host starts with example.com (without any subdomain)
RewriteCond %{HTTP_HOST} ^example\.com
# prevent looping
RewriteCond $1 !^www/
# internal redirect to /www/ (example.com goes into the www folder)
RewriteRule ^(.*) /www/$1 [L]
#
# host is xyz.example.com (this will not affect www.example.com, because an
# external redirect took place above to example.com
# this will not match for www.sub.example.com
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.example\.com(:80)?<>/([^/]*) [NC]
# prevent looping if we're already in /xyz/
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
RewriteRule ^(.*) /%1/$1 [L]