Welcome to WebmasterWorld Guest from 54.198.2.110

Forum Moderators: Ocean10000 & incrediBILL & phranque

Rewrite HTTP HOST to HTTP HOST/HTTP HOST

mod_rewrite

     
9:43 pm on Oct 6, 2017 (gmt 0)

New User

joined:Oct 6, 2017
posts: 3
votes: 0


I am trying to create a Rewrite rule to do the following.

http://www.example.com rewrite to http://www.example.com/example.com <-- Notice the missing www for the url link portion.
http://example.com rewrite to http://example.com/example.com

I do need this to be for ANY host though. So for example...
%{HOST_NAME}/%HOST_NAME

Preferably without the client seeing the extra /example.com

Basically we setup a lot of new sites with various platforms so I am using haproxy with the help of an ACL to redirect all hosts listed in a file to a specific backend server where it's only job is to serve up static index.html files with a custom "Coming Soon" message for each site.

Then after the site has been completed with content and the customer is ready to go live we simply remove their hostname from the ACL and then it proceeds to the correct web server cluster for that customer.

I hope that made some sense. I am trying to prevent having to create a coming soon folder on each of our web clusters and have haproxy simply redirect to a specific apache server until the site is ready to go live.

With this solution our designers can simply create a folder under say a Directory Root folder of coming_soon place a simple folder called example.com under the Document Root and place the index.html and supporting images etc in that folder and it will automatically just work.
1:24 am on Oct 7, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14256
votes: 551


Preferably without the client seeing
If you rewrite, the client won't see anything. That's the definition of a rewrite as distinct from a redirect.

The part that isn't clear is whether you also want to rewrite
www.example2.com
www.example3.com
www.example.org
and so on, all to serve content from a files living at
example.com

That is: client requests
www.example6.org
receives content from
www.example.com/example6.org
Client requests
www.example97.net
receives content from
www.example.com/example97.net
and so on.

Will the DNS for all these different domains all point to the same physical location, or will they all “exist” individually?

and place the index.html and supporting images etc in that folder and it will automatically just work
Now you need to be careful, because if the browser "thinks" it is at example.org/ (which is to say example.org/index.html) then it will request supporting files on that basis. So the visitor will request example.org/images/prettypicture.jpg when it really needs example.com/example.org/images/prettypicture.jpg. In other words everything would need to be rewritten, not just the front page.

Leaving out the www is probably best done as a RewriteCond involving
^(?:www\.)?([\w-]+\.com)
and then you plug that captured %1 into the target. That's assuming there will never be other subdomains involved, just with/without www.
4:05 am on Oct 7, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11078
votes: 106


welcome to WebmasterWorld, tekati! [webmasterworld.com]

if i understand your description properly, you probably want to use mod_proxy, either with the ProxyPass(Reverse) Directives:
https://httpd.apache.org/docs/current/mod/mod_proxy.html#proxypass

and/or the equivalent RewriteRule with the [P] flag specified.
12:04 pm on Oct 7, 2017 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 306
votes: 21


you probably want to use mod_proxy


But there doesn't seem to be any change in host here? (Not at the Apache level at least.)

From the OPs example it looks to me like its...


www.<whatever.com>/<something>


Internally rewrite to


www.<whatever.com>/<whatever.com>/<something>


Where the "www" is optional. The host passes through unchanged (keeping the www, if present), but the "www" subdomain (if present) is stripped from the subdirectory. (?)
1:48 pm on Oct 7, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11078
votes: 106


But there doesn't seem to be any change in host here?


i'm going by this statement:
Basically we setup a lot of new sites with various platforms so I am using haproxy with the help of an ACL to redirect all hosts listed in a file to a specific backend server...


hence:
if i understand your description properly
6:55 pm on Oct 7, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14256
votes: 551


The part we're waiting to get cleared up is whether a request for
http://www.example.org/blahblah
gets rewritten to
http://www.example.org/example.org/blahblah
or to
http://www.example.com/example.org/blahblah
2:07 am on Oct 8, 2017 (gmt 0)

New User

joined:Oct 6, 2017
posts: 3
votes: 0


For some reason I can not post a reply to individual posts so I will have to try to answer them all here.

Okay so we do a lot of school, business and even wordpress sites. So domains are all over the place. So let me try to explain myself better.

So say school x.k12.in.us wants us to bring their site to our platform. During the development process they request a coming soon page.

The way we are setup now with haproxy is we have approx 6 different platforms that can be used. Each of these platforms require their own vhost address so all in all we have approx 6 different IP addresses 1 for each platform. So instead of giving them an IP address to point their domain at for a coming soon page and then when they go live give them a different IP address depending on their platform it would be best to simply have haproxy via what they call ACL's and a backend server direct all traffic for the domain to a specific apache server that simply serves up html and nothing else. So what we can do is give them the IP address of the platform they will be located on and haproxy would simply just route they traffic to a specific apache server regardless of the IP they use or the platform they will end up on. We have ColdFusion, PHP, Wordpress, etc platforms.

So in order to not have to create a new vhosts file everytime we need a coming soon page what I am looking for is simply this.

We put
x.k12.in.us
www.x.k12.in.us
businessx.net
www.businessx.net
personalx.com
www.personalx.com

In what I will refer to as a redirect file on the haproxy server. Any domains in that "redirect" file will simply be forwarded to an specific apache server dedicated to serving up coming soon pages.

So if the domain is entered into the URL of a browser this is what I would like to have happen.

