Forum Moderators: phranque

Message Too Old, No Replies

Redirecting a machine name

I am confused about using mod_rewrite

         

sperkins

2:24 pm on Dec 4, 2008 (gmt 0)

10+ Year Member



Hi All, first of all thank you all for your postings, I have learned a lot from the information shared here. I can usually pick up enough to make something work with enough examples, but something in my brain will not allow me to grasp this.

I want to point "machine.mycompanywebsite.com" to a long path on another server such as [mycompanywebsite.com...]

My DNS Host is Verio and they will not allow DNS records ending in a path, only a TLD or IP.

My Website is hosted elsewhere with the company who wrote our ECommerce software, I do not have full access to source there.

A Simple redirect will not work because once I have it working, I am sure I will end up with a few machine names pointing to their own specific categories.

I upgraded my Verio account to their minimal account so I can create an .htaccess on Verios recommendation. Although they do not support .htaccess they gave me the following idea


RewriteEngine On
Options +FollowSymlinks
RewriteBase /
# Rewrite Rule for machine.forexample-domain.net
RewriteCond %{HTTP_HOST} machine.forexample-domain.net$
RewriteCond %{REQUEST_URI} !machine/
RewriteRule ^(.*)$ machine/$1

So I followed their directions and improvised my information to


RewriteEngine On
Options +FollowSymlinks
RewriteBase /
# Rewrite Rule for machine.mycompanywebsite.com
RewriteCond %{HTTP_HOST} machine.mycompanywebsite.com$
RewriteCond %{REQUEST_URI} !/Categories.aspx?Category=a1b2c3d4-1a2b-1234-5678-4d3c2b1a
RewriteRule ^(.*)$ /Categories.aspx?Category=a1b2c3d4-1a2b-1234-5678-4d3c2b1a $1

My results are a 500 Internal Server Error. Can some one offer any suggestions? Thanks in advance!

jdMorgan

3:18 pm on Dec 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your syntax is invalid -- mod_rewrite will not know what to do with that "$1" following the substitution URL-path.

The syntax is:

RewriteRule <space> ^regex-pattern-matching-URL-path$ <space> substitution-URL-or-filepath <space> [flags]

It is not clear what you want to do with the originally-requested URL-path, so I cannot advise on that. With the "$1" removed and the corrections noted below, *all* URLs on machine.example.com will be rewritten or redirected to the single path "/Categories.aspx?Category=a1b2c3d4-1a2b-1234-5678-4d3c2b1a"

In addition, REQUEST_URI contains only the URL, and not the query string data appended to that URL. To be clear, a query string is not part of a URL, it is data appended to a URL to be passed to the resource *at* that URL. The "L" in URL stands for "locator" and a query string is not part of the location of a resource.

To fix this, use an additional RewriteCond to check %{QUERY_STRING} for the value you want to match.

Escape the literal periods in your patterns by preceding them with a "\". Otherwise, they are taken as regex tokens meaning "match any single character." e.g., use:

RewriteCond %{HTTP_HOST} ^subdomain\.example\.com

Don't end-anchor that hostname; You may get requests with a period (FQDN) or a port number appended. With the pattern end-anchored, such requests will cause the rule to fail.

If the destination of this "pointing" is actually hosted on a different server, then you do not need the REQUEST_URI and QUERY_STRING checks, but you do need to use a redirect, rather than an internal rewrite:


# Rewrite Rule for machine.example.com
RewriteCond %{HTTP_HOST} machine.example.com$
RewriteRule ^(.*)$ http://www.example.com/Categories.aspx?Category=a1b2c3d4-1a2b-1234-5678-4d3c2b1a $1 [R=301,L]

This redirect will expose the new location to the client. If you do not want that, then you'll need to set up the machine.example.com server to reverse-proxy these requests to the "www" server. That is an entirely different game, and may require extensive config changes: For example, "www" will see all these proxied requests from machine.example.com as originating at machine.example.com. It will not see the original client's request information unless you set up the machine.example.com server to forward this information in additional HTTP headers, and set up the "www" server to log those additional headers instead of the the regular headers sent by machine.example.com.

Jim

sperkins

4:49 pm on Dec 4, 2008 (gmt 0)

10+ Year Member



Awesome, Thanks for the info. That makes sense.

Using your sample I couldn't get it to redirect. Using the information you and others provided I was able to get the following to work:


RewriteCond %{HTTP_HOST} machine.example.com
RewriteRule ^$ http://www.example.com/Categories.aspx?Category=a1b2c3d4-1a2b-1234-5678-4d3c2b1a [L]

I guess what they say about a blind squirrel is true.

jdMorgan

5:06 pm on Dec 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, when I posted the redirect syntax I copied your code with the invalid $1 on the end and failed to correct that... Need more coffee, I guess... :)

You still need to escape those literal periods, and I recommend being specific as to whether you want a 301-Moved Permanently redirect or a 302-Found redirect:


RewriteCond %{HTTP_HOST} machin[b]e\.e[/b]xampl[b]e\.c[/b]om
RewriteRule ^$ http://www.example.com/Categories.aspx?Category=a1b2c3d4-1a2b-1234-5678-4d3c2b1a [[b]R=301,[/b]L]

If the above code wraps on your screen, be advised that it should all be on one single line.

A 301 will tell search engines to list the new URL in their search results, whereas a 302 will tell them to keep the old URL (as long as the old URL is the one that everybody still links to).

Jim