Url rewriting with IIS 6 and MVC 3 - asp.net-mvc

I have a url that can look like
domain.com/f1/v1/f2/v2/f3/v3/f4/v4/f5/v5/f6/v6
with variations of lesser counts such as
domain.com/f1/v1/f2/v2/f3/v3/f4/v4/f5/v5
domain.com/f1/v1/f2/v2/f3/v3/f4/v4
domain.com/f1/v1/f2/v2/f3/v3
domain.com/f1/v1/f2/v2
domain.com/f1/v1
how can i build a rewrite rule that will construct the following
domain.com/models?f1=v1&f2=v2...f6=v6
this website is an IIS 6 MVC 3 application using Helicon Isapi_Rewrite for url rewrites.

The best way is to create a set of rules each rule will process 1 pair, 2 pairs, 3 pairs of paraters...
RewriteEngine on
RewriteBase /
# rule for one pair of parameters
RewriteCond %{HTTP:Host} ^domain\.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)$ /models?$1=$2 [NC,L]
# rule for two pairs of parameters
RewriteCond %{HTTP:Host} ^domain\.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ /models?$1=$2&$3=$4 [NC,L]

I came up with this solution using a map. The main problem I had was exceeding the $1-$9 limitation on placeholders.
RewriteMap filtermap txt:content/maps/filtermap.txt [NC]
RewriteCond ${filtermap:$6|NOT_FOUND} !NOT_FOUND [NC]
RewriteRule ^models/([^/]*/[^/]*)/([^/]*/[^/]*)/([^/]*/[^/]*)/([^/]*/[^/]*)/([^/]*/[^/]*)/([^/]*/[^/]*) /models/?${filtermap:$1}&${filtermap:$2}&${filtermap:$3}&${filtermap:$4}&${filtermap:$5}&${filtermap:$6} [NC,L]
RewriteCond ${filtermap:$5|NOT_FOUND} !NOT_FOUND [NC]
RewriteRule ^models/([^/]*/[^/]*)/([^/]*/[^/]*)/([^/]*/[^/]*)/([^/]*/[^/]*)/([^/]*/[^/]*) /models/?${filtermap:$1}&${filtermap:$2}&${filtermap:$3}&${filtermap:$4}&${filtermap:$5} [NC,L]
RewriteCond ${filtermap:$4|NOT_FOUND} !NOT_FOUND [NC]
RewriteRule ^models/([^/]*/[^/]*)/([^/]*/[^/]*)/([^/]*/[^/]*)/([^/]*/[^/]*) /models/?${filtermap:$1}&${filtermap:$2}&${filtermap:$3}&${filtermap:$4} [NC,L]
RewriteCond ${filtermap:$3|NOT_FOUND} !NOT_FOUND [NC]
RewriteRule ^models/([^/]*/[^/]*)/([^/]*/[^/]*)/([^/]*/[^/]*) /models/?${filtermap:$1}&${filtermap:$2}&${filtermap:$3} [NC,L]
RewriteCond ${filtermap:$2|NOT_FOUND} !NOT_FOUND [NC]
RewriteRule ^models/([^/]*/[^/]*)/([^/]*/[^/]*) /models/?${filtermap:$1}&${filtermap:$2} [NC,L]
RewriteCond ${filtermap:$1|NOT_FOUND} !NOT_FOUND [NC]
RewriteRule ^models/([^/]*/[^/]*) /models/?${filtermap:$1} [NC,L]
Its not super pretty but it does work. The filter map looks like this
p1/v1 p1=v1
p2/v2 p2=v2
...
This is not an ideal solution if your combinations of p1/v1 are unlimited, but mine are finite so its pretty easy to manage.

Related

Trouble with long url (rewrite mod) subpages

Im looking for someone who will knowing the main code
I have subpages with ugly names www.forexample.eu/index_pl.php?src=home How cut it for this url www.forexample.eu/start
Pls tell me how?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index_pl.php?src=home=$1&subpage=$2 [L,QSA]
Try the following:
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^start index_pl.php?src=home [L]
If your link is www.forexample.eu/start it would load www.forexample.eu/index_pl.php?src=home 'in the back' but it would still show the first link.

