-1

this is my first question on ServerFault and I think I couldn't find a similar one.

To the point. I would like to achieve the following using my .htaccess.

subdomaina.example.com/stats/ -> subdomaina.example.com/subdomaina/stats/
subdomainb.example.com/stats/ -> subdomainb.example.com/subdomainb/stats/
subdomainc.example.com/stats/ -> subdomainc.example.com/subdomainc/stats/

Subdomaina,b,c and test.com need to be dynamic and not static.

I know that I should be using the mod-rewrite but I am a bit confused.

I quite unexperienced with apache and .htaccess, so any pointers, clues, and explanation is greatly appreciated.

Andrew Schulman
  • 8,561
  • 21
  • 31
  • 47
Jimmy Kane
  • 101
  • 1
  • 5

1 Answers1

4

To solve problems like this, you should read and become familiar with the mod_rewrite documentation, especially the mod_rewrite reference and regular expressions.

This example is easy enough:

RewriteEngine on
RewriteCond %{HTTP_HOST} (.*)\.example\.com$
RewriteRule ^/stats/(.*) /%1/stats/$1 [R]
Andrew Schulman
  • 8,561
  • 21
  • 31
  • 47
  • Yes I am reading atm. But I could not get this up 2 hours now. I am following a similar approach and trying to learn as fast as I can. Will keep this updated. Thank you very much – Jimmy Kane Mar 06 '14 at 10:47
  • My current problem is how to match the path. eg as you mentioned `(.*)\.example\.com$` with `$` at the end will match all paths (example.com/test and example.com/stats). But I would only like to match stats and this is the part where I am stuck. – Jimmy Kane Mar 06 '14 at 10:50
  • 1
    The RewriteCond only matches the host name, but the RewriteRule includes a second condition that matches only the `/stats` path. – Andrew Schulman Mar 06 '14 at 10:54
  • Great. Yes it's more clear now. Thanks for your effort. – Jimmy Kane Mar 06 '14 at 10:56
  • Good, glad to help. – Andrew Schulman Mar 06 '14 at 10:56
  • I got it. I think though that on the rule the first slash should be ommited because it matches on the condidion. I think like so: `RewriteRule ^stats/(.*) /%1/stats/$1 [R]` – Jimmy Kane Mar 06 '14 at 11:53