Redirect old urls when changing to prefixless in Piranha Cms - 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 ;)

Related

How to stop double redirect on https and www in ASP.NET Core 6?

I was using IIS rewrite rules in web.config to do my HTTPS and non-www canonical URI enforcement, but then using Web Deploy became complicated so I switched to ASP.NET Core 6 middleware. Here's my code from Program.cs:
app.UseRewriter(new RewriteOptions().AddRedirectToNonWwwPermanent());
app.UseHttpsRedirection();
For http://example.com and https://www.example.com it works great and there is a single redirect.
But, for http://www.example.com it now implements the non-www rule, then the https rule, so you get two 308 redirects chained together (with IIS I had this with a single redirect).
You can test it here: https://www.redirect-checker.org/index.php
Enter: http://www.magazedia.com/
http://www.magazedia.com
308 Permanent Redirect
http://magazedia.com/
308 Permanent Redirect
https://magazedia.com/
200 OK
This plays havoc with my OCD.
Is there a simple fix or do I need to write a custom redirect that combines both?
EDIT:
I also just tried this, but the result is the same (double redirect):
app.UseRewriter(new RewriteOptions().AddRedirectToNonWwwPermanent().AddRedirectToHttpsPermanent() );
// app.UseHttpsRedirection();
As far as I know, the asp.net core url rewrite middleware also support using the url rewrite rule xml.
You could create a url reiwte xml named IISUrlRewrite.xml and like below:
<rewrite>
<rules>
<rule name="Rewrite segment to id querystring" stopProcessing="true">
<match url="^iis-rules-rewrite/(.*)$" />
<action type="Rewrite" url="rewritten?id={R:1}" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
Then modify the startup.cs(.net 5) or program.cs(.net 6)
using (StreamReader iisUrlRewriteStreamReader =
File.OpenText("IISUrlRewrite.xml"))
{
var options = new RewriteOptions()
.AddIISUrlRewrite(iisUrlRewriteStreamReader)
app.UseRewriter(options);
}
More details, you could refer to this article.

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

.Net Core / IIS web config rewrite rules

In my situation I have two domains:
domain.com
domain1.com
where domain1.com is the alias of domain.com
For domain1.com I want to allow only traffic for the urls starting with api/ and redirect all other traffic to domain.com with 301 status code.
i.e. I want to allow requests like domain1.com/api/products/list on the other hand I want to redirect domain1.com/products/list to domain.com
Can anyone please guide how to achieve this with web.config?
Following URL redirect rule should work.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Domain1">
<match url="^(.+)" />
<conditions>
<add input="{HTTP_URL}" pattern="^/api/(.+)" negate="true" />
</conditions>
<action type="Redirect" url="http://domain/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Quick explanation.
We capture the whole incoming URL with match pattern for back-reference for the redirect if needed.
And in conditions we negate the API URLs pattern, hence the rule is applied to the URLs not containing /api.
The redirect action takes the captured back-reference and appends it to the domain.
Notes.
You might need to adjust slashes in the rule, I assume there will always be a slash after /api.
Browsers heavily cache permanent redirects, make sure to use a new Incognito/Private session each time you modify the rule.

Converting a web.config rewrite rule to Apache format

I would like to convert this web.config
<rule name="cambiarPass" stopProcessing="true">
<match url="^cambiarPass/" />
<action type="Rewrite" url="modulos/cambiarPass/controller.php" appendQueryString="false" />
</rule>
to .htaccess. Please help!
Bear in mind that I do not use IIS but the provided code seems pretty self-explanatory in terms of how it should be converted to an Apache rewrite rule.
The <match url="^cambiarPass/" /> line is set to apply URL rewriting only to URLs (paths) which begin with cambiarPass/. The
<action type="Rewrite" url="modulos/cambiarPass/controller.php" appendQueryString="false" />
line is the one doing the rewriting and redirecting all matched URLs to modulos/cambiarPass/controller.php. The appendQueryString attribute is obviously a synonym for the Apache QSA rewrite flag meaning the rewrite process will discard and ignore any existing query string data during the rewrite. The stopProcessing attribute seems to be yet another equivalent for the Apache L rewrite flag meaning if this rule is matched any additional rewrite rules that might follow the current rule will simply be ignored.
Here is the complete code.
RewriteEngine on
RewriteRule ^cambiarPass/ modulos/cambiarPass/controller.php [L]

301 redirect on IIS. Redirect non-www to www

I ought to redirect my website from non-www to www on IIS 6. e.g if I enter domain.com in the url, it has to redirect to www.domain.com for SEO optimization.
I followed the video "http://www.youtube.com/watch?v=PYxabNrIMQ4" for creating my rewrite rule. And I made it as below
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="mydomain.com" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:0}" />
</rule>
</rules>
</rewrite>
But it is not working as expected. Help me out to find the solution.
Note:
My Visual studio shows warning like "The element 'system.webServer' has invalid child element 'rewrite'".
I believe it doesn't make any problem.
According to this video you can redirect using the IIS Manager UI. Make sure you have a second site for the non-WWW version, with the same "Location" as the WWW version of your site; when creating it, be sure to specify your non-WWW domain for the "Host Header" field.
After creating it, right-click the site and go to Properties. Disable logging if you're so inclined (probably not needed because you'll be redirecting anyways), then go to the "Home Directory" tab. Now remove the Application by clicking the appropriate "Remove" button, since it's not used for redirection. Next, select the "A redirection to a URL" radio button, be sure to check "The exact URL entered above" and "A permanent redirection for this resource".
Lastly, in the "Redirect to" field, enter your full WWW url followed by $S$Q (i.e. http://www.example.com$S$Q) -- the $S$Q are important to capture any path and query string the client might send, so if the user goes to http://example.com/foo/bar.html they will be appropriately redirected to http://www.example.com/foo/bar.html instead of just http://www.example.com/ . Click Apply and OK, and your redirection should be set!
Does this works ? remember to add in the root web.config
<rules>
<rule name="Redirect domain.com to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain.com" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}" />
</rule>
</rules>