I am hosting my AngularJS site in Azure, but running into issues when I only want to perform a URL rewrite to the www. version. I am using HTML5 mode for routing.
If I have the following web.config and refresh the page I get an error stating "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear/>
<rule name="WWW Rewrite" enabled="true">
<match url="(.*)"/>
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\."/>
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
If I leave my web.config as follows, I have no issues with refreshing, but I can't redirect to the www. version of my site:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
How can I have both scenarios working together?
Try adding
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
to your conditions block.
Related
I am trying to get a React app on IIS to redirect to https for everything. It is not working at the moment. I can access the application and assets through http and https, but the former won't redirect to the latter.
This is what I started with:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Static Assets" stopProcessing="true">
<match url="([\S]+[.](html|htm|svg|json|js|css|png|gif|jpg|jpeg|map))" />
<action type="Rewrite" url="/{R:1}" />
</rule>
<rule name="ReactRouter Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Here is what I have so far:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Static Assets" stopProcessing="true">
<match url="^(http|https):\/\/([\S]+[.](html|htm|svg|json|js|css|png|gif|jpg|jpeg))" />
<action type="Rewrite" url="https://{R2}" logRewrittenUrl="true" />
</rule>
<rule name="ReactRouter Routes" stopProcessing="true">
<match url="^(http|https):\/\/(.*)\/(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="https://{R2}/index.html" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I leave this here and I hope it helps someone. It took me hours to find a solution. With the web.config bellow, I can rewrite to index.html and redirect to HTTPS. This works for deeper urls too.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTPS Redirect" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="ReactRouter Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
According to your description, I found you use rewrite instead of the redirect. Since the rewrtie will not redirect from http to https, you feel your rule is not working.
I suggest you could try to use below rule.
<rule name="ReactRouter Routes" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" url="https://www.sample1.com/index.html" logRewrittenUrl="true" />
</rule>
<rule name="Force https" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="true" redirectType="Permanent" />
</rule>
I am using like this my Web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="X-Prerender-Token" value="MY TOKEN" />
</customHeaders>
</httpProtocol>
<rewrite>
<rules>
<rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="https://www.homes247.in/" redirectType="Permanent" />
</rule>
<rule name="Enforce canonical hostname" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" matchType="Pattern" pattern="^www\.homes247\.in$" ignoreCase="true" negate="true" />
</conditions>
<action type="Redirect" url="https://www.homes247.in/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Prerender" stopProcessing="true">
<match url="^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent))(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_USER_AGENT}" pattern="baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator" />
<add input="{QUERY_STRING}" pattern="(.*)_escaped_fragment_(.*)" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="https://service.prerender.io/https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" />
</rule>
<rule name="AngularJS Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(cms)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(crm)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
</staticContent>
</system.webServer>
</configuration>
I am trying to SEO friendly and social Media sharing purpose.
For SEO Purpose, I want rendered data in page view source also. I am struggling in this part for page view source.
FOR EG:
<div><h1>{{Title}}</h1></div>
I want the title content also in page view source. How to render the page in view source in angularjs.
Instead of:
<httpProtocol>
<customHeaders>
<add name="X-Prerender-Token" value="MY TOKEN" />
</customHeaders>
</httpProtocol>
Can you try this inside the <rule>:
<rule name="Prerender" stopProcessing="true">
<serverVariables>
<set name="HTTP_X_PRERENDER_TOKEN" value="MY TOKEN" />
</serverVariables>
...
You might need to add HTTP_X_PRERENDER_TOKEN as an allowed server variable inside your IIS server to make sure that gets sent through. I know you can set the Allowed Server Variables in the IIS user interface.
Copied my CakePHP v2.2 app from a linux server to Azure App Service for dev/testing. I get that .htaccess won't work, so I've added a web.config in each folder \ & \app\ & \app\webroot\. The root of the site works, the home page loads, reads data form SQL, the CSS and JS loads. CSS files are hosted in \app\webroot\css, but accessed via example.com\css\file.css, so I assume some sort of rewrite is working.
The issue is when going to a page, i.e. example.com/about, I get the error The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
I've seen most if not all posts on here about hosting this on IIS, and again I think my web.configs are there. What am I missing?
web.config in \
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="rule1A" stopProcessing="true">
<match url="^$" />
<action type="Rewrite" url="app/webroot/" />
</rule>
<rule name="rule2A" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<action type="Rewrite" url="app/webroot/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
web.config in \app\
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
<system.webServer>
<rewrite>
<rules>
<rule name="rule 1A" stopProcessing="true">
<match url="^$" />
<action type="Rewrite" url="webroot/" />
</rule>
<rule name="rule 2A" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="webroot/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
web.config in \app\webroot\
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="CakePHP" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Fixed it! Found this article again, and updated just the web.config at \ and removed the web.configs from the other two locations. Works like a charm!
https://book.cakephp.org/2.0/en/installation/url-rewriting.html#url-rewrites-on-iis7-windows-hosts
web.config in \
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite requests to test.php"
stopProcessing="true">
<match url="^test.php(.*)$" ignoreCase="false" />
<action type="Rewrite" url="app/webroot/test.php{R:1}" />
</rule>
<rule name="Exclude direct access to app/webroot/*"
stopProcessing="true">
<match url="^app/webroot/(.*)$" ignoreCase="false" />
<action type="None" />
</rule>
<rule name="Rewrite routed access to assets(img, css, files, js, favicon)"
stopProcessing="true">
<match url="^(img|css|files|js|favicon.ico)(.*)$" />
<action type="Rewrite" url="app/webroot/{R:1}{R:2}"
appendQueryString="false" />
</rule>
<rule name="Rewrite requested file/folder to index.php"
stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<action type="Rewrite" url="index.php"
appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
In my web.config I have this rule set up for AngularJS (HTML5Mode)
<rewrite>
<rules>
<rule name="AngularJS Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/token" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
Then I have a load of 301 redirects set up like this:
<location path="about.html">
<system.webServer>
<httpRedirect enabled="true" destination="/about" httpResponseStatus="Permanent" />
</system.webServer>
</location>
obviously the redirects are not being reached. How can I get my rewriter to only apply to anything that is not being redirected?
I hope that makes sense.
Update
I removed the rewrite rule just to test if the redirects work without it. They do, but my application is still working.
I was sure I needed the rewrite rule for HTML5mode. Is that not the case?
Update 2
It seems the rewriter is for url parameters.
What I didn't know is that you can add redirects to the rewrite rules. If you add them first and set the stopProcessing flag to true, it will redirect and not "hit" the rewrite rule.
Here is an example:
<rules>
<rule name="301" stopProcessing="true">
<match url="volleyball" />
<action type="Redirect" url="/" redirectType="Permanent" />
</rule>
<rule name="AngularJS Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/token" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
I can’t get the css, js and img loaded.
I’ve been using the web.config out of this article:
http://book.cakephp.org/view/1533/A-Note-on-mod_rewrite Please any suggestions…
Insert this rule
<rule name="Imported Rule 0" stopProcessing="true">
<match url="^(img|css|files|js)(.*)$" />
<action type="Rewrite" url="/app/webroot/{R:1}{R:2}" appendQueryString="false" />
</rule>
together with the site rules for cakephp.org:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 0" stopProcessing="true">
<match url="^(img|css|files|js)(.*)$" />
<action type="Rewrite" url="/app/webroot/{R:1}{R:2}" appendQueryString="false" />
</rule>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
</rule>
<rule name="Imported Rule 3" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="/app/webroot/" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<action type="Rewrite" url="/app/webroot/{R:1}" />
</rule>
<rule name="Imported Rule 1-1" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="/" />
</rule>
<rule name="Imported Rule 2-1" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<action type="Rewrite" url="/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I had the same issue and after a few hours of research this is how i've fixed my problem:
I've set the permissions to Full Control for Everyone on the CakePHP folder. After that i no longer needed any .htaccess files or web.config