I have a multilingual site in MVC 5. I've implemented SEO friendly URL using attribute routing.
Here are the sample URL:
http://mycompany.com/en
http://mycompany.com/en/about_us
http://mycompany.com/en/product/type/item
http://mycompany.com/fr
http://mycompany.com/fr/about_us
http://mycompany.com/fr/product/type/item
Now, I need to remove language code (en) for default language English.
How can I achieve that using IIS URL rewrite tool?
Any alternate solution would work too.
You can try this rule. When user enters the url, it will be redirected to URL without language and show in the browser.
<rule name="rule2" enabled="true" stopProcessing="true">
<match url="en(.*)" />
<action type="Redirect" url="{R:1}" />
</rule>
Related
We have a requirement for redirecting couple of webpages to different URLs of our website. For instance:
https://old-domain/-->https://new-domain/;
https://old-domain/promotion/campaign-->https://new-domain/;
https://old-domain/about-us-->https://new-domain/about-us/company;
https://old-domain/about-us/awards-and-recognitions-->https://new-domain/about-us/company;
https://old-domain/careers-->https://new-domain/careers/;
https://old-domain/careers/apply-for-a-job-->https://new-domain/careers/;
https://old-domain/claim-status-->https://new-domain/;
https://old-domain/contact-us-->https://new-domain/contact-us;
We need generic rewrite rules for the above, where
we either redirect to the desired destination URLs (manually hard-coding them in the rewrite rules)
or
add a single redirect rule for the first URL and regex expression to skip the rest (since the rest can be managed internally within the CMS but not the first)
I would do it via IIS Rewrite Rules in the web.config (unless the client requires to be able to edit those in the UI)
I would add a rule for each Url (or maybe combine a few where possible)
They could look like this:
<rewrite>
<rules>
<rule name="Redirect old domain" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="[www.]oldDomain.com$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www.newDomain.com/{R:0}" />
</rule>
....
</rules>
</rewrite>
Edit: Updated the rule above so that it will cater for any page on the old domain and will redirect to the same page of the new domain.
Examples:
www.olddomain.com will redirect to www.newdomain.com
www.olddomain.com/about-us will redirect to www.newdomain.com/about-us
Now, you can create the /about-us page in Sitefinity and make it a redirect page and redirect to wherever you want.
I am using the IIS Rewrite module with my web.config, and would like to rewrite certain requests by replacing a word in the URL.
Example: http://domain.com/windows to be http://domain.com/WINDOWSSoftware
I'm aware that a URL with query string parameters can be rewritten using for example below rule
<rules>
<rule name="Rewrite to article.aspx">
<match url="^article/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="article.aspx?id={R:1}&title={R:2}" />
</rule>
</rules>
Instead of /article?id=243&title=some-title we can have /article/234/some-title
I'm wondering if this can be achieved using URL Rewrite, or can it be done by developing a custom provider.
This can definitely done with url rewrite module with bit of regex pattern matching.
http://domain.com/windows/sdfsdf
so if you just match {R:1} or {C:1} depending on how you setup
The final redirect can look like
{HTTP-HOST}/WindowsSoftware/{R:2}
I have setup a URL rewrite in IIS 7 for a particular site, that has 2 bindings.
main.mydomain.com
hub.mydomain.com
I have also applied a URL Rewrite Rule as shown below:
match (.*)
and then
under the condition
where {HTTP_HOST} matches ^hub\.mydomain\.com$
301 redirect to
http://main.mydomain.com/hub/home.html
and this works, the purpose was to have hub.mydomain.com direct the user to a URI of http://main.mydomain.com/hub/home.html
I have now been asked to change this so that the hub.mydomain.com remains in the user's browser address but that they are shown the correct /hub/home.html content.
How can this be achieved? I presume that as the name suggests, URL Rewrite is no longer suitable? and if so how else can I do this?
EDIT:
main.mydomain.com still needs to go to the root of the website.
In your question, you state that main.mydomain.com and hub.mydomain.com are binded to a single website.
So if you want the users who hit hub.mydomain.com to be shown with the content from http://main.mydomain.com/hub/home.html, it is equivalent to have them hit hub.mydomain.com and be shwon the content from http://hub.mydomain.com/hub/home.html.
You rule would then go as:
<rule name="hub rewrite">
<match url="^/?$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^hub\.mydomain\.com$" />
</conditions>
<action type="Rewrite" url="hub/home.html" />
</rule>
I'm using IIS URL rewriting module to mask my internal URLs with friendly URLs through rewrite maps and Rewrite rules (not redirection). This is my rewrite map:
<rewriteMap name="HashTest">
<add key="/nohash" value="/nohash.aspx" />
<add key="/hash1" value="/hashtest.aspx#hash1" />
</rewriteMap>
and this is my rewrite rule:
<rule name="Rewrite rule1 for HashTest">
<match url=".*" />
<conditions>
<add input="{HashTest:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" />
</rule>
This is working for URLs with no hashtags, so every time I query www.mysite.com/nohash it shows me content from www.mysite.com/nohash.aspx with our changing the URL on the browser.
Now when I try to rewrite to a URL containing a hashtag I get a 404 error, for instance www.mysite.com/hash1 should just show me content from /hashtest.aspx#hash1 but I just get a 404.
Now if I change my rule action type to Redirect it does make redirection successfully, so I don't know why it doesn't work with rewrite.
I know hashtags are not sent to the server on the request, but it would make sense if my rewrite map was backwards, like <add key="/hashtest.aspx#hash1" value="/hash1" />.
Any insights on why redirection works with hastags but rewrite doesn't?. I'm not married to IIS redirection, if you have another module or approach I can use it's very welcome
The part after the hash sign (officially called the fragment identifier) is a client-side only part of the URL. It's never send to the server. That's why it won't work for rewrite but only for redirects. The rewrite rule will match but IIS will actually try to open a file called hashtest.aspx#hash1 (i.e. a file with the extension .asp#hash1). This file won't be processed as a normal ASP page as the extension is not linked to ASP.NET. And most likely it's content won't even be displayed at all as IIS is by default configured to only allow request for known extensions.
I want to create a rule to redirect query to a page (which doesn't exist) to another
Example:
http://www.example.com/en/page.asp?id=2&...
to
http://www.example.com/en-US/newpage.asp?id=2&...
I use this rule:
<rule name="Redirect" stopProcessing="true">
<match url="page\.asp\?(.+)$" />
<action type="Rewrite" url="newpage.asp?{R:1}" />
</rule>
But this doesn't work... I got a 404 error...
What is my mistake?
Thanks
URL Rewrite's Rewrite action is only for rewriting the page URL that
gets displayed on the browser but it expects the original page to
exist on the server. For your case, you need a Redirect action.
The regex needs to be changed to reflect "en-US" in the final URL.
Try this code instead:
<rule name="Redirect" stopProcessing="true">
<match url="en/page\.asp\?(.+)$" />
<action type="Redirect" url="en-US/newpage.asp?{R:1}" redirectType="Permanent"/>
</rule>
The permanent redirect helps makes your website SEO (Search Engine Optimized) preventing search engine bots to index the old URL (and hence not splitting page ranks between the 2 URLs).