RewriteRule using HTTP_HOST and another port

I need to use the HTTP_HOST header in the RewriteRule, but change the port I cannot use SERVER_NAME since it will be different from the host header (which I need)

Is there a way to trim: port off from the HTTP_HOST variable for mod_rewrite?

+6
source share
1 answer

Yes, you can trim the port from the host header. Just compare with %{HTTP_HOST} and use %1 backlink. For instance:

 RewriteCond %{HTTP_HOST} ^([^:]+)(:[0-9]+)?$ RewriteRule ^ http://%1:12345/ [R,L] 

Just keep in mind that relaying %1 can only be used in the first parameter of the RewriteCond , and not in a match:

 RewriteCond %{HTTP_HOST} ^([^:]+)(:[0-9]+)?$ RewriteCond %1 ^the.hostname.com$ [NC] 

ok

 RewriteCond %{HTTP_HOST} ^([^:]+)(:[0-9]+)?$ RewriteCond %{REQUEST_URI} ^%1 

NOT OK

+9
source

Source: https://habr.com/ru/post/920852/


All Articles