Forum Moderators: phranque
Problem is that I don't know how to make that virtual subdomain and variable trimming together.
I have done virtualsubdomain like this:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} testbed.domain.com$
RewriteCond %{REQUEST_URI} !2009/
RewriteRule ^(.*)$ 2009/$1
And I have previously used variable trimming like this:
RewriteRule ^([^/\.]+)/?$ /index.php?p1=$1&%{QUERY_STRING} [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?p1=$1&p2=$2&%{QUERY_STRING} [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?p1=$1&p2=$2&p3=$3&%{QUERY_STRING} [L]
Can someone help me to put those RewriteRules together so that testbed.domain.com/page/subpage would get it's content from www.domain.com/?p1=page&p2=subpage.
Thanks in advance!
RewriteCond %{HTTP_HOST} \.?testbed\.example\.com
RewriteCond %{REQUEST_URI} !2009/
RewriteRule ^([^/.]+)/?$ /2009/index.php?p1=$1&%{QUERY_STRING} [L]
RewriteRule ^([^/.]+)/([^/.]+)/?$ /2009/index.php?p1=$1&p2=$2&%{QUERY_STRING} [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /2009/index.php?p1=$1&p2=$2&p3=$3&%{QUERY_STRING} [L]
#
RewriteCond %{HTTP_HOST} \.?testbed\.example\.com
RewriteCond %{REQUEST_URI} !2009/
RewriteRule ^(.*)$ /2009/$1 [L]
For example, if you prefer the non-trailing-slash form (as I would), then a request for "www.testbed.example.com/page/subpage/ should be 301-redirected to testbed.example.com/page/subpage
A general rule of thumb when rewriting and redirecting with mod_rewrite is to put all external redirects first, in order from most-specific pattern (fewest URLs affected) to least-specific pattern (most or all URLs affected), followed by all internal rewrites, again in order from most-specific to least-specific. When the patterns are mutually-exclusive --as those in your three index.php rules are here-- then the order among similar rules doing internal rewriting is not critical, but some care is still needed to prevent unexpected operation caused by interaction with the other rules.
For example, with the rules in your original order, you would have likely ended up with requests for /testbed.example.com/page/subpage being rewritten to /2009/index.php/p1=2009&p2=page&p3=subpage instead of to /2009/index.php/p1=page&p2=subpage
If you feel that you must end-anchor your hostname patterns, then use a pattern of \.?testbed\.example\.com\.?([0-9]+)?$ instead of \.?testbed\.example\.com$ so that your rule won't be defeated if an FQDN or appended port number is present. For example, you could get a perfectly-valid request for "example.com.:80" which would not be affected by your original rule.
Jim