homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

how to do a rewrite of a hostname

 12:35 am on Jun 19, 2010 (gmt 0)

I'm sure this is a very simple process but I can't seem to get it to work.

I have a host that can be accessed from myhost.ops and from myhost.ops.expert.com. I have an ssl cert that only works for myhost.ops.expert.com so if someone tries to access the http server from myhost.ops they are successful but ssl doesn't work.
So I want to rewrite myhost.ops to be myhost.ops.expert.com and I have had no luck with this. Any help would be appreciated?




 7:45 am on Jun 19, 2010 (gmt 0)

You'll need a redirect. There's close on 30 000 examples of similar code in this forum.

Use a RewriteCond to check which host is requested, and if it is not the correct one, the following RewriteRule will redirect the user to the correct URL. If you want to retain the requested path, capture it in a (.*) pattern and reuse it as $1 in the target. Use the [R=301,L] flags to finish the job.

Let's see your code.


 6:05 pm on Jun 21, 2010 (gmt 0)

thanks for the response. I did search in the forums, but I guess I just wasn't looking for the right keywords.

here is the code:
RewriteCond %{HTTP_HOST} ^myhost\.ops\.expert\.com [NC]
#RewriteCond %{HTTP_HOST} ^myhost\.ops [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*) https://myhost.ops.expert/$1 [L,R=301,L]

The commented out line is another RewriteCond that i tried with no luck.


 6:32 pm on Jun 21, 2010 (gmt 0)

The "check for blank" RewriteCond is redundant in this case, since a match on the previous RewriteCond indicates that the hostname was not blank. The blank-check is primarily useful when a negative-match on a hostname is being used.

In addition, using the "exact-string match" operator when possible is faster, instead of using the regex library.

So the code reduces to:

RewriteCond %{HTTP_HOST} ^myhost\.ops\.expert\.com [NC]
RewriteCond %{SERVER_PORT} !=80
RewriteRule ^/(.*)$ https://myhost.ops.expert/$1 [L,R=301,L]

If the goal here is to only redirect HTTPS requests, then consider using the pattern "=443" instead of using the pattern "!=80".



 6:45 pm on Jun 21, 2010 (gmt 0)

this is what i changed it do (it didn't work):

RewriteCond %{HTTP_HOST} ^myhost\.ops\.expert\.com [NC]
RewriteCond %{SERVER_PORT} =4443
RewriteRule ^/(.*) https://myhost.ops.expert.com/$1 [L,R=301,L]

it is listening on port 4443

what i am trying to accomplish is the following; I want to rewrite:


 7:30 pm on Jun 21, 2010 (gmt 0)

Check your usage of terms.

Do you want to externally redirect a client from one URL to another URL, or do you want to internally rewrite a request for a URL to a non-default server filepath?

Which is the "wrong" hostname, and which is the "right" hostname?

Which host is this code installed on? Or are they actually the same host?

Is this code located within a <Directory /apex/apex/> container?



 9:33 pm on Jun 21, 2010 (gmt 0)

I'm an oracle dba and I am just using Apache to service an oracle web client(apex) so my understanding of this is very limited.

The problem is that the ssl certification is only valid for myhost.ops.expert.com,
but a user can access the webapp at myhost.ops and if they do they get an invalid certificate.

What I have been able to accomplish is if a user types in [myhost.ops.expert.com:7777...] then it gets rewriten(redirected?) as https://myhost.ops.expert.com:4443/apex/apex and is able to use the valid certificate. I want this to happen if the user types in https://myhost.ops:4443/apex/apex.

the code I am using to rewrite(redirect?) the http address to https address is:
RewriteCond %{SERVER_PORT} !^4443$
RewriteRule ^/apex/(.*)$ https://myhost.ops.expert.com:4443/apex/$1 [L,R]


 9:51 pm on Jun 21, 2010 (gmt 0)

You'll probably want to change the R to R=301 here.


 10:57 pm on Jun 21, 2010 (gmt 0)

I tried just this:

RewriteRule ^/apex/(.*)$ https://myhost.ops.expert.com:4443/apex/$1 [L,R=301]

it still worked to rewrite the http address to https://myhost.ops.expert.cop:4443,
but I can't get it to rewrite https://myhost.ops:4443 to https://myhost.ops.expert.cop:4443.

Is there something that I am missing when trying to rewrite an https address to an https address?


 11:54 pm on Jun 21, 2010 (gmt 0)

That's apparently because the pattern in the left side of your rule contains "/apex/" and the URL you're having trouble redirecting does not contain "/apex/"

The pattern has to match the requested URL-path, or the rule won't be invoked.



 12:37 am on Jun 22, 2010 (gmt 0)

sorry for the confusion, I just didn't write out the complete url:

I can't get it to rewrite https://myhost.ops:4443/apex/apex to https://myhost.ops.expert.com:4443/apex/apex.

Is there something that I am missing when trying to rewrite an https address to an https address?


 3:27 pm on Jun 22, 2010 (gmt 0)

There's nothing particularly tricky about it except that http vs. https must be detected indirectly using the %{HTTPS} or %{SERVER_PORT} variable as already done.

However, the patterns have to match the desired requested URL-paths, and the code has to be located where it will be executed when those URL-paths are requested -- e.g. in the proper VirtualHost, <Directory> container, or .htaccess file (as applicable).

Because your URL-path patterns start with a slash, that implies that this code is located in a server config file and is not contained within any <Diretory> container in that config file.

In addition, beware of other modules which can be invoked before mod_rewrite and pre-empt it. Examples include mod_alias, mod_proxy, mod_dir, mod_negotiation, mod_speling, and possibly, additional third party modules.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved