I am working on a site that uses IIS. I have it working to remove extensions but then I was informed that the client was everything removed after the trailing slash.
I first thought this might not be possible with URL Rewrite, but then again I don't know much about IIS.
Basically if anyone browses the site and clicks on an interior page the url should stay the same, the top level domain.
http://www.example.com instead of http://www.example.com/whatever
this is what I have so far to remove the extension.
<rule name="Remove PHP Extension">
<match url="(.*)\.php" />
<action type="Rewrite" url="{R:1}" />
</rule>
<rule name="Remove PHP Extension">
<match url=".*" negate="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}.php" />
</rule>
Any help would be greatly appreciated
Thanks
Doing it via IIS may be difficult since each link would actually have to navigate the user to the page in the first instance. And then you're stuck, unless you then redirect to the homepage and then perform a rewrite based upon the referrer - which is all a bit nuts.
Instead, you could use an javascript/jquery approach which may be better. Something like:
$('a').on('click', function(ev){
ev.preventDefault();
$('body').load($(this).attr('href'));
})
This would take any anchor and override it ensuring th the content was loaded into the current page.
Admittedly, this is without any conditional checks for the href pointing to an page id etc. and also falls short when considering you may already have ajax calls in tht esite.
It also doesn't take into consideration forms submittals, but you could work this in.
Another problem with this is what would happen if someone navigated to a page directly.
A final approach would be to load the entire site into an Iframe. Nasty but apprpiate if that is the effect the client wants. You could have some javascript on each page that forced itself into an iframe if it wasn't already.
Whichever approach you take, your client can kiss goodbye to any search rankings. Which reminds me, don't forget to disallow all in your robots.txt
Related
The main problem is specified in the title; however, it is a little more nuanced than that.
I cannot give away the exact address, but the URL has the syntax: https://name.mywebsite.com.
If I go to that exact address, the page will load as intended. If I try to be more specific about where exactly I want to go on the page - let's say to: https://name.mywebsite.com/home - I get a blank page.
Curiously enough, after I go to https://name.mywebsite.com, all the other addresses work as well and either redirect me to that address or display the site that they are supposed to show. I figured out that some necessary Cache-Data and data for the local storage are not set if I don't call the site over the mentioned address.
All of that wouldn't be much of a problem, since only the first call of the website is affected. However, most users of the site will first call it with a QR-Code (which leads to a specific sub-page of the site), and it wouldn't be very nice if they are greeted with a blank page.
Also, it is a .NET Core App with Angular Frontend.
I can reproduce the error by clearing my cache and cookies. The necessary data is no longer set and all domains will display a white page - except https://name.mywebsite.com - again.
I think I understood the problem, but I cannot seem to come up with a solution. I tried some changes in the IIS Manager on the server, but to no avail. My main bet was to apply an inbound rule with the URL Rewrite function, but that did not work.
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect from first shop open" enabled="false" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="https://name.mywebsite.com/" appendQueryString="true" logRewrittenUrl="true" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
I also tried that rule with all possible Redirect types. Those rules do alter the page, but not in the way I want it. Instead of the blank white page I get an error that the redirect failed (with the hint that it is probably due to a lack of cookies). That error applies to all pages, even https://name.mywebsite.com. So I have no way of accessing the website now.
I also tried to find any other configuration that might prevent other URLs from loading the data, but I did not find anything like that either (or I simply looked at the wrong places).
Edit: /home would be a route in Angular. Also, the only rewrite rule that exists and that I tried is the one shown in the picture (which I updated so that it fits better with the question). I made some changes to the question in general.
As it turns out, I simply had to make some changes to the rule I posted in the question. Changing the Redirect to a Rewrite that targets /index.html and adding some conditions (which should be adapted to your personal needs) did the trick and the website no longer shows a blank page.
The link from Lex Li in the comments from the questions angular.io/guide/deployment#fallback-configuration-examples and the link https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#accessing-url-parts-from-a-rewrite-rule helped me building the correct rule.
<rewrite>
<rules>
<rule name="Angular Routes" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern=".*/(home|item.*)" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" logRewrittenUrl="false" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name=".*" />
</rewriteMaps>
</rewrite>
I have to take a site down for maintenance, and need a site down page.
I have come across the app_offline.htm page, which seems to do what I want.
However, when I put it on one of the test sites it did not work properly and required a bit of extra work to get it working properly.
Given this, and the fact that I cannot test it on the live site, I am trying ot create a backup plan.
So I have the following web.config rule:
<rule name="Site Down Rule" stopProcessing="true">
<match url="(.*)"/>
<conditions/>
<action type="Redirect" url="app_offline.html" />
</rule>
Which should redirect to the custom page.
The problem is that it gives the following error: ERR_TOO_MANY_REDIRECTS, which I assume is because when it reaches the given page, it still tries ot redirect.
But I do not know how to solve this. I have seen this question:HTTP redirection issue in IIS, keep getting ERR_TOO_MANY_REDIRECTS on the browser
and the related question it references, but these do not provide a working solution.
Any ideas?
EDIT:
Is it possible to add an exclusion to this rule - which says do not redirect given page
Got it - I just needed to exclude the page that was being redirected to:
<rule name="Site Down Rule" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{REQUEST_URI}" negate="true" pattern="app_offline.html" ignoreCase="true" />
</conditions>
<action type="Redirect" url="app_offline.html" />
</rule>
My hosting plan has a limited number of web applications for use, but unlimited subdomains. I plan to take advantage of these subdomains by using IIS rewriting, like the following:
<rule name="Home Rewrite" enabled="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^home\.mydomain\.com$" />
</conditions>
<action type="Rewrite" url="home/{R:1}" />
</rule>
This works fine for the most part, I can go to http://home.mydomain.com and it'll take me to what is essentially http://www.mydomain.com/home as expected.
I am publishing using Web Deploy, and I believe the host is IIS7.
The problem is that I want to take advantage of #Html.ActionLink, but when viewing the source, this resolves out to include the virtual directory.
So what I end up with is a site that works when I go to the original address:
http://www.mydomain.com/home/application
And a site that loads, but doesn't function correctly, at the redirected address:
http://home.mydomain.com/application
With generated URLs in the page source pointing relative to the original address:
/home/application/Account/Login
This applies to links to other pages/routes, bundles, basically anywhere that ~/ or #Html.ActionLink is used.
How do I get around this? I'm hoping to keep the use of #Html.ActionLink at least, I think I can live without the tildes.
I finally found a solution!
https://support.gearhost.com/entries/23689272-URL-Rewrite-Subdomain
My web.config rewrite rule required an extra line:
<rule name="Home Rewrite" enabled="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^home\.mydomain\.com$" />
<add input="{PATH_INFO}" pattern="^/home/" negate="true" /> <!-- This one! -->
</conditions>
<action type="Rewrite" url="home/{R:1}" />
</rule>
Now everyone is happy :)
You could write a custom HtmlHelper so instead of using ActionLink you can use MyActionLink and it can generate the url you need.
For better SEO we are using URL rewrite to convert all the URL's to lowercase. I set this one as mentioned in this the below article.
Everything is working fine from URL perspective, but we see lot of 301 redirects when we check in fiddler.
It looks like the images, javascript, css, jquery ajax calls and everything is getting converted into lower case.
I am trying to remove that and want to rewrite only aspx extension and no extension urls. I tried to play around the matchurl without any success. Any help or guidelines will be highly appricated.
Thanks
Edit:
My Current rule is
<rules>
<rulename="LowerCaseRule1"patternSyntax="ExactMatch"stopProcessing="true">
<matchurl="[A-Z]"ignoreCase="false"/>
<actiontype="Redirect"url="{ToLower:{URL}}"/>
</rule>
</rules>
You could probably use something as follow:
<rule name="LowerCaseRule1" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{URL}}" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" pattern="\.aspx$" />
<add input="{REQUEST_FILENAME}" pattern="\." negate="true" />
</conditions>
</rule>
The rule will be triggered only if one of the condition is true:
The first one checks if the requested path (filename) ends with .aspx.
The second one checks if the if the requested path (filename) doesn't contain a . (so doesn't have an extension)
That title isn't very descriptive, but I couldn't figure out how to phrase my question very well. What I'm trying to do is use a single page to interpret multiple URLs. Here's an example: [domain]/name-of-question.aspx is clearly not a file on the site's server, and yet the server acts like it is. This behavior makes pages much more readable and more easily bookmark-able.
My vision for the solution is to be able to have to server redirect a request to a certain directory to a particular page, whilst appending the name of the page requested to the page as a URL parameter. Here's what I mean: [domain]/questions/name-of-question redirects to [domain]/question.aspx?page=name-of-question.
This is how reddit does their self posts, I think, but they don't use ASP.Net or IIS.
Is this possible, and if so, how would one implement this behavior? If there's any code you write, please write it in C#, because I don't know VB.Net very well. Thanks!
You need to use URL rewriting to accomplish this.
You have to create a rewrite rule that rewrites any requests to [domain]/questions/{1}
to [domain]/question.aspx?{1}
In ASP.NET you have the URL Rewriter module: http://www.iis.net/downloads/microsoft/url-rewrite
The rule might look similar to this and is applied in the web.config file:
<rewrite>
<rules>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="questions/(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="question.aspx?{R:1}" />
<serverVariables>
<set name="{RESPONSE_CONTENT_TYPE}" value="image/png" />
</serverVariables>
</rule>
</rules>
</rewrite>
EDIT: To change the content type, add the serverVariables section in the rewrite rule and authorise that variable to be set in IIS manager:
If you are using ASP.NET 4.0+, then this might be worth a read, as ASP.NET Routing and URL Rewriting are not necessarily competing technologies, but potentially complementary features.
URL Rewriting vs. ASP.NET Routing