AWS Cloudfront and S3 redirects lose custom domain name - amazon-s3

I have a S3 static website set up using CloudFront for HTTPS. I'm trying to add redirect rules, but I can't get it all to work together.
I have a Cloudfront distribution for www.mysite.com pointing at mysite.s3-website.us-east-2.amazonaws.com. This works great. It serves the default index page and custom error page. However, I now want to move page1 to page2. I tried setting up a redirect rule:
<RoutingRule>
<Condition>
<KeyPrefixEquals>page1</KeyPrefixEquals>
</Condition>
<Redirect>
<ReplaceKeyWith>page2</ReplaceKeyWith>
</Redirect>
</RoutingRule>
The redirect is triggered; however, it redirects away from mysite.com/page1 -> mysite.s3-website.us-east-2.amazonaws.com/page2!
Note: I can't use 301 redirect pages because some of the actual redirects are hash-related.
Any suggestions?

I remember we had the following,
<RoutingRule>
<Condition>
<KeyPrefixEquals>page1</KeyPrefixEquals>
</Condition>
<Redirect>
<HostName>www.mysite.com</HostName>
<ReplaceKeyWith>page2</ReplaceKeyWith>
</Redirect>
</RoutingRule>
Hope it helps.

Related

Trying to set up a "masked URL redirect" (that might not be the correct term)

OK, what I want is, if a person types the URL "test.MyClientsDomain.net" they actually go to "MyCompanyDomain.com" (but the URL bar shows "test.MyClientsDomain.net").
Here is what I have done so far:
We purchased a SSL from goDaddy that allows us to add multiple domains and both domains are on it.
MyCompanyDomain.com is our main site, on a windows server at Rackspace that I have control over, and that works as expected
I have MyClientsDomain.net registered at NameCheap, the DNS is hosted at a 3rd party (plesk server that I have control of)
I have added a CNAME at the 3rd party DNS that points test.MyClientsDomain.net to MyCompanyDomain.com
I have added the "zone" MyClientsDomain.net to the DNS at Rackspace and it points to the webserver's IP
I have added a CNAME at the Rackspace DNS that points test.MyClientsDomain.net to MyCompanyDomain.com
But it's not working as I want, can you tell me what I am doing wrong?
According to your description, when you type test.MyClientsDomain.net in the browser, you want to be redirected to MyCompanyDomain.com, but the URL bar of the browser still displays test.MyClientsDomain.net.
In IIS, to achieve this effect you need to use the URL Rewrite module and configure proxy settings for ARR. If you have not installed the ARR module on IIS, you can refer to this link.
When you have two websites, the domain name of website 1 is bound to MyCompanyDomain.com, and the domain name of website 2 is bound to test.MyClientsDomain.net. If you want to achieve your needs, you need to create a rewrite rule on website 2. The following URL rewrite rule in the web.config can give you a reference:
<rewrite>
<rules>
<rule name="test rule in website2" enabled="true">
<match url=".*" />
<action type="Rewrite" url="http://MyCompanyDomain.com" />
</rule>
</rules>
</rewrite>
Also, you need to select Application Request Routing Cache module at server level and select "Server Proxy Settingsā€¦" on the right tree node
, then check "Enable Proxy" and apply.
At this time, when you type test.MyClientsDomain.net in the browser, you will see that the page loads the content of the MyCompanyDomain.com website, and the URL bar still shows test.MyClientsDomain.net.
You aren't mentionning the use of Name Virtual Hosting on MyCompanyDomain.com and for this reason you're ending serving that old domain matching the same IP.
Name Virtual Hosting allows one to publish multiple applications on the same IP, or to publish a very same application with different domain names (what you call "masking"). However for the latter to be completely transparent, nothing like a 301/302 Redirect from one domain to the another should be configure.
Have a look at how configuring Configuring SSL Host Headers in IIS.

IIS URL Rewrite to redirect all requests for a domain

I have a web server that hosts multiple domains running IIS 8.5. One of those domains is going away and needs to be redirected to the root of another domain on the server. I created the rule below and it works fine as long as there is not path. If there is a path, it appends the path and results in a 404 error. I want to ignore the path and only redirect to www.DomainB.com. I would think path would only be included if I put in http://www.DomainB.com/{R:1} as the redirect url but this doesn't seem to be the case. Where am I going wrong here?
<rule name="Redirect DomainA.com">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^.*DomainA.com$" />
</conditions>
<action type="Redirect" url="http://www.DomainB.com/" redirectType="Found"/>
</rule>
I figured it out. The above is actually good. It was another rule for DomainB, one set to do a redirect from www to non www, which was causing the issue. It did not have DomainB in the condition, but rather a reg expression that matched any www and that rule does have the {R:1} on it, so the path was actually being appended by that rule after the DomainA.com rule processed. After making that rule DomainB specific, the above Redirect DomainA.com rule worked as is

Redirect old urls when changing to prefixless in Piranha Cms

I've unfortunately got a lot of pages SEO-indexed with "/home" as prefix.
Now I need to change to prefixless urls in Piranha, which is easy. But is there a place where I can force redirects from old urls to new urls?
E.g. redirect "www.example.com/home/page" to "www.example.com/page". The problem is that the old page doesn't exist anymore so I can't put a script on that page to do the redirect.
Best regards
Lars, Denmark
I think the best way is to use url rewrite module (need to install it from web platform).
In web.config You can append rewrite rules to system.webServer => rewrite => rules.
I will not test it, but it should be something like this:
<system.webserver>
<rewrite>
<rules>
<rule name="RedirectToPrefixless" stopProcessing="true">
<match url="^home/(.*)" />
<action type="Redirect" url="{R:1}" />
</rule>
</rules>
</rewrite>
</system.webserver>
Tag "match" finds for urls begining with "home/" - part of regex "(.*)" create group which will be used in redirect url
Tag "action" states, that for matching URL it should be redirected to "(.*)" part from "match" part.
So for example:
/home/test1 redirects to /test1
/home/test2?page=22 redirects to /test2?page=23
All redirects are "Permanent" (http status 301) - it can be also changed ;)

