In a DotNetNuke site how do I bypass a 404 error? - http-status-code-404

I have a site -- example.com. It has static pages -- example.com/signup, example.com/faq, etc.
However, I want to be able to have "example.com/some-search-term". Of course, there is no page "some-search-term".
I tried installing a custom module on the 404 error page that would take the query string and display some info based on that search criteria. However, I still get a 404 error "HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier).
GET - http://example.com/some-search-term." and the page is blank.
I can debug my module when the 404 page is triggered and see that everything is working fine but the final results are not displayed.
Any ideas?

For something like this I would suggest using IIS rewrite rules or rewritemaps. You can do this via the web.config or separate files that the web.config points to. If you setup a redirect for each term, you'll be good to go.
EG: in Web.config
<rewrite>
<rewriteMaps configSource="fmrewrites.config" />
<rules configSource="fmrules.config" />
</rewrite>
And within the fmrewrites.congig
<?xml version="1.0" encoding="utf-8"?>
<rewriteMaps>
<rewriteMap name="FmRedirects">
<add key="www.siteexample.com/," value="https://www.siteexample.com/" />
</rewriteMap>
</rewriteMaps>
Or within the fmrules.config
<?xml version="1.0" encoding="utf-8"?>
<rules>
<clear />
<rule name="FmRewrites" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{FmRedirects:{HTTP_HOST}{REQUEST_URI}}" pattern="(.+)" ignoreCase="true" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
You can read up on a lot of this via MS Docs.

I solved this a different way.
DNN provides a default 404 page. I simply added a custom module that evaluates the status code on Page_Load():
_rcode = Response.StatusCode;
if (_rcode == 404) //we're on our 404 page
{
Response.Clear();
Server.ClearError();
}
The module then does a few database lookups on the query string to see if there are any matches. If there are, it redirects to the appropriate page. If nothing matches, then I generate a 404 error.
I was trying to keep everything on the 404 page but I couldn't. Since the page "example.com/some-search-term" doesn't actually exist, some page components aren't available -- IsPostBack, ViewState, Session, etc. -- so it was difficult to generate a page that worked. Even setting the Response.StatusCode to 200 did not work.
Ultimately, I was trying to prevent google from seeing a 404 error unless I wanted it to. This seems to have worked.

Related

Deploying React app via FTP to Azure WebApp

I followed this tutorial to deploy a react app to and Azure WebApp
https://medium.com/#to_pe/deploying-create-react-app-on-microsoft-azure-c0f6686a4321
After I uploaded the Build folder via FTP, when I access the url i get the following message:
"You do not have permission to view this directory or page."
I tried with and without the web.config file.
Log Presented by the App
I then tried adding this to the web.config file but then it just renders a white page with the correct Page Title.
<defaultDocument enabled="true">
<files>
<add value="build/index.html" />
</files>
</defaultDocument>
I assume because it can't access the referenced .js files in the index.html.
I went to deployment options. Configured my local Git Repository.
Configured my local Git. Then inside the build folder ran:
git init
git add .
git commit -m "initial build"
git remote add <azure your-git-clone-url>
git push azure master
And it now runs
I've also followed the tutorial that you've tried.
Sometimes it works and sometimes it doesn't work.
Problem
Even after I've restarted my server, the new static content was not reflected to endpoint. (eg. https://my-endpoint.azurewebsites.net)
Or routing is not updated properly with my web.config.
Environment
Webapp(Node 10.14 + Windows), FileZilla of MacOS
Source Code: React-boilerplate
Solution
Copy and paste all files in build directory into server's webroot directory except web.config
Then, copy and paste web.config
Comment
I don't know why Azure not works as my expectation.
I've made several Azure Web app for deployment. Whenever I've created new Azure web app, some instance is updated with web.config trigger and other instance is updated with index.html trigger. However, depending on my experience, web.config looks final trigger.
If you trouble with some 4xx, 5xx error which is not mentioned in tutorial page from your Web app , just delete web app and create another new one. I also tried to connect DevOps CI/CD pipeline in Azure to Web app. However, after success build and deployment process, my site is not updated and there was no files in directory of server(I checked via FileZilla)
After that, direct FTP deployment not working. The only solution for me was deleting it and creating new Web app.
I spent my 2-3 days due to this issue. If anybody have advice or right guide or right information about Azure's internal logic for my case, please comment it for saving my few weeks in future.
Web.config
Below is mine from the tutorial
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="React 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" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Configure Web.Config (ASP.NET) to redirect web crawlers to another page

I have a website using AngularJS running in an IIS web server. Since all the site's information is being requested via AJAX, social media crawlers are unable to parse that data. So, to prevent social media from showing empty expressions, for example: {{helloWorld}}, I'm trying to redirect some social media crawlers to a specific section in my web application that can prerender the page for the social media crawlers.
I've done this with success using an Apache .htaccess file:
RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit|Facebot|Twitterbot)
RewriteRule ^(.*)$ http://myapp.com/prerender?url=%{REQUEST_URI} [P]
What i would like to know is how can i accomplish the same thing using IIS. Basically i would like configure a Web.Config file with the equivalent rewrite rule as above.
Importante, in the script above i'm using the [P] flag to informe Apache to access the prerender page without the crawler knowing that a redirection has occured.
Any ideas on how i can do this?
I was able to solve my issue.
To simply rewrite the URL to another page in the same domain, URL Rewrite must be enabled in IIS. In order to rewrite to another domain you not only need URL Rewrite, but Application Request Routing enabled in the server as well (reverse proxy).
Then just use the following example rule in web.config:
<rewrite>
<rules>
<rule name="RewriteTest" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="%{HTTP_USER_AGENT" pattern="(facebookexternalhit/[0-9]|Facebot|Twitterbot/[0-9])" />
</conditions>
<action type="Rewrite" url="http://example.com/{R:1}" />
</rule>
</rules>
</rewrite>
Hope this helps.

