Forum Moderators: phranque

Message Too Old, No Replies

Domain-based redirection

Mod_rewrite to files depending on requested host name

         

vwsequeira

9:54 am on Jul 12, 2006 (gmt 0)

10+ Year Member



Hello,

RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} example\.com
RewriteRule (.*) http://example.com/video.htm$1 [L]

I am trying to redirect the domain 'example.com' to a page video.htm. The video.htm is a file located in the same directory where the domain points. Can you please explin what I am doing wrong?

[edited by: jdMorgan at 5:12 pm (utc) on July 12, 2006]
[edit reason]
[1][edit reason] No specific URLs, please. See TOS. [/edit]
[/edit][/1]

jdMorgan

5:18 pm on Jul 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You want to redirect all file requests on this domain to the single video.htm file? And video.htm is the *only* file that will exist on this domain? -- You have no robots.txt, images, css files, external JavaScripts, or actual video files, just the one video.htm file?

This does not sound right, so I suggest you carefully think about exactly which requests you do and do not wish to redirect to this video.htm file. Until the problem is well-defined, it can be dangerous to write and test code...

Also, do you want 'video.htm' to be listed as your site's home page in search results?
Is this code intended for use in .htaccess or in httpd.conf or conf.d?

Jim

vwsequeira

5:56 am on Jul 14, 2006 (gmt 0)

10+ Year Member



Thank you Jim.

I have 4 doamins and 4 files in the account root. When I access any domain it will show up a default index.html file. I want domains to show differnt files:
domain1.com should show video1.html
domain2.com should show video2.html
domain3.com should show video3.html
domain4.com should show video4.html

All the files are located in the same webdirectory root.

jdMorgan

6:52 am on Jul 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the domain names are not lexically associated with the video names, then the simplest approach is to use case-by-case rules. And unless you really want to expose the name of the video page, use internal rewrites, not external redirects:

RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com
RewriteRule ^$ /video-one.htm [L]
#
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com
RewriteRule ^$ /video-two.htm [L]
#
RewriteCond %{HTTP_HOST} ^(www\.)?domain3\.com
RewriteRule ^$ /video-three.htm [L]
#
RewriteCond %{HTTP_HOST} ^(www\.)?domain4\.com
RewriteRule ^$ /video-four.htm [L]

If the domain names *are* lexically associated with the video pages, then you can "copy" the identifying part of the domain name into the video page name, and use only a single rule:

RewriteCond %{HTTP_HOST} ^(www\.)?domain([1-4])\.com
RewriteRule ^$ /video%2.htm [L]

-or alternately-

RewriteCond %{HTTP_HOST} ^(www\.)?domain(oneŠtwoŠthreeŠfour)\.com
RewriteRule ^$ /video%2.htm [L]

Important: Replace the broken pipe "Š" characters above with solid pipe characters from your keyboard before use; Posting on this forum modifes pipe characters.

An alternative to the multiple-rule approach is to use an association trick. This would be most useful if you had many domains and many video pages which had no direct name relationships with each other.


RewriteCond %{HTTP_HOST}<>video-uno.php ^(www\.)?domain-apple\.com[^<]*<>(.+)$ [OR]
RewriteCond %{HTTP_HOST}<>movie-two.htm ^(www\.)?domain-pears\.com[^<]*<>(.+)$ [OR]
RewriteCond %{HTTP_HOST}<>cine--tres.pl ^(www\.)?domain-peach\.com[^<]*<>(.+)$ [OR]
RewriteCond %{HTTP_HOST}<>flick-su.html ^(www\.)?domain-mango\.com[^<]*<>(.+)$ [OR]
RewriteCond %{HTTP_HOST}<>films-chi.htm ^(www\.)?domain-plums\.com[^<]*<>(.+)$
RewriteRule ^$ /%2 [L]

Note that the character string "<>" has no special meaning to the regular-expressions parser or to mod_rewrite. It is simply a unique string intended to demarcate the boundaries of the two variables and values in the RewriteConds. You can in fact use any sufficiently-unique string for this purpose.

The "^$" pattern in all of the above rules is intended to match only requests for the domain root, such as "example.com/". This avoids the problems you'll have if you use a pattern that will match (and therefore rewrite or redirect) *any* file. For example, you *do not* want to rewrite or redirect requests for robots.txt, favicon.ico, labels.rdf, labels.xml, or w3c/p3p.xml or any custom error document files -- doing so will cause you very serious problems.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

vwsequeira

9:48 am on Jul 16, 2006 (gmt 0)

10+ Year Member



Jim you are a gem.

I really appreciate your help and thank webmasterworld.

Vincent