iis7 url rewrite - optional querystring parameters - asp.net

I'm using the iis7 URL Rewrite module and it's working fine, except for two things. Being new to this, I might be missing something obvious.
1) My URL gets converted from
www.mysite.com/search.aspx?fName=John&sName=Smith
to www.mysite.com/John/Smith. This works fine, but if I add a trailing / , a few images on the site disappear, wheras a few don't. (They're all in the same location). However, the search results are fine.
2) Is it possible to make cerain querystrings optional? Server side, this is implemented (i.e. if nothing is entered, then assume a default value). But how would this work with the URL rewrite module?
e.g. www.mysite.com/John would search for John and use a default value for the sName parameter.
Thanks for any help.

I can't help with the optional query string parameters I'm afraid, but the images one should be fairly easy:
How are you declaring the image paths in your markup? If you are using relative paths (i.e. src="../Images/someimage.png" then adding a trailing slash to the URL is telling the browser that the /Images/ folder is under the folder /John/ instead of being at the root of the site.
If you are using HTML <img /> tags, you should prefer a virtual path: src="/Images/someimage.png" - this tells the browser to request the image path from the root of your site.
If your application isn't running in the root of the site, you can also use the ResolveUrl method that is part of the page and control object tree, this allows you to pass in a virtual path of the form ~/Images/someimage.png and the framework will work out what the correct path should be.

Related

How do I create a rule that will fire only on my site's home page?

I using Google Tag Manager and I am trying to setup a rule that will fire ONLY on my sites homepage.
The issue is that I am not certain how to handle all of the URL permutations of the homepage. How can I create a rule that will handle:
"http://" "https://" "http://www." "https://www."
Also, we use Sitecore and support multiple languages, so the homepage url can also display as:
"http://www.mysite.com/en"
I am not sure how to handle the culture identifier that is inserted into the URL path after a visitor has used the navigation on the site.
Is it possible to use the OOTB Google Tag Manager rules to handle this scenario, or will I have to implement a Tag Manager Data Layer?
The following rule would check if it's the homepage:
{{url}} matches RegEx ^https?://(www\.)?mysite\.com/?(index\.html)?$
{{url}} gives the whole address whereas {{url domain}} just gives the domain and {{url path}} just the path (including the initial forward slash).
This matches http and https, with or without www and with or without index.html at the end. It also matches mysite.com/ and mysite.com (without the forward slash at the end). If you want to check for URL permutations at the end of the homepage, you could do something like:
^https?://(www\.)?mysite\.com/?(en|es|fr)?$ etc.
Also, forward slashes do NOT have to be escaped. In fact, escaping forward slashes broke the firing rule in GTM for me...
See http://en.wikipedia.org/wiki/Regular_expression
edit: and if you want to ignore the querystring (which is a good thing to do because most ads add query keys such as utm_source etc. to the url), you can have something like this:
^https?://(www\.)?mysite\.com/?(index\.html)?/?(\?.*)?$
(note the (\?.*)? at the end)
Ok... so after researching the Google Tag Manager Forum, this can be accomplished by making separate url "ends with" rules for your site url and then your site url with a trailing forward slash such as:
rule 1 : url ends with http://mysite.com
rule 2 : url ends with http://mysite.com/
I think it was the trailing slash that was confusing the matter as I was setting up the rules.
This is a super robust way to know it's your home page with either protocol and allow for a backslash all with one line of Regex...
^https?://www.mydomain\.com\/?$
Not sure sure why the initial double backslashes don't have to be escaped, but it works. Would have expected ^https?:\/\/www.mydomain\.com\/?$ Maybe someone else knows that :)

Asp.Net MVC 4 Default Url - missing trailing slash messes up href links

My project is configured to use the project url a.k.a "localhost/AppName/" and no specific page or anything, everything works fine, but if the user removes the last slash, my hrefs stop working because it ignores the "AppName", so for an example, if I've got an action called Books/Index, if the user removes the last slash "localhost/AppName" and hit enter and then clicks on the link to navigate to the books/index, the full path will be "localhost/Books/Index" which will not work, because it is missing the "AppName" after localhost.
I've tried to change the route and create a rewrite rule but still, no luck.. I'm not sure why this happends and that is what worries me, I've considered changing the way the links are generated, but I think enforcing the "/" would be better, any thoughts on this will be appreciated.
I'm using the default route by the way.
There are some ways trying to fix this, you can google it, quite some solutions there you can try. but none of them perfect.
In my option, the real fix is just to void using the relative path. In your case, you should not use books/index as the href, instead, you should always use the url helper to create the absolute path.
The cause of this problem comes from the asp.net route mechanism, so the root fix is just staying with the route mechanism.
Actually i had this problem too.. i'm not sure if this is the right solution, but this worked fine for me ! Use #Url.Action() for href..
<a href = #Url.Action("ActionName","ControllerName")>

IIS 7.0 Url Rewrite2 Rule and Regex Issues

I am using IIS.net's Url Rewrite program on my server to make rules for URL Rewrite. I have pages on my Asp.NET 4.0 Web Site such as Page.aspx, and News.aspx.
When I create a rule for Page.aspx (http://www.foo.com/Page.aspx?PageTitle=Foo to http://www.foo.com/Foo) It works great.
When I create a rule for News.aspx (http://www.foo.com/News.aspx?NewsTitle=Foo to http://www.foo.com/Foo) It works great too.
But when I create rules for both News.aspx and Page.aspx, it fails. Program looks rule order. If News's rule is at top, News.aspx Url rewritten, and Page.aspx Url rewrite fails.
If Page's rule is at top, Page.aspx Url rewritten, and News.aspx Url rewrite fails.
I guess thhis problem occurs because of regex matching. Let me show you:
Rule for News.aspx is at top. So it works fine. But when I click Page.aspx, the RewriteUserFriendlyURL1's Pattern catch my URL, and tries to redirect it News.aspx (Shown in Action Url).
I hope my description is clear.
My question is: How can I avoid this problem? For example I can make a new page called Redirect.aspx, and can redirect all pages to here. I can make 1 rule for Redirect.aspx.
But I think there should be another way to solve this issue with configuring these rules.
Thank you.
The problem is that the regular expressions for RewriteUserFriendlyURL1 and RewriteUserFriendlyURL2 are exactly the same so in that case the order in which they are listed decides which rule gets applied first.
I understand what you are trying to accomplish but it can not work like that. It's impossible for the URL rewrite module to know if /Foo is a news article or a page. Your best option is probably to put the news under /news/<title> and keep /<title> for your regular pages. In that case you can use /news/ to make a difference between news and pages.
So you should keep RedirectUserFriendlyURL2 and RewriteUserFriendlyURL2 the same but change the action URL for RedirectUserFriendlyURL1 to /news/{C:1} and change the pattern for the RewriteUserFriendlyURL1 rule to ^news/([^/]+)/?$ (note: no slash between ^ and news).

Am I able to create a taxonomy/term/%/x view path using the term alias?

I have enabled the built in taxonomy/term/% view
I have created a display with path /taxonomy/term/%/test
I also have a URL alias setup on the taxonomy/term path to be /fruits/apple
So I would expect this to work /fruits/apple/test would display my view.
When I browse to this path I get Page not found.
If I browse to the un-aliased path ie /taxonomy/term/156/test it works.
Should the alias work like this?
Ive tried every combination I can think of. Reenabled the view, created new displays, removed all other displays etc
Try this: Path redirect. If itsn't help, rewrite engine help to you.
Try sub-pathauto module.
This module extends that behavior by also matching known sub-paths and
replacing them with their respective alias.
For example, if user/1 is aliased to users/admin, this module rewrites
the link to the user contact page user/1/contact to use the aliased
URL users/admin/contact instead.

DRUPAL broken images after enabling url rewriting

I turned on URL rewriting on Drupal, and some URL image are broken.
For example :
local/tw/sites/all/themes/tw/images/1-p1.jpg become
local/tw/content/sites/all/themes/tw/images/1-p1.jpg
or
local/tw/sites/all/themes/tw/images/2-p1.jpg become
local/tw/node/sites/all/themes/tw/images/2-p1.jpg
Any ideas?
If you used relative path, and you're talking about contents within nodes, it's perfectly normal, since "node/" is interpreted like a directory.
You could fix this problem adding a "/" before image src, or using module like Path Filter that provides a simple file:relative/path/to/file syntax.

Resources