Reputation: 5670
I have a URL like this www.mydomain.com/brands/bname
and I want to rewrite it to www.mydomain.com/bname
I have done some logic like this
<add name="301_bname" redirectMode="Permanent" redirect="Domain" ignoreCase="true"
rewriteUrlParameter="IncludeQueryStringForRewrite"
virtualUrl="http://(?:www.)??mydomain.com/brands/bname`"
destinationUrl="www.mydomain.com/bname`" />
but the problem is that this works only when I hardcode bname
.And in my case this bname is dynamic..What I can do to overcome this?
Note:I am new in URL rewriting case ,so if you found any mistake in my current code,you can always point out..
Upvotes: 1
Views: 3246
Reputation: 667
If you would like to redirect/rewrite /brandName_x
to /brands/brandName_x
, you need to setup a virtual URL that redirects to a real one.
<add name="redirect_brands" redirectMode="Permanent" ignoreCase="true"
rewriteUrlParameter="IncludeQueryStringForRewrite"
virtualUrl="^/(.*)$" destinationUrl="^/brands/$1" />
In the above line the virtualUrl is looking for a string "/brandName_x" which will be redirected to "/brands/brandName_x" which is where the actual node is located.
To achieve the opposite, you can do this:
<add name="redirect_brands" redirectMode="Permanent" ignoreCase="true"
rewriteUrlParameter="IncludeQueryStringForRewrite"
virtualUrl="^/brands/(.*)$" destinationUrl="^/$1" />
Upvotes: 4
Reputation: 4330
Try:
<add name="301_bname" redirectMode="Permanent" ignoreCase="true"
rewriteUrlParameter="IncludeQueryStringForRewrite" virtualUrl="^~/brands/(.*)"
destinationUrl="~/$1" />
The virtualUrl
parameter uses a regular expression to match the incoming URL. You can then use the $1
notation to pass the pattern to the destinationUrl
parameter.
Since both urls are on the same domain, and (I assume) within your web application, you don't need the redirect="Domain"
part, and you can use relative URL paths.
Upvotes: 1