Regex for fixing URL patterns - asp.net

I have the following url structure:
http://www.xyxyxyxyx.com/ShowProduct.aspx?ID=334
http://www.xyxyxyxyx.com/ShowProduct.aspx?ID=1094
and so on..
Recently I used IIS rewrite to rewrite this structure as
http://www.xyxyxyxyx.com/productcategory/334/my-product-url
http://www.xyxyxyxyx.com/productcategory/1094/some-other-product-url
and so on..
This works fine.
I want to create another rule so that if an invalid url requests comes with the following structure:
http://www.xyxyxyxyx.com/productcategory/ShowProduct.aspx?ID=334
the 'productcategory' part should be removed from the url and the url should look like
http://www.xyxyxyxyx.com/ShowProduct.aspx?ID=334
How do I write this rule?

It may vary depending on what you are using to apply the regex, but here's a basic one:
's|productcatgory/||'
If you want to make sure it also only does this when the xyxyxyxyx url is present, this should work:
's|^http://www\.xyxyxyxyx\.com/productcategory/|http://www\.xyxyxyxyx\.com/|'
Edit: Ah, so if productcategory could be any category, then you'll need to match around it, like so:
's|^http://www\.xyxyxyxyx\.com/.*/ShowProduct|http://www\.xyxyxyxyx\.com/ShowProduct|'

Related

Get the host name from url without www or extension in asp.net

Hello i need a way to find out the host part of an url , i've tried
Request.Url.Host.Split('.')
but it doesn't work with url like this:
sub.sub.domain.com
or
www.domain.co.uk
since you can have a variable number of dots before and after the domain
i need to get only "domain"
Check out the second answer at Get just the domain name from a URL?
I checked the pastebin link; it's active. I didn't test the code myself, but if it outputs as he describes, you can .split() from there.
If you need to be totally flexibel, you need to make a list of all possible top-level-domains, and try to remove those, with dot, from the end of your string, resulting in
www.domain
or
sub.sub.domain
Then take the last characters after the last dot.

Rewrite Rule Config... Is this possible?

So I have a set of pages like so:
domain.com/product/?_d=1
domain.com/product/?_d=2
domain.com/product/?_d=3
I have set up a rewrite map for cleaner URLs like so
domain.com/shoes
domain.com/pants
domain.com/gloves
So now I have nice URLs for people. But what I also want to do is start forwarding the existing URLs to the newer URLs. So for anyone who goes to:
domain.com/product/?_d=1
I want them to be forwarded or rewrite the URL to:
domain.com/shoes
I tried adding in another map where I have the following:
Original: domain.com/product/?_d=1
New Value: domain.com/shoes
And I got a 404. I guess I understand why because the other map won't apply since I am using the first map. But how can I set up a map for the old URLs? Or can I? I know if my query string value was like _d=shoes I could just do a rewrite rule, but it's not like that. I guess I could change my page to be that way, but was just looking for another way.
I ended up creating a rewrite rule that looked for the pattern to the old URLs and forwarded them to the new URL. Then in my Server side code I just did a check for those legacy variables and redirected with a 301 accordingly.

site url change by htaccess

this is my project site http://readysolution.info/usjobs/test-category/?cpid=2&c_id=acc/
i just needed show url by http://readysolution.info/usjobs/XXXX/YYYY/VVVVV instead of this
test-category/?cpid=2&c_id=acc/
this is possible by .htaccess that my site url will be show by this new format . whetver will be display after /usjobs , it will be replace another word
example : 1) if it have /usjobs/test-category/?cpid=2&c_id=acc/ , then it will be usjobs/XXXX/YYYY/VVVVV
2) 1) if it have /usjobs/test-category/?cpid=2 , then it will be usjobs/DDDD/KKKKK
please helpl me !!!!
You can use an htaccess Rewrite to "translate" a requesting URI into a WP URI. For example:
Request in the browser: mysite.com/seg_1/wp_post_slug/seg_2
With a Rewrite you can ask for mysite/com/wp_post_slug and you'll be fine.
Please note:
1) The above is pseudo code for reference purposes and might not be accurate.
2) Somewhere in the requesting RI you're going to have to have something that can be used to identify the page / post you actually want requested. For example you can't have a request of mysite.com/xxx/yyy/zzz and then somehow have a rewrite serve you mysite.com/bbb. The bbb has to be in the request so you can use the rewrite to parse it out. (Note: Actually, tere probably is a work around for this but then you're going to have to maintain some sort of reference / conversion table.)

IIS 7 URL rewrite on WCF Service

Question Edited for better understanding:
I have a WCF service and any of my links look like :
https://192.168.1.31/ContactLibrary2.0HTTPS/Service.svc/..... .
I want to get rid of the Service.svc. I installed URL Writer in IIS but i don't know how to work with it. I search a little bit and didn't find anything to help me with this particular problem.
Any idea ?
Assuming you are configuring the application hosted at /ContactLibrary2.0HTTPS directly (and not the website containing that directory, for example), you may add an exact match for:
rest/GetContact
with a rewrite url of:
Service.svc/rest/GetContact
Perhaps you wish to rewrite every action of Service.svc, however; then you would need a regular expression match for:
^rest/.*$
with a rewrite url of:
Service.svc/{R:0}
UPDATE
Assuming you also need to remove that string from the urls of your HTML pages, you would need to couple the aforementioned inbound rule with a new outbound rule, applied to the files you are interested in.
To do that, please:
add a new outbound rule to your website and give it a name;
add a new precondition with two rules (matching any of them):
{RESPONSE_CONTENT_TYPE} matches text/html
{RESPONSE_CONTENT_TYPE} matches application/xhtml+xmll
configure the rule to match the response scope, matching the content within A tags:
should match the pattern using a regular expression;
with this pattern: ^(.*)(/Service\.svc/)(.*)$
case insensitive;
configure the action to be a rewrite, with this value: {R:1}{R:3}

Need regex help for URL rewriting querystrings to friendly URLs

I updated my website CMS and the URL formats have changed. Where previously I had the URL /blog.aspx?Year=XXXX&Month=YY I now have /blog/XXXX/YY
Can someone help me create a regex for this?
Two additional notes:
it has to also support simply the year (/blog.aspx?Year=XXX)
the old Month urls use only 1 digit for single digit months (/blog.aspx?Year=2009&Month=2 instead of Month=02)
Here is what I came up with:
/blog.aspx[?]Year=([0-9]{4})([&]?)(Month=)?([0-9]*)
I can't seem to get it to work, as I still get a 404 on the page when I go to one of the above URLs.
Is this workable?
/blog.aspx\?Year=([0-9]{4})(?>\&?Month=?([0-9]{1,2})|)
works with these input
/blog.aspx?Year=1983&Month=2
/blog.aspx?Year=1983
/blog.aspx?Year=1983&Month=12
there is this (?>blabla|moomoo) syntax.
If it cant find blabla match, it will match moomoo
Though i suspect regex here is not the root problem, what CMS handles the redirect?

Resources