Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite redirect complications

Can't get syntax correct using RewriteCond and mod_rewrite

         

gnashbar

10:42 pm on Feb 23, 2009 (gmt 0)

10+ Year Member



My scenario is this. My company has multiple domain names under our purview. In DNS we already have www.site2.com pointing to the same IP that www.site1.com is using. I need to take this a step further and make incoming requests to www.site2.com point to www.site1.com/page2 if this makes sense. Within the VirtualHost section of my httpd.conf file I have the ServerName listed for the www.site1.com site. At the beginning of the rules within this VirtualHost section I have the following condition

RewriteCond %(SERVER_NAME) ^www.site2.com.*
RewriteRule (.*)$ www.site1.com/page2 [R=301,L]

I think the 301 directive might indicate that I'm attempting to use mod_alias but something else is wrong with the syntax I'm using because no matter what I do the request for www.site2.com is being routed to www.site1.com rather than www.site1.com/page2. Any help would be greatly appreciated.

Thank you

g1smd

11:35 pm on Feb 23, 2009 (gmt 0)

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



You'll be needing to test HTTP_HOST here. You'll also need to deal with non-www requests for both domains in a proper way.

However, something more fundamental comes to mind. Do you need a redirect or a rewrite? Which URL should users be using and seeing for that content?

A redirect forces the user to see a different URL before the content is served. A rewrite connects their request to the content at a location different to that implied by the URL, and without revealing what that location actually is.

There have been several similar topics in the last few days where longer examples can be found.

gnashbar

5:28 pm on Feb 24, 2009 (gmt 0)

10+ Year Member



Thanks for the quick reply. Using the, HTTP_HOST, variable was definitely the way to go. I was also using parens instead of braces around the variable name which was preventing the string from matching. I think the answer to your "more fundamental" question is more of a business rule but if I were to want to use a rewrite rule (which I'm currently doing by the way, although I'm giving it a [R=302,L] at the end)so that the content would originate from a different location than the URL implies, how would I do that? I'm not sure in this particular case that it matters as the user will see their newly formed redirected URL but I would like to know how to get it working the other way, and I apologize if this question has already been answered in a different thread.

jdMorgan

6:02 pm on Feb 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



By using an external redirect, you are telling the search engines to dump all of the old URLs, and re-index the site using the new ones. You may expect a temporary ranking drop on all of these URLs -- lasting from a week to many months, and depending on your pages' current ranking and the search engines' whims.

If you simply move content inside the filesystem of your server, then there is no need to change URLs; Use an intenal rewrite instead, to tell the server where it should now look to find the content associated with the pre-existing URLs.

If you are co-hosting different sites in the same filespace, then again, there is no need to expose your internal filesytem architecture in a redirected URL. Simply internally-rewrite the requests for the pre-existing URLs to the correct location in the filesystem.

If, however, all of the sites' content is identical, then best practice is to externally 301-redirect all non-canonical URLs to the canonical URLs, and link only to one domain on the Web. The alternate domains can be used in print, radio, or TV advertising, but should not be promoted on the Web; Search engines take a dim view of trying to make the same content rank on multiple domains. In addition, the multiple sites compete against each other for ranking.

External redirect syntax:

 RewriteRule URL-path-pattern http-or-https:optional-port-number//domain/substitution-URL-path [R=301,L] 

Internal rewrite syntax:

 RewriteRule URL-path-pattern /substitution-filepath [L] 

Jim

[edited by: jdMorgan at 6:04 pm (utc) on Feb. 24, 2009]