Can I use "redirect.regex" and "redirect.replacement"? to redirect from mysite.com to my-site.com? - traefik

I'm trying to redirect all traffic from mysite.com to my-site.com. I'd like to do this with Traefik, if possible.
- traefik.frontend.redirect.regex=^https?://(?:www.)?.mysite.([a-z.]+)
- traefik.frontend.redirect.replacement=https://www.my-site.$$1
- traefik.frontend.rule=HostRegexp:www.my-site.{tld:[a-z.]+}
The documentation for redirect.regex and redirect.replacement is a little unclear to me :
Redirects to another URL to this frontend.
Before a match (redirect.regex) is made, do I still need to match the request with rule first - in order to qualify that request to be tested against redirect.regex?
Is what I am trying to achieve doable with regex and replacement?

Related

Apache 2.4 rewriting directory URLs without trailing slash to https://default_site/dir/ instead of preserving domain

This is a relatively recent behavioral change and appears to be related only to requests which include a "Upgrade-Insecure-Requests: 1" request header.
Apache has started rewriting such requests for sites which are HTTP-only to an HTTPS URL using the default site name instead of just adding the / at the end of the requested URL.
Example: URL submitted in browser: http://www.example.com/blah
Intended redirect: 301 to http://www.example.com/blah/
Instead redirects: 301 to https://default.site.configured/blah/
This happens whether it's a named virtual on the same address as the default server or a virtual using a separate address with separate Listen directives.
I understand all the arguments in favor of the idea that everything should always be encrypted and I don't want to get into a debate about that. This site doesn't consider the tradeoffs desirable at this time.
The default site does have SSL and is configured to redirect HTTP->HTTPS, but the www.foo.com site is not configured that way and does not wish to implement SSL at this time.
Is there any way to get Apache 2.4 to disregard that "Upgrade" header and simply rewrite the URL as desired rather than altering the domain name?
After banging on this some more, I finally found the source of my woes.
This happens when you have IP based virtual hosts and did not configure a name for them using the "ServerName" directive.
tl;dr: If you are having this problem, try adding a "ServerName www.example.com" directive within the VirtualHost definition for the site and that should resolve it.
Details:
It does not happen until you encounter a URL that requires a rewrite other than adding a trailing /. (i.e. if you get a request that doesn't contain the "Upgrade-Insecure-Requests: 1" header, it only gets the trailing / added, but if you get one with that header, it also tries to rewrite the protocol to https which triggers the full URL rewrite).
In my case, the default host name had an SSL configuration, so it didn't fall back to HTTP after the rewrite or reject the rewrite as invalid.
YMMV, I did not continue to do an exhaustive test of all permutations once I found the solution.

Apache reverse proxy - rewrite and Substitute returning answer

We have reverse proxy server which use rewrite rule to redirect one address to another.
When redirection is working, we get back an answer from that site (google) as a txt page.
Now, we wish to Substitute few words in that page and direct it to the source server that asked for it.
Our configuration looks like this:
ProxyRequests Off
RewriteEngine on
RewriteRule ^/books\.google\.com(.*) https://books.google.com/$1
Substitute "s/thumbnail_url/test/ni"
We do get the page back from google, but Substitute of words in the page is not working.
Hoping someone can answer it.
Thanks
Found the way to do so, by adding the following lines:
SSLProxyEngine On
RequestHeader set Front-End-Https "On"
Substitute "s/thumbnail_url/test/ni" [P]
Cause [P] will made the all query to work with https (as rewrite rule defined) and two first lines support SSL on apache proxy.
Lavi

Apache redirection dependent on the given input

I have a vpn server which listens to https://sub.domain.com:943 and https://sub.domain.com:943/admin for the admin panel.
I would like to type on my browser (http) sub.domain.com or sub.domain.com/admin and the apache redirections, to redirect me accordingly.
I manage to do the first part my using a simple Redirect / https://sub.domain.com:943
But how can I achieve the second part?
Was so easy at the end, but I couldnt come up with that on that time.
Redirect /admin https://sub.domain.com:943/admin
Redirect / https://sub.domain.com:943

Making two sites appear as single domain

I have two websites set up as follows
www.site1.com - IIS 6.0, Url rewriting with Helicon ISAPI Rewrite 2
www.site2.com - Apache
I've managed to make requests to www.site1.com/site2/ reverse proxy to www.site2.com/, so site2 appears to be part of site1. This was achieved by a RewriteProxy rule in the Helicon httpd.ini
However, to preserve SEO, I also need requests for www.site2.com itself to 301-redirect to www.site1.com/site2 - except of course when the request comes via the above reverse proxy. I believe this is possible via a conditional rewrite rule in the .htaccess file of site2, the condition being that if something unique about a request from site1 is detected, it serves the content, otherwise it 301 redirects.
The condition I'd wanted to use was the www.site1.com IP address, but in our scenario it's likely to change, so I need to use something different to identify such a request.
How else could this be achieved? Is the 301/reverse proxy combination a typical solution to this type of problem?
A slightly odd set-up perhaps but I think doable.
Just off the top of my head, I think that you would need to check the referrer header as part of the rewrite rules in .htaccess. So you would check to see if the referrer is NOT site1 before hitting the rule to rewrite using a 301 header.
If you need further help on setting the rules, let me know and I'll see if I can give you a more specific example - I'm a bit pressed for time just now.

301 + Canonical to link subdomain to root domain for search engines?

I'd like to have the search ranking on my root domain to carry through to my blog subdomain. I'm not sure if this makes sense, but I thought perhaps 301 Redirect from mydomain.com/blog to blog.mydomain.com and having a canonical tag linking back to mydomain.com/blog would help.
For example: A user or search engine goes to mydomain.com/blog/my-post, a 301 redirect takes them to blog.mydomain.com/my-post. On that post is the link tag: rel='canonical' href='mydomain.com/blog/post'
Or, a better example would be someone links to blog.mydomain.com/post and that inbound link goodness gets transferred to mydomain.com. I'm not sure if this works in the other direction: i.e. a link to mydomain.com transferring link goodness to blog.mydomain.com
No, this won't work. The 301 redirect will be followed by search engines to determine the "real" canonical URL.
See the Google FAQ:
Can rel="canonical" be a redirect?
Yes, you can specify a URL that redirects as a canonical URL. Google will then process the redirect as usual and try to index it.
However, RFC 6596 discourages to use a permanent redirect:
To better ensure that applications properly handle the canonical link relation, administrators ought to consider the following guidelines:
[…]
Avoid designating the target (canonical) as:
[…]
The source IRI of a permanent redirect (for HTTP, this refers to 300 and 301 response codes defined in Sections 10.3.1 and 10.3.2 of [RFC2616]
What framework/language/CMS are you working with?
I guess you could dynamically add your canonical element
<!-- html from mydomain.com -->
%define_domain% = ( %define_domain% ) ? 'mydomain.com' : 'blog.mydomain.com'
%extra_goodness% = 'blog/my-post'
<meta name="canonical" content="%define_domain% / %extra_goodness%">