Htaccess: Condition on HTTP_USER_AGENT then rewrite with dynamic subdomain

I’m creating a static webpage solution for my angularJS project where I check for the HTTP_USER_AGENT in my .htaccess file and then redirect any crawlers to a static php page so I can add all the necessary meta data.

The problem is that I’m using a wildcard subdomain, so any of my clients can have their own subdomain and I can’t figure out how to redirect via RewriteRule to the right url.

My RewriteCondlooks like this:
RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Facebot|Twitterbot|Pinterest|Google.*snippet)

and my basic non-working RewriteRule looks like this:
RewriteRule ^stuff/(.*)$ http://example.com/static.php?token=$1 [NC,L]

But I want it to go to static.php on that subdomain the request is on, so if I request http://subdomain.example.com/stuff?token=my-token is it possible to make the rewriterule go to http://subdomain.example.com/static.php?token=$1 instead of just the static mydomain.com above? (there will NEVER be a request on http://example.com/stuff - all requests to stuff will be via a subdomain)

I’m not that experienced in .htaccess and get’s a bit confused from all the answer that here listed at SO and Google finds.

Edited
I found the solution by piecing some code together and getting my head around it.
My three lines of .htaccess codes that works looks like this:

RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Facebot|Twitterbot|Pinterest|Google.*snippet)
RewriteCond %{HTTP_HOST} ^(.+?)\.example\.com$
RewriteRule ^s/(.*)$ http://%1.sociuu.com/static.php?token=$1 [NC,L]

Why not simply replace http://example.com with %{HTTP_HOST} ?

Naw, you got the right track with your second RewriteCond but, unless you’re changing the domain, you’re working too hard!

Regards,

DK

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.