Mobile app version of vmapp.org
Login or Join
Shelley277

: Mod_rewrite: rewriting http://$SOMETHING.app.example.com/ to http://www.example.com/app/$SOMETHING I'm trying to make http://$SOMETHING.app.example.com/ transparently rewrite to http://www.example.com/app/$SOMETHING.

@Shelley277

Posted in: #Cname #Htaccess #ModRewrite #Subdomain

I'm trying to make $SOMETHING.app.example.com/ transparently rewrite to www.example.com/app/$SOMETHING.
The config below is giving me a 404 and I'm not sure why.

ServerName example.com ServerAlias *.app.example.com

RewriteEngine On
RewriteCond %{HTTP_HOST} (.*).app.example.com
RewriteRule ^/(.*) /app/%1/


I'm missing something obvious, what is it?

Edit: let me make this a little clearer.

If I browse to www.example.com/app/abc, I get a page. If I browse to abc.app.example.com/ I expect to see the same page, but I'm getting a 404 instead.

Edit2: The DNS record for *.app.example.com is already in place and I can see my request in the access log, with the 404 status.

Edit3: After enabling the mod_rewrite log file, it appears that the post-rewrite path is "$DOCROOT/app/abc" which fails because it doesn't exist. This is a Python/WSGI application, I think that has something to do with my problem. For the moment it looks like we'll just use a regular redirect instead.

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Shelley277

3 Comments

Sorted by latest first Latest Oldest Best

 

@Holmes151

We ended up using an external redirect instead.

RewriteCond %{HTTP_HOST} (.*).app.example.com
RewriteRule ^/(.*) www.example.com/app/%1/ [R]

10% popularity Vote Up Vote Down


 

@Correia994

Perhaps something like:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com
RewriteCond %{HTTP_HOST} !^example.com
RewriteCond %{HTTP_HOST} ^(.)+.example.com
RewriteCond %{REQUEST_URI} !^/app/
RewriteRule ^(.*)$ app// [L]


Which ensures you're not currently accessing example.com or example.com directly; then takes your prefix, checks that you're not currently accessing at the /app directly (or it will loop forever), and then rewrites. This should be transparent..

Elaborated from corz.org/serv/tricks/htaccess2.php

10% popularity Vote Up Vote Down


 

@Rambettina238

You need to set a DNS-record for *.app.example.com. to redirect to the IP of the server. I think in your case that will be the same as the one app.example.com.is pointing to.

Edit:
I think one of these should do the trick (normally both):

RewriteCond %{HTTP_HOST} !^app.example.com
RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteRule ^(.*) /app/%1/ [L]

RewriteCond %{HTTP_HOST} ^([^.]+).app.example.com$
RewriteRule ^/(.*)$ www.example.com/app/%1/ [L,R]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme