URL Rewrite language querystring - iis-7

I want to rewirte my querystring for selected language.
I have this URL: www.example.com/?lang=en, and want it to be www.example.com/en
It should rewrite on all pages. So www.example.com/contact.aspx?lang=en would be www.example.com/en/contact.aspx
Is there a general rewrite rule for this?

This one works.
<rule name="Rewrite Language">
<match url="([a-z]{2})(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:2}?lang={R:1}" />
</rule>

my syntax may be a little off but you could probably do something like this:
<rewrite>
<rules>
<rule name="Rewrite Language">
<match url="/([a-z]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="{R:2}?lang={R:1}" />
</rule>
</rules>
</rewrite>

Please consider using the following:
<rewrite>
<rules>
<rule name="Rewrite Language">
<match url="/([a-z]{2})(.*)" />
<action type="Rewrite" url="{R:2}?lang={R:1}" />
</rule>
</rules>
</rewrite>

Related

IIS web.config redirect rule for specific URL but not others

I want to add a redirect rule to the web.config file http://myurl.com/path/to/old
to http://myurl.com/path/to/new
but http://myurl.com/fr/path/to/old
http://myurl.com/cn/path/to/old
should stay the same. how can i achieve that with match/rule?
Add this to your web.config:
<rewrite>
<rules>
<rule name="Redirect old to new" stopProcessing="true">
<match url="^path/to/old" ignoreCase="false" />
<conditions>
<add input="{URL}" pattern="^fr/.*" ignoreCase="false" negate="true" />
<add input="{URL}" pattern="^cn/.*" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="/path/to/new" />
</rule>
</rules>
</rewrite>

Setting up URL Rewrite rule

I've a rewrite role in my web.config, and it's redirect to mydomain.com/de.
But now I will replace the action de/{R:1} with the user language, where I can get from {HTTP_ACCEPT_LANGUAGE}. But this is a string of language and looks like fr-CH,**fr**;q=0.8,en;q=0.6,de;q=0.4,de-CH;q=0.2/
So it's there possibility to get the out only the **fr** of this string?
Thanks for any help.
This is my role:
<rule name="mydomain.com" stopProcessing="true" >
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="/en/|/de/" negate="true" />
</conditions>
<action type="Redirect" url="de/{R:1}" />
</rule>
In the past i have used the following rule to redirect to a language based page
<rewrite>
<rules>
<rule name="RedirectToLang" enabled="true" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="{R:0}/{HTTP_ACCEPT_LANGUAGE}" appendQueryString="true" />
<conditions>
<add input="{HTTP_ACCEPT_LANGUAGE}" pattern=".+" />
</conditions>
</rule>
</rules>
</rewrite>

Removing index.cfm from url with web config

quick question -
Currently my urls look like this: index.cfm/camp/another-test
I would like for them to look like this: camp/another-test
I'm able to do this fine on apache with my .htaccess but I need to be able to do it on iis7 with the web.config. Here's my rewrite so far:
<rewrite>
<rules>
<rule name="Remove index.cfm" enabled="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" negate="true" pattern="^/(assets|files|miscellaneous|robots.txt|favicon.ico|sitemap.xml|index.cfm)($|/.*$)" />
</conditions>
<action type="Rewrite" url="/index.cfm/{R:1}" />
</rule>
</rules>
</rewrite>
Thanks for the help!
I believe CFWheels requires that you route rewrite requests through rewrite.cfm not index.cfm.
See the comment by Chris Peters on this question
If you adjust:
<rewrite>
<rules>
<rule name="Remove index.cfm" enabled="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" negate="true" pattern="^/(assets|files|miscellaneous|robots.txt|favicon.ico|sitemap.xml|index.cfm)($|/.*$)" />
</conditions>
<action type="Rewrite" url="/index.cfm/{R:1}" />
</rule>
</rules>
</rewrite>
to:
<rewrite>
<rules>
<rule name="ColdFusion on Wheels URL Rewriting" enabled="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" matchType="Pattern" ignoreCase="true" negate="true" pattern="^/(flex2gateway|jrunscripts|cfide|CFFileServlet|cfformgateway|railo-context|files|images|javascripts|miscellaneous|stylesheets|robots.txt|favicon.ico|sitemap.xml|rewrite.cfm)($|/.*$)" />
</conditions>
<action type="Rewrite" url="/rewrite.cfm/{R:1}" />
</rule>
</rules>
</rewrite>
it should solve your problem, provided you have:
<cfset set(URLRewriting = "On")>
within /config/settings.cfm
Try adding this rewriting rule:
<rewrite>
<rules>
<rule name="ColdFusion on Wheels URL Rewriting" enabled="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" negate="true" pattern="^/(flex2gateway|jrunscripts|cfide|CFFileServlet|cfformgateway|railo-context|files|images|javascripts|miscellaneous|newsletters|stylesheets|robots.txt|favicon.ico|sitemap.xml|rewrite.cfm)($|/.*$)" />
</conditions>
<action type="Rewrite" url="/rewrite.cfm/{R:1}" />
</rule>
</rules>
</rewrite>

remove .aspx extention using IIS7 & URL Rewrite [duplicate]

I'm using ASP .NET rewriteModule to rewrite http://example.com to http://www.example.com.
<section name="rewriteModule" type="RewriteModule.RewriteModuleSectionHandler, RewriteModule"/>
Then i have this inside <system.webServer>.
<rewrite>
<rules>
<rule name="Canonical" stopProcessing="true">
<match url=".*"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^([a-z]+[.]com)$"/>
</conditions>
<action type="Redirect" url="http://www.{C:0}/{R:0}" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
Now i want to remove all the .aspx in the end of my pages. Example:
http://www.example.com/Register.aspx
Will turn into:
http://www.example.com/Register/
How can i do that?
I'm on Shared Web Hosting on GoDaddy using IIS7.
These are the standard rewrite rules I start every project with. I use only clean URLs for all the pages (example first rule works for www.example.com/about and second rule www.example.com/product/123)
<rewrite>
<rules>
<rule name="Rewrite default to aspx" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="default.aspx" />
</rule>
<rule name="Rewrite page to aspx" stopProcessing="true">
<match url="^([a-z0-9/]+)$" ignoreCase="false" />
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
</rules>
</rewrite>
Pages where I need to parse out the ID (this case number only) and add it to the query string I add a similar rule to the front:
<rule name="Rewrite Product ID" stopProcessing="true">
<match url="^product/([0-9]+)$" ignoreCase="false"/>
<action type="Rewrite" url="product.aspx?id={R:1}"/>
</rule>
If you want to use lower and upper case letters in the URL, set ignoreCase="true"
Edit to answer your second question plus a bonus
This rule will redirect aspx page to the clean URL:
<rule name="Redirect to clean URL" stopProcessing="true">
<match url="^([a-z0-9/]+).aspx$" ignoreCase="true"/>
<action type="Redirect" url="{R:1}"/>
</rule>
Replace url="{R:1}" with url="{ToLower:{R:1}}" to change URL to lowercase. See below why you would want to do this.
Also a good idea to update the Form action so that post backs don't return back to the ugly URL. Using IIS 7.5 or newer this should work:
if (!String.IsNullOrEmpty(Request.RawUrl))
form1.Action = Request.RawUrl;
or for IIS 7:
if (!String.IsNullOrEmpty(Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"]))
form1.Action = Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"];
One more thing to keep in mind... it's a good idea to keep all URLs lower case. Mixing lower/upper case characters in the URL creates duplicate content issues for SEO/Google. For example website.com/About and website.com/about will load the same page, but Google will index them as two separate pages.
First you need to remove the .aspx (default.aspx) and redirect to default to change the browser address then add the .aspx and rewire to page using IIS
<rewrite>
<rules>
<clear />
<rule name="Redirect to clean URL" enabled="true" stopProcessing="true">
<match url="^([a-z0-9/]+).aspx$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="RewriteASPX" enabled="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="Rewrite" url="{R:1}.aspx" />
</rule>
</rules>
</rewrite>
<rewrite>
<rules>
<remove name="RewriteUserFriendlyURL1" />
<remove name="RedirectUserFriendlyURL1" />
<rule name="RedirectUserFriendlyURL2" stopProcessing="true">
<match url="^www\.myserver\.com/(.*)\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
</conditions>
<action type="Redirect" url="www.myserver.com/{R:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL2" stopProcessing="true">
<match url="^www\.myserver\.com/(.*)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="www.myserver.com/{R:1}.aspx" />
</rule>
</rules>
<outboundRules>
<remove name="OutboundRewriteUserFriendlyURL1" />
<rule name="OutboundRewriteUserFriendlyURL2" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*)www\.myserver\.com/(.*)\.aspx$" />
<action type="Rewrite" value="www.myserver.com/{R:1}" />
</rule>
</outboundRules>
</rewrite>
this will do it - I have generated this vis IIS on my local machine - change myserver.com to your own URL. you can change the regex to actually take care of the x.aspx part of the url then it should work across all pages

httpRedirect - Redirect from http://www.*.com to http://*.com

I'm running IIS7 and I need to setup a redirect in my web config - in the httpRedirect...
What I need to do is up a couple permanent redirect:
- http://www.*.com to http://*.com, and
- http://*.com/test.html to http://*.com/test
Here is a rule for IIS7 that will work for your first requirement. The RedirectType is permanent (301).
<rewrite>
<rules>
<rule name="test" stopProcessing="true">
<match url="^www\.(\w+\.com)$" />
<action type="Redirect" url="http://{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Your second requirement will require the following rules:
<rewrite>
<rules>
<rule name="RedirectUserFriendlyURL" stopProcessing="true">
<match url="^domain\.com/test\.html$" />
<conditions>
<add input="{REQUEST_METHOD}" negate="true" pattern="^POST$" />
</conditions>
<action type="Redirect" url="domain.com/test" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="RewriteUserFriendlyURL" stopProcessing="true">
<match url="^domain\.com/test$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="domain.com/test.html" />
</rule>
</rules>
</rewrite>
These rules will rewrite http://domain.com/test.html to http://domain.com/test.
You may need to change some of the settings to fit your needs.

Resources