Forum Moderators: phranque
I'm new to the forum and relatively new to .htaccess, reg exp, apache, etc. I am having trouble with back references in the form %N in the .htaccess file.
I would like to do rewrite in which I maintain the request protocol. I found an elegant solution in a previous post and *attempted* to modify it to meet my needs.
My rewritten version reads:
RewriteCond %{SERVER_PORT}s ^(443(s)¦[0-9]+s)$
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com/?
RewriteRule ^(.*)$ http%2://example2.com [R=301,L]
I did use a solid pipe.
I think that I understand what is happening here. The first condition will always be true. However, if the port is 443, you've captured the "s" in a back reference - %2 - that can be used in the rewrite rule. If the port is anything else, the back reference will not exist or will be empty. The net effect is that if the original url was [,...] the rewritten url will be [....] If the original url was [,...] the rewritten url will be http://
Is that correct?
I did several tests and it appears that there is no substitution for the %2. In all cases, I get http://
I don't think that it is a port detection problem because the following works as expected.
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com/?
RewriteRule ^(.*)$ [example2.com...] [R=301,L]
Is this a pilot error? Is there a setting on the server that disallows this type of backreference?
Back references in the form $N work fine.
I also have a question regarding the pattern ^(.*)$. I've seen it written with and without the anchor points. If you are matching any set of characters, are those really needed? Do they help performance?
Thanks in advance for your help. I've learned a lot from this forum.
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com
RewriteCond %{SERVER_PORT}s ^(443(s)¦[0-9]+s)$
RewriteRule (.*) http%2://example2.com/$1 [R=301,L] I haven't tested the code.
In your original code, you also didn't re-use what you captured in $1 by using (.*) pattern.
If you don't need to carry over the path and page name, the Rule can be simplified:
RewriteRule . http%2://example2.com/ [R=301,L]