Forum Moderators: phranque

Message Too Old, No Replies

Redirect/Rewrite between multiple domains

         

o0Corps0o

10:40 am on May 12, 2010 (gmt 0)

10+ Year Member



I am new to Apache .htaccess coding so bare with me.

Basically we have a main website which hosts a joomla setup. We also have several other domain names which all point to the same website but retains the domain in the address bar.

What i am trying to do it make it so that the index.php file of one of the other domains is redirected (or rewritten) to point to a different page. This is proving to be difficult because all the pages (because its joomla) all are index.php?blahblahblah.

Here is the code which i am trying to get working (although i can't get my head around it yet):

RewriteCond %{HTTP_HOST} ^www.otherdomain.org$ [NC]
RewriteRule ^(index.php)$ httq://www.otherdomain.org/newlink [L]

(*http to httq is changed so the link remains here)

the issue is possibly to do with index.php where i need it to pick up only index.php with none of the trailing querystring parts (index.php?option=com_content&view=category&layout=blog&id=1&Itemid=50). But as it stands i believe it picks up index.php with all querystring and goes into a loop..

So to wrap up, i'm trying to get the www.otherdomain.org/index.php to point to www.otherdomain.org/index.php?option=com_content&view=category&id=1 where i believe i have to find out what domain name was entered before i can rewrite the index.php?

If anyone could shed some light on this it would be great, cause i need to set it up for several different domains.



(confusing myself)

jdMorgan

12:54 pm on May 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> We also have several other domain names which all point to the same website but retains the domain in the address bar.

I'd suggest you research this point first, as you are essentially creating duplicate content here - Multiple URLs which resolve to the same content, and essentially multiple sites competing with each other for search ranking and links. Unless you have no other competitors in your market niche, this may not be a very good idea...

If you want to redirect only the root-directory index.php page, and only when it has no query string, then you must test the query string:

# Externally redirect non-blank non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} ^([^.]+\.)*otherdomain\. [NC]
RewriteCond %{HTTP_HOST} !^(www\.otherdomain\.org)?$
RewriteRule ^(.*)$ http://www.otherdomain\.org/$1 [R=301,L]
#
# Internally rewrite www.otherdomain.org/index.php requests
# with blank query string to alternate/non-default filepath
RewriteCond %{HTTP_HOST} ^www\.otherdomain\.org$
RewriteCond %{QUERY_STRING} =""
RewriteRule ^index\.php$ http://www.otherdomain.org/newfilepath [L]

All of your hostnames should be canonicalized (as shown) before doing any internal rewrites. Otherwise -again- you create duplicate content issues, and the URLs/sites (for example www- and non-www) can compete with each other and leave you constantly fighting links to the "wrong domain" and incorrect domain listings in search results listings.

If you also wish to rewrite the URL "www.otherdomain/" to "/newfilepath", then you should also add a rule to force the canonical URL on /index.php requests. This rule should be inserted before the domain canonicalization rule:

# Externally redirect requests for "/index.php" to "/" (retaining any query strings)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php([?#][^\ ]*)?\ HTTP/
RewriteRule ^index\.php$ http:www.otherdomain.org/ [R=301,L]

and then change the last rule above to

RewriteRule ^$ http://www.otherdomain.org/newfilepath [L]

Jim

o0Corps0o

3:02 pm on May 13, 2010 (gmt 0)

10+ Year Member



Hi Jim,

Thanks for the swift reply, very helpful, and has made me look into a different way of approaching this. Basically we have one main site (domain) where everything is hosted, and this is basically just made up of index.php with querystrings... I wonder if you can point me in the right direction for doing the following:

say we have main website www.main.org
and then several other domains www.otherdomain.org (just use one for example)

I want to add into the htaccess file something like this:

RewriteCond %{HTTP_HOST} ^www\.otherdomain\.org$
RewriteRule ^index\.php$ httq://www.main.org/otherdomain [L]

which seems to work and points otherdomain.org to main.org/otherdomain , the problem i am having is what if i want to point www.otherdomain.org/advert to www.main.org/page

at the moment if i put into the browser www.otherdomain.org/advert it would just take me to the www.main.org/otherdomain (as with any other extra additions)

I have a problem in that we have JoomlaSEF also in the htaccess which does 301 redirects so main.org/page would actually be pointing to index.php?content=something. So unsure if this is just messing it all up. Or wether i have to have these redirects before it runs the joomsef part?

Thanks

g1smd

3:23 pm on May 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Once you start thinking about this in terms of URLs on the web and their mappings to server internal filepaths and filenames, this all becomes much simpler to solve.

URLs don't exist inside a server. They are a naming convention used out on the web.

Run all redirects first, before the rewrite serves the content.

o0Corps0o

4:17 pm on May 13, 2010 (gmt 0)

10+ Year Member



Hi g1smd,

Thanks for the comment and after having a think about internal folders/filepaths etc, I think I worked what I was looking for:

RewriteCond %{HTTP_HOST} ^www\.other\.org$
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ httq://www.main.org/newfolder [L]

RewriteCond %{HTTP_HOST} ^www\.other\.org$
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php$ httq://www.main.org/main [L]

which i think is working fine, where basically if someone typed in www.other.org/whatever it would then point to the new place.. I'm assuming i'll have to do this for every different subfolder (i'm saying subfolders but they're not actual folders, they are just pointing to an existing page on the main site)

Feel free to add comments if i'm doing it wrong, or anything i can do to optimise would be great..

Thanks again.

g1smd

11:48 pm on May 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What you have now is a 302 redirect (because the target contains a domain name). A redirect makes the browser ask for a different URL.

Are you sure you don't want a rewrite? A rewrite connects a URL request to an internal filepath. Internal filepaths don't have a domain name.

o0Corps0o

10:05 am on May 14, 2010 (gmt 0)

10+ Year Member



I'm not sure a rewrite would work the way you are suggesting cause there isn't really a file structure as it were. Because it is joomla based (database driven) there is no directory structure and everything is pointing to index.php which has query strings depending on what page you want to go to.

Thanks.

g1smd

10:12 am on May 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, so the URL
example.com/somepage
is connected to the internal filepath
/index.php?some-parameter=value
by the action of a rewrite.

You just need to change the default action and override it with more specific rules for each domain.