x.k12.in.us gets put into the browser and actually http://x.k12.in.us/ANYTHING and what I want the specific apache server to do is instead of giving out the Document Root index.html I want it to give out http://x.k12.in.us/x.k12.in.us and if they enter in http://www.x.k12.in.us instead of having to create two folders with the coming soon files in it have it go to http://www.x.k12.in.us/x.k12.in.us so it would simply remove the www. so that we could direct them to the same files.

Or if it makes more sense it would "redirect" to /x.k12.in.us as the domain portion is now irrelevant.

Now what I want to avoid doing is having to specifically place in the .htaccess or even the 000-default.conf file entries for every domain name. This would defeat the purpose altogether if that were the case I would simply create a .vhosts for each one.

So something using the http://%{HTTP_HOST}/%{HTTP_HOST} or $1 if we need to do a condition to strip the www. off.

So lets say that the Document Root for that same apache server is /var/www/html then we would simply create a folder under it /var/www/html/x.k12.in.us and place an index.html and supporting files there and it would automagically server up the proper files.

I hope that made more sense.

So Lucy24 in your first reply you are correct in that it would be a Rewrite and not a Redirect thank you for pointing that out.

phranque that makes a lot of sense and I had not thought about the mod_proxy module.

whitespace you are correct but it needs to work for any domain whether it be .com, .org, .net, .us etc...

Lucy24 again you are correct and I understand what you are asking I think. So lets use an example again. I do not care what the url is really as some of these customers may be coming from a different platform so they may have links in different search engines pointing to specific content so really http://www.x.k12.in.us/Posts/24/?user=anonymous&blah=blah would need to then point to http://www.x.k12.in.us/x.k12.in.us/ and that is it.

This is all to make it semi idiot proof for our internal people. Simply add a couple entries to a text file and everything just works. When the site is ready to go live remove those same entries and walla they are live.

[edited by: phranque at 3:34 am (utc) on Oct 8, 2017]
[edit reason] unlinked urls [/edit]

3:49 am on Oct 8, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11078
votes: 106


Any domains in that "redirect" file will simply be forwarded to an specific apache server dedicated to serving up coming soon pages.

So lets say that the Document Root for that same apache server is /var/www/html then we would simply create a folder under it /var/www/html/x.k12.in.us and place an index.html and supporting files there and it would automagically server up the proper files.

as i understand your description the /var/www/html/ subdirectories (/x.k12.in.us etc) are in the document root directory of one central "coming soon" server and for this you don't need mod_proxy, the internal rewrite will suffice.
use the regular expression suggested by lucy24 in your RewriteCond for the internal rewrite ruleset.
^(?:www\.)?([\w-]+\.com)

it sounds like you have the dns for all these domains pointing to the haproxy server.

i would be concerned that haproxy sends/modifies the proper headers regarding hostname and that you have the proper ssl configuration if/when you migrate to https.

you should also insure these urls are noindexed until the actual content is publish.
3:45 pm on Oct 9, 2017 (gmt 0)

New User

joined:Oct 6, 2017
posts: 3
votes: 0


Still not sure what the RewriteCond would solve. It would pull out the $1 (www.) and $2 (x.com) for me but the $2 would only work for .com and not any of the others. So it would not catch (x.k12.ca.us) for instance.

^(?:www\.)?(.*)/$


My RegEx is a little rusty but something like that or a few tweaks should catch the proper $1 and $2 and I do not need the rest after the /.

HAProxy handles the SSL's extremely well. We are now using Let's Encrypt and it is all automated on our side.

noindexed is a great catch and one I will implement thank you.
5:39 pm on Oct 9, 2017 (gmt 0)

New User

joined:Oct 6, 2017
posts: 3
votes: 0


So here is my current solution.

In the Document Root folder .htaccess

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*)$ /%1/$1 [L]

RewriteCond %{HTTP_HOST} ^(.+) [NC]
RewriteRule ^(.*)$ /%1/$1 [L]


In each of the subfolders .htaccess
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /


This does EXACTLY what I want but I am sure it could be streamlined quite a bit. For instance I do not like having to add the .htaccess to each subfolder off of the Document Root.

Suggestions would be greatly appreciated.
6:43 am on Oct 11, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11078
votes: 106


i think you could do something like this in the .htaccess file of the document root directory:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?([\w-]+) [NC]
RewriteRule . /%1/index.html [L]


and then get rid of the .htaccess files in the subdirectories.

this way when a request is made for any virtual host on your server, for example:
http://www.x.k12.in.us/any/arbitrary/path/doc.html
or:
http://x.k12.in.us/any/arbitrary/path/doc.html
or even with port specified:
http://x.k12.in.us:80/

the url in the browser won't change but the visitor will see the content in:
/x.k12.in.us/index.html

i should remind you this document should contain or serve the appropriate noindex directive until you are serving live content.
10:27 pm on Oct 11, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14256
votes: 551


It would pull out the $1 (www.) and $2 (x.com) for me but the $2 would only work for .com and not any of the others.

What you call $2 is in fact $1, or rather %1 since it is from a RewriteCond. That's the point of the non-capture (?:wwww\.)? -- you just ignore the www. if it happens to be present.

If you have things ending in other-than-dot-com just leave the rest of it open:
^(?:www\.)?(.+)
or, if you want to exclude port numbers,
^(?:www\.)?([^:]+)
If negatives make you uneasy, say
([\w.-]+)
instead. That's: word character, literal period, hyphen.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members