Forum Moderators: phranque

Message Too Old, No Replies

Stop Redirection if subdomain is requested.

Domain and Subdomain : Apache Redirect

         

Gopinath

11:10 am on Feb 11, 2009 (gmt 0)

10+ Year Member



Hello,

Can anyone please help me on the problem described below?

I have a number of sub domains under the domain - example.com as product1.example.com, product2.example.com, product3.example.com etc

Both example.com and product1.example.com has web pages with same URL except the domain name
Example
http://example.com/abc/picture.html
http://product1.example.com/abc/picture.html

I have to redirect http://example.com/abc/picture.html to http://example.com/abc/new.html if the request is from example.com. It should not get redirected if the request is from any of its sub domains (product1.example.com)

I have mapped all my sub domains as shown below. The redirect rule which I used is also mentioned.

<VirtualHost xx.xx.xx.xx>
RewriteEngine On
DocumentRoot /web/xyz
ProxyPreserveHost On
RewriteCond %{HTTP_HOST} ^product1\.example\.com [NC]
RewriteRule .* - [L]
RedirectMatch permanent /abc/picture.html /abc/new.html
ProxyPassMatch /abc/picture.html !
ProxyPass / balancer://xyzcluster/
ServerName example.com
ServerAlias www.example.com
ServerAlias product1.example.com
</VirtualHost>

<Proxy balancer://xyzcluster>
BalancerMember http://yy.yy.yy.yy:80
..........
</Proxy>

Thanks
Abhilash Gopinath

[edited by: jdMorgan at 3:16 pm (utc) on Feb. 12, 2009]
[edit reason] example.com [/edit]

jdMorgan

1:59 pm on Feb 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're apparently trying to mix mod_rewrite directives --i.e. RewriteRule and RewriteCond-- with the mod_alias directive RedirectMatch. Understand that the code you place in server config files is *not* a sequentially-executed "program" or "script." Rather, this code is parsed ("scanned") by each Apache module in turn, with each module executing only the directives that it understands. Therefore, the "conditional" function you attempted to implement with the mod_rewrite "skip rule" above *will not* apply to the RedirectMatch directive, and that RedirectMatch directive will execute unconditionally.

Change the RedirectMatch line to a RewriteRule (note that the syntax differs markedly), and the function you expected will likely work. Note also that it could be recoded more simply using only two lines as


RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^/abc/picture\.html$ http://example.com/abc/new.html [R=301,L]

if you have only one redirect to implement for example.com only.

---

Both example.com and product1.example.com has web pages with same URL except the domain name.

I should also point out that it's considered non-optimal to have the same content returned by more than one absolutely-unique URL. If you allow this to happen, then you are making your pages compete with themselves for ranking in search results! The current best practice is to 301-redirect all non-canonical domains and URLs to the canonical domain and URLs, so that any given page on your server can be reached using one and only one unique URL, to include domain/subdomain, http/https, and uppercase/lowercase variations, etc.

One way to look at it is like a car race: Do you want to enter two cars in the race, each with a 200 horsepower engine, or one with a 400 horsepower engine?

Jim

[edited by: jdMorgan at 3:17 pm (utc) on Feb. 12, 2009]

Gopinath

11:51 am on Feb 12, 2009 (gmt 0)

10+ Year Member



Hi Jim,

Thanks a lot for the solution and it worked!.

I actually wanted to do a negative condition.
I wanted to do a redirect if the domain is company.com, else skip the redirects, and actually I had to do many redirects from this domain, so I did the following

RewriteCond %{HTTP_HOST} !^company\.com [NC]
RewriteRule .* - [S=354]
Include conf/MyRedirects.conf

where all my 354 redirects are included in MyRedirects.conf
in a format as you mentioned above and it worked.

I saw your solution on skipping redirects at
[webmasterworld.com...]

Thanks again
Abhilash Gopinath

Gopinath

12:17 pm on Feb 12, 2009 (gmt 0)

10+ Year Member



Hi Jim,

I have one more problem as mentioned below.

I have to redirect to a new website http://example.com/index.html if the user enters
product1.example.com or
product1.example.com/
http://product1.example.com/

But if the user invokes a valid page on product1.company.com it should get served from product1.company.com itself.

I have defined a valid page as any URL with a period and added a redirect rule in the httpd.conf of product1.example.com as shown below.

RedirectMatch permanent ([/]*(^[^.]*$)) http://example.com/index.html

But it didn't work for Servlets since they don't have a period!

Any solutions on the top of your head Jim?

Thanks
Abhilash

[edited by: jdMorgan at 3:20 pm (utc) on Feb. 12, 2009]
[edit reason] example.com [/edit]

jdMorgan

3:12 pm on Feb 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are mixing mod_alias and mod_rewrite directives again. Don't use mod_alias directives any more, unless you enjoy suffering...

RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule !^/([^/]+/)*[^.]+\. http://example.com/ [R=301,L]

In order to prevent a problem with servlets, you must "define" a servlet for mod_rewrite in terms of what its URL looks like. Since I have no idea what your servelet URLs look like, I can't offer useful advice.

Jim

[edited by: jdMorgan at 3:19 pm (utc) on Feb. 12, 2009]