Infinite loop in rewrite rule: How to prevent www in url?

I'd like my site to be accessible by the following two urls only:
http://domain.com
http://origin.domain.com
But the following rewrite rule gets into an infinite loop:
RewriteCond %{HTTP_HOST} !^(origin)\.domain\.com [NC]
RewriteRule (.*) http://domain.com$1 [R=301,L]
What am I doing wrong? Any help would be greatly appreciated!
Change the first line to
RewriteCond %{HTTP_HOST} !^(origin)?\.?domain\.com [NC]

How to define a subdomain URL rewrite rule for more complex URL

how to create rewrite rule for that url like
http://www.abc.in/subfoldername/index.php?alist&TP=xyz&ct=bcd
to
http://bcd.abc.in
Enter this following code to .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9_-]+)\.(.+)$
RewriteRule ^$ http://%{HTTP_HOST}/subfoldername/index.php?alist&TP=xyz&ct=%2 [L]

htaccess multi rss feed redirection with language variable

I have a multilingual blog, default language is Italian and second language is English.
So I have 2 RSS feeds:
/feed
/feed/?lang=en
The first points to Italian the second to English.
I wanna both redirect to Feedburner using .htaccess, the first should point to http://feeds.feedburner.com/SimoBlog and the second to http://feeds.feedburner.com/digitalking/haPl
I successfully done it for the first with this code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !^.*(FeedBurner|FeedValidator) [NC]
RewriteRule ^feed/?.*$ http://feeds.feedburner.com/SimoBlog [L,NC,R=302]
</IfModule>
I don't figure out to make it working also for the second feed URL, any help?
Thanks, Simone
You could check if the query string is equal to lang=en:
RewriteCond %{HTTP_USER_AGENT} !(FeedBurner|FeedValidator) [NC]
RewriteCond %{QUERY_STRING} !=lang=en
RewriteRule ^feed/?$ http://feeds.feedburner.com/SimoBlog [L,NC,R=302]
RewriteCond %{HTTP_USER_AGENT} !(FeedBurner|FeedValidator) [NC]
RewriteCond %{QUERY_STRING} =lang=en
RewriteRule ^feed/?$ http://feeds.feedburner.com/digitalking/haPl [L,NC,R=302]

URL rewrite in smarty based website?

I wanted to achieve this
example.com/search_results/?action=search&username[equal]=PorscheSA
to
example.com/PorscheSA
I have used .htaccess for many websites to achieve this, but since this website is smarty based, it doesn't seem to work. Any help will be much appreciated.
Ok, this is the .htaccess file:
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php
In order to achieve the following I tried this:
RewriteRule ^([a-zA-Z0-9-_+^/])/$ /search_results/?action=search&username[equal]=$1
and nothing happened.
Your question is still a little vague as to what exactly you expect to have happen, but since you've posted your .htaccess, I'll take a stab at it and see if I get what you were after.
The most obvious reason that your RewriteRule isn't working is that your test pattern doesn't match the URL you've provided as an example, as your RewriteRule requires a trailing slash (and only matches one character before that). Additionally, if you put it after the rules that you currently have in your .htaccess file, it will never match because the request will have already been rewritten to index.php.
I think that you want something like this...
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteRule ^.*$ http://example.com/$0 [L,R=301]
# Make sure we end and force the redirect so %{REQUEST_FILENAME} gets changed
RewriteRule ^[a-zA-Z0-9-_+^/]+$ search_results/?action=search&username[equal]=$0 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
However, if the search_results/ directory doesn't exist, then this is just going to be rewritten to index.php anyway, so I'm not sure what the purpose of the redirection would be in that case. And, if it does exist, since your test pattern matches pretty much anything I'd expect to see in a site path, then everything will be rewritten to the search_results/ directory, so very little (if anything) will end up at your site root's index.php.
Based on that, I feel like maybe there's some other criteria that you may have left out, but I could be wrong.

Resources