IIS SSL Is Not Functioning Properly

I have a new SSL certificate that I'd like to assign to my website. I am managing this on a VPS server so I have full control of the environment. I've successfully followed instructions of GoDaddy to verify and install my SSL certificate. Now the problem is, I can still navigate to my website with HTTP and it becomes an unsafe website...
4 bindings are made in the IIS application
example.com (https, 443, certificate selected)
www.example.com (https, 443, certificate selected)
example.com (http, 80)
www.example.com (http, 80)
I've fiddled with the "SSL Settings" menu of my web application. If I check "Require SSL" with leaving "Accept" selected and if I try to access http://example.com, IIS returns 403 - Forbidden: Access is denied., but https://example.com works fine. If I disable it, both http and https works fine. But isn't it normally supposed to go to https connection automatically? If I remove the http domain bindings from the web app, naturally they end up getting 404 not found.
How am I going to achieve this?
Server uses IIS 10 by the way.
Cheers.
isn't it normally supposed to go to https connection automatically?
No it will not automatically redirect.When you enable RequireSSL,you are just enforcing that the connection should be over SSL.
How am I going to achieve this?
You have to have both http and HTTPS binding and do not set RequireSSL.Now we have to configure automatic redirect using URLRewrite module explicitly.
Install URLREWRITE module
Add a rule as below to have http to HTTPS automatic redirection for any user who comes on http.
As you can see, these rules are stored on inetpub\wwwwroot\web.config file.
`<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>`
The "Require SSL" option will prevent all non-HTTPS requests from hitting the website at all in the first place, so your non-HTTPS bindings are useless in that case.
If you want to add automatic HTTP-to-HTTPS redirections, there are 2 ways of doing it:
Method 1
Remove the HTTP bindings from the website.
Leave the "Require SSL" option enabled on the secured website.
Create a second website, and add the HTTP bindings to that website.
Add an IIS Redirection to the second website. Use the "HTTP Redirect" tool.
Method 2
Disable the "Require SSL" option.
Ensure you have the "URL Rewriting" module installed.
Open the URL Rewriting tool on the website and use the template for HTTP-to-HTTPS Redirection.
But isn't it normally supposed to go to https connection automatically?
No. Adding a certificate to IIS does not mean it automatically starts redirecting. This is usually handled by either the application itself, or a rewrite method like IIS's URL Rewrite module.
If I check "Require SSL" with leaving "Accept" selected and if I try to access http://example.com
This is for client certificates. You likely don't want to enabled.
If I remove the http domain bindings from the web app, naturally they end up getting 404 not found.
You want both bindings enabled, and have something redirect from HTTP to HTTPS as described above.

Subfolder redirect issue with static website hosting using S3, CloudFront and Origin Path

I'm having some difficulties setting up static website hosting using Amazon S3 and Cloudfront.
We have many websites that we would like to serve as static websites using Amazon S3 + Cloudfront and we would prefer to host them all in a single S3 bucket.
Initial setup is pretty straight forward but we are having issues with sub-folder redirects if omitting trailing slash in the URL.
example, setting up a single website from the bucket:
bucket contents for website1:
s3://bucket-name/websites/website1/index.html
s3://bucket-name/websites/website1/about/index.html
I have enabled static website hosting for this bucket with default document set to 'index.html'
I have created a Cloudfront web distribution to serve this single website, default root object is set to 'index.html'.
The distribution has a custom origin pointing to the static website url 'bucket-name.s3-website-us-east-1.amazonaws.com' with Origin Path set to '/websites/website1'
When navigating to the distribution url 'http://example.cloudfront.net' it correctly serves the 'index.html' document from 's3://bucket-name/websites/website1/index.html'
When navigating to 'http://example.cloudfront.net/about/' it also correctly serves the 'index.html' document from 's3://bucket-name/websites/website1/about/index.html'
But, if I omit the trailing slash like 'http://example.cloudfront.net/about' S3 redirects me to 'http://example.cloudfront.net/websites/website1/about/', since I have Origin Path set to '/websites/website1' Cloudfront will request index.html from 's3://bucket-name/websites/website1/about/websites/website1/about/index.html' which does not exist.
Am I missing something here? Is this an impossible setup using only Cloudfront and S3?
I ended up solving it by using routing rules for the S3 bucket
https://docs.aws.amazon.com/AmazonS3/latest/dev/how-to-page-redirect.html
the problem is the redirect caused by omitting a trailing slash results in the Orgigin Path being appended to the full S3 bucket path ("example.cloudfront.net/about" redirects to "example.cloudfront.net/websites/website1/websites/website1/about/" that fails because the path is invalid)
The below routing rule solves this by triggering on the faulty path pattern prefix and redirecting back to the Cloudfront distribution with the prefix stripped from the request, i.e ("example.cloudfront.net/about" redirects to "example.cloudfront.net/websites/website1/websites/website1/about/" that redirects to "example.cloudfront.net/about/")
The downside is that you need to remember to modify the routing rules when adding new distributions
<RoutingRules>
<RoutingRule>
<Condition>
<KeyPrefixEquals>websites/website1/websites/website1/</KeyPrefixEquals>
</Condition>
<Redirect>
<HostName>example.cloudfront.net</HostName>
<ReplaceKeyPrefixWith></ReplaceKeyPrefixWith>
</Redirect>
</RoutingRule>
</RoutingRules>