Forum Moderators: phranque

Message Too Old, No Replies

Simple RewriteCond

         

Teschio

2:40 pm on Mar 1, 2005 (gmt 0)

10+ Year Member



Hi to all!
I have to do these redirections with mod_rewrite:

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

jdMorgan

3:48 pm on Mar 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That looks too complicated, and the rules are out of sequence, making it difficult to optimize.

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]

Replace all broken pipe "¦" characters with solid pipe characters before use.

Jim

[edit] de-linked URLs [/edit]

[edited by: jdMorgan at 6:54 am (utc) on Mar. 29, 2005]

Teschio

12:42 pm on Mar 2, 2005 (gmt 0)

10+ Year Member



Thank a lot!
It worked!

ChrisBlessing

9:27 pm on Mar 29, 2005 (gmt 0)

10+ Year Member



Hi jdMorgan,

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]

Basically I want to optionally match the "www." part.

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]

What's the (.*) part doing, and the [R=...] part?

Thanks very much!

ChrisBlessing

10:15 pm on Mar 29, 2005 (gmt 0)

10+ Year Member



OK I did a little investigation decyphering the apache mod_rewrite docs ;)

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. :)

jdMorgan

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

WebmasterWorld Senior Member 10+ Year Member



The correct syntax would be:

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

ChrisBlessing

10:36 pm on Mar 29, 2005 (gmt 0)

10+ Year Member



Thanks Jim, much appreciated!

ChrisBlessing

12:21 am on Mar 30, 2005 (gmt 0)

10+ Year Member



So it seems with each solution, my problem grows in scope.

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

jdMorgan

1:31 am on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you test it yourself? If so, what's wrong with it?

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]

You could also make it work with any form of sub-domains, including non-alpha, using:

# 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]

Jim

ChrisBlessing

1:35 am on Mar 30, 2005 (gmt 0)

10+ Year Member



Jim you are the man! I did test it and it seems to work, I just wanted confirmation. Plus I figured I'd learn a bit more about references, and sure enough, you tweaked it so I could. Thanks.

bellomatic

6:47 am on Apr 1, 2005 (gmt 0)

10+ Year Member



Hi,

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]

jdMorgan

3:42 pm on Apr 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since your code is going into httpd.conf, not .htaccess, you need to do one of two things. Change the rule to either:

RewriteRule [b]^/([/b].*) http://www.%1/$1 [R=301,L]

-or-

RewriteRule (.*) http://www.%[b]1$[/b]1 [R=301,L]

Jim

ChrisBlessing

4:50 pm on Apr 1, 2005 (gmt 0)

10+ Year Member



Wow someone is already using my little subdomain sniffer, nice. lol

bellomatic

8:40 pm on Apr 3, 2005 (gmt 0)

10+ Year Member



Thanks very much. Works fab.