Forum Moderators: phranque
a)mysite.com ---> www.mysite.com
b)forum.mysite.com ---> to/the/forum/path
c)www.everything.mysite.com ---> www.mysite.com
d)everything.mysite.com ---> www.mysite.com
I wrote this code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite\.com
RewriteRule ^(.*) [mysite.com$1...]
RewriteCond %{HTTP_HOST} ^forum\.mysite\.com
RewriteRule ^(.*) /home/www/mysite.com/forum/$1
RewriteCond %{HTTP_HOST} ^!www\.mysite\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.mysite\.com
RewriteRule ^(.*) [mysite.com$1...]
But it does not work properly...
Problem is at c,d steps... i think it loops!
How do i make it works?
Thanks you all!
Bye
Always use the [L] flag unless you want the output of a RewriteRule to be processed by following RewriteRules.
Use the [R] flag and a canonical URL to specify an external redirect. Use a local URL-path only to specify an internal rewrite:
RewriteEngine on
# Rewrite forum subdomain to /home/www/mysite.com/forum subdirectory
RewriteCond %{HTTP_HOST} ^forum\.mysite\.com
RewriteRule (.*) /home/www/mysite.com/forum/$1 [L]
#
# Redirect all other non-blank non-www domains to www domain
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^(www¦forum)\.mysite\.com
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Jim
[edit] de-linked URLs [/edit]
[edited by: jdMorgan at 6:54 am (utc) on Mar. 29, 2005]
Got pointed over here to solve a problem, so thanks for the solution.
So now I'm faced with a new issue! What I want to do is catch incoming requests for certain domains which are virtually hosted in the same content directory. Here's the scenario:
content directory: /home/www/thesite
domains "virtually" hosted here:
mydomain.com
mysite.com
myexample.com
What I want to do is catch mydomain.com and mysite.com requests coming in here. Is this the proper condition, for catching all www. and non-www. requests?
[3][b]
RewriteCond %{HTTP_HOST} !^[www\.](mydomain¦mysite)\.com
[/b][/3] Also as a quick learning point, can you explain this in more detail?
[3][b]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
[/b][/3] Thanks very much!
The [R=301,L] is the 301 redirect code apache returns, and L just means "last". That all makes sense now.
And I've determined my [www\.] syntax is incorrect, that's just character matching correct?
So I just made a new RewriteCond to check for the presence of the www. as such:
[2]
RewriteCond %{HTTP_HOST} !^(mydomain¦mysite)\.com
RewriteCond %{HTTP_HOST} !^(www)\.(mydomain¦mysite)\.com
[/2] I presume that multiple conditions executed in one RewriteRule are handled in an "AND" boolean fashion, hence the optional [OR] flag?
Lastly, I've found the (*.) syntax to just be a pattern match for the original URI requested (or modified) for any character string, is that correct?
Thanks for any light you can shed, I'm trying to learn this on the quick. :)
RewriteCond %{HTTP_HOST} !^(www\.)?(mydomain¦mysite)\.com
> RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
>
> What's the (.*) part doing, and the [R=...] part?
The ".*" matches all possible requested local URL-paths, and the () creates a back-reference to (copy of) that URL-path, which is then referenced as $1 in the new URL. {R=301] specifies that this is an external redirect, using a 301-Moved Permanently response.
You might want to review the documents cited in our forum charter [webmasterworld.com] for more information about mod_rewrite and regular expressions.
Jim
Given this situation of domains/subdomains:
mysite.com
blah.mysite.com
www.mysite.com
dude.com
foo.dude.com
yeah.dude.com
www.dude.com
I want to make sure anyone coming in at mysite.com (or any other domain such as dude.com) gets 'rewritten' to www.mysite.com (www.dude.com), but anyone coming to blah.mysite.com stays at blah.mysite.com.
This is across several domains, so I need something global. Initially I was experimenting with the HTTP_HOST var, but I can't seem to get it to cooperate.
Let me make an attempt here, then perhaps someone (you? ;) can correct what I've foob'd up:
# check to make sure it's not a www or subdomain request
RewriteCond %{HTTP_HOST} ^[a-zA-Z]+\.com
# if that's the case, rewrite to www.HTTP_HOST
RewriteRule (.*) [%{HTTP_HOST}...] [R=301,L]
So if the request is not for a subdomain of the main domain (i.e. blah.mysite.com or www.mysite.com), rewrite them to www.mysite.com (or www.domain.com or www.whateverdomainisrequested.com).
Does this seem like it would work? The condition is the tricky part, I think the "+" quantifier is right here but I'm not sure. I want to make sure that the request is not already for a subdomain nor the www alias, so in theory is should be alphas followed by a .com.
[edit -- switched condition and truncated post!]
It looke basically OK to me, if a bit inefficient. I'd tweak it like this:
# check to make sure it's not a www or subdomain request
RewriteCond %{HTTP_HOST} ^([a-z]+\.com) [NC]
# if that's the case, redirect to www.HTTP_HOST
RewriteRule (.*) http://www.%1/$1 [R=301,L]
# check to make sure it's not a www or subdomain request
RewriteCond %{HTTP_HOST} ^([^.]+\.com) [NC]
# if that's the case, redirect to www.HTTP_HOST
RewriteRule (.*) http://www.%1/$1 [R=301,L]
I have followed this advice but the redirect from [example.com...] goes to [example.com...] (with two trailing slashes).
I have rebooted my server a hundred times and gone for a walk to clear my head, but I can't see what I am doing wrong.
Here's the .htaccess file:
<VirtualHost ***.168.0.1>
ServerName www.example.com
DocumentRoot /www/vhtdocs/example.com
ScriptAlias /cgi-bin/ /www/vhtdocs/example.com/cgi-bin/
CustomLog /www/logs/example.com.log combined
DirectoryIndex index.html index.php index.shtml
RewriteEngine on
# check to make sure it's not a www or subdomain request
RewriteCond %{HTTP_HOST} ^([a-z]+\.com) [NC]
# if that's the case, redirect to www.HTTP_HOST
RewriteRule (.*) [%1...] [R=301,L]
</VirtualHost>
Thanks in advance for any advice.
andy
[edited by: jdMorgan at 3:44 pm (utc) on April 1, 2005]
[edit reason] Removed specifics per TOS. [/edit]