homepage Welcome to WebmasterWorld Guest from 54.211.113.223
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Rewrite host and path in URL
Rewrite host and path in URL
Jstanfield



 
Msg#: 4379627 posted 4:37 pm on Oct 26, 2011 (gmt 0)

I have two virtual hosts set up and working:
api.example.com (let's call this Virtual Host A)
*.example.com (let's call this Virtual Host B)

I would like to create a rewrite rule so that...

xyz.example.com/api/something/to/do

...which Apache would interpret as a request for Virtual Host B...

...rewrites to...

api.example.com/something/to/do

...which Apache would interpret as a request for Virtual Host A.

I basically want to do something like
- if the host is *.example.com
- and the path begins with /api
- change the host to api.example.com
- remove /api from the path

I tried using what's below, which I am ashamed to admit is nothing more than cargo-culting from various sources -- indeed, I'm new to URL-rewriting.


RewriteCond %{HTTP_HOST} *.example.com
RewriteCond %{REQUEST_URI} ^/api
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^api/(.*)$ http://api.example.com/$1


Thank you.

 

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4379627 posted 9:05 pm on Oct 26, 2011 (gmt 0)

Ouch. If that isn't getting you a 500 error, your server is nicer than mine. Does your first * mean that you have wild-card subdomains, so there could be anything in that beginning slot? In general, the safest approach is to say

RewriteCond %{HTTP_HOST} !(^api\.example\.com$)?

meaning "if the host is anything other than exactly api.example.com or exactly nothing". That should cover Conditions 1 and 3, and also any other port that an evil competitor might happen to invent.

You don't need the second Condition, because the Rule says the same thing.

Rewrite Rules should always end in [L]. And since you're going to a different (sub)domain, your Rewrite has changed into a Redirect. By default that means 302, which hardly anyone ever wants, so you also need the [R=301] flag.

Now, what if someone asks for

api.example.com/api/{blahblah}

?

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4379627 posted 10:44 pm on Oct 26, 2011 (gmt 0)

One question.

Should the user see the URL in the browser address bar change after they make the initial request?

Jstanfield



 
Msg#: 4379627 posted 4:00 pm on Oct 27, 2011 (gmt 0)

For sure, what I pasted didn't work... I don't remember if it did nothing at all or caused Apache to complain about bad configuration.

Yes, we have wildcard subdomains.

abc.example.com is ABC Company's branded version, intended for use with a web browser.
xyz.example.com is XYZ Company's branded version, intended for use with a web browser.
and so forth

api.example.com is what the mobile apps are hitting to retrieve data. Also, scripts written by IT staff occasionally hit api.example.com to retrieve CSV/XML/etc.

I ended up getting this to work using a reverse proxy. I would've preferred URL-rewriting because in theory it would use less resources, but this works. Interesting: the removal of /api is done without having to rewrite the URL. Just two lines added to example.com's configuration.


#example.com's API
#needs to be defined before *.example.com or the wildcard will override it
<VirtualHost *:80>
ServerName api.example.com
DocumentRoot /var/www/apps/example_api
</VirtualHost>

#example.com
<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
DocumentRoot /var/www/apps/example

#sends abc.example.com/api/something
#to api.example.com/something
ProxyPass /api http://api.example.com/
ProxyPassReverse /api http://api.example.com/
</VirtualHost>

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