Azure AngularJS Index.html

My AngularJS app requires all actions to redirect to index.html for processing and it works fine on localhost, but it doesn't work in Azure.
For example:
localhost:1234 -> localhost:1234/#/ works
myapp.com -> myapp.com/#/ doesn't work
myapp.com/index.html -> myapp.com/index.html#/ works
I added:
<system.webServer xdt:Transform="Replace">
<rewrite>
<rules>
<rule name="redirect all requests" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="index.html" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
The weird thing is that this works sometimes. I can't for the life of me figure out why sometimes going to myapp.com -> myapp.com/#/ works and other times it doesn't. It seems to also break myapp.com/index.html#/ but only sometimes. This is without making any deployment changes. It just works about half the time.
Edit
Here is the actual error message:
Microsoft.Owin, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
It might be about application startup. Here are some troubleshooting ideas.
Restart your app. Does that reliably cause the error?
Turn on application logging. portal.azure.com > Browse All > The Site Name > Settings > Diagnostic logs > Application Logging > Level Error. If you want lots of stuff, set level to verbose and turn on web server logging, detailed error messages, and failed request tracing.
View diagnostics at service control manager mysite.scm.azurewebsites.net. Choose Debug console > PowerShell. Now you can look through the logs at home/LogFiles.
Based on the error you are receiving, it looks like Owin is the problem. Check your websites bin to see if there are any Owin references. From the online service control manager, you can try this:
cd site/wwwroot/bin
Get-ChildItem -filter *Owin*
If there is anything there, then it's probably the cause of the particular error you are receiving.

Angular JS with facebook sharing

URL not redirecting to Prerender.io when sharing the url from facebook.
My site is http://www.vbuy.in/ , which is in demo and I am trying to share one of the url in facebook for example "http://www.vbuy.in/product/samsung-galaxy-grand-2-black-/1975"
Title and Description are showing from the home page (I added all those supported ng-content, and in script i registed the viewtitle etc..
I wrote a URL rewrite and also tested in https://developers.facebook.com/tools/debug/og/object/.
But, for me it looks like its not redirecting to prerender.io url. What Am I missing here? . I use IIS and also downloaded ARR and enabled proxy there.
I am having tough times for the last one week with SEO & social sharing part. Even before starting the project we were aware of SEO issues in angular js, but we thought when we go
live, we can use prerender or any pre render static html support. ( we didnt evaluated in depth earlier, due to time & resource constraints)
My code .. (Which is copied from other Stack overflow solutions.
<httpProtocol>
<customHeaders>
<add name="X-Prerender-Token" value="XXXXX....." />
</customHeaders>
</httpProtocol>
<rewrite>
<rules>
<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="googlebot|baiduspider|facebookexternalhit|twitterbot" />
<add input="{QUERY_STRING}" pattern="_escaped_fragment_" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="http://service.prerender.io/http://www.vbuy.in/{R:1}" />
</rule>

ReWrite in web.config to go between mobile and desktop sites

I am finishing up a new mobile website to compliment the desktop version. Currently I have a rewrite rule in the web.config file on my desktop site that looks like so:
<system.webServer>
<rewrite>
<rules>
<rule name="MobileRedirect" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_COOKIE}" pattern="nomobile" ignoreCase="true" negate="true" />
<add input="{HTTP_USER_AGENT}" pattern="android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera mobile|palmos|webos" />
</conditions>
<action type="Redirect" url="http://m.mysite.com" appendQueryString="false" redirectType="Found" />
</rule>
</rules>
</rewrite>
This works perfect if I never want the user to be able to use the desktop site while on a mobile device, but that is not always the case. Some links on the mobile site DO link back into the desktop version. I have a link on the footer of the mobile site to "view full site" as well.
So my question IS: how to properly handle cookie setting on that link and then detecting in the web.config and NOT redirecting to the mobile version IF it exists.... I have a conditional check in the web.config for http_cookie "nomobile", but I don't think it is properly working. Do I just send in a querystring value from the mobile and check that in the global.asax file or does that not work because the web.config runs first?
The desktop is a C# MVC4 site on IIS 7.5 if any of that helps, and the mobile site is a simply jquery mobile site.
Thank you!
EDIT:
I have tried checking the querystring in the global.asax file (code below) but it seems that "Request is not available in this context".
// create and set cookie if ?nomobile detected
string forcedesktop = HttpContext.Current.Request["nomobile"];
if(forcedesktop != null){
HttpCookie nomobile = new HttpCookie("nomobile");
Request.Cookies.Add(nomobile);
}
}

Resources