IIS Rewrite URL to remove duplicate query strings - iis

Is it possible to rewrite/redirect a url such as this:
http://example.com?id=123&id=123
To instead be:
http://example.com?id=123
The actual reason for doing this is a bug in code, but a URL rewrite/redirect would be a quick workaround.

Didn't realize it was possible to add a condition for {QUERY_STRING} that matches the pattern id=(.*)&id=.* and then just redirect to http://{HTTP_HOST}/{R:0}?id={C:1}

Related

.htaccess rewrite url that has already been changed

I am upgrading my site which involves new scripts and a different URL
structure. There are currently a few thousand pages so I want to
basically move them in to a subdirectory so that they are not lost.
I am not very confident with htaccess so can someone please confirm that
the first part I have done is correct:
Old URL: http://www.example.com/article/another-dir/page-group/whatever.html
RewriteRule ^article/?$ http://www.example.com/archive/ [R=301,NC,L]
To achieve this: http://www.example.com/archive/another-dir/page-group/whatever.html
Search engines will see the above as a permanent move and in the address bar
it will show the new url. Is this right?
The second part is on the new script - the url's could be improved but I am
unable to change all the script so thought about using htaccess again but am
not sure if it can be achieved like this.
At the moment the url looks like this:
url: http://www.example.com/category/4/categoryname
In the htaccess the current rewrite rule for this type of url looks like this:
RewriteRule ^category/(.*)/(.*)$ category.php?id=$1&slug=$2
Is it possible to change this so that in the url address bar I end up
with this:
http://www.example.com/categoryname
Basically I don't want to see either the number or category/ in the resulting
url as it will be easier for visitors to use - is that possible??
Thanks in advance for any help.
The second question related to passing in URI components for querystring redirect and then hiding those components in the URL I don't think would be easy, if even possible, using RewriteRules.
For the first question though. Given the sample URLs you mentioned, the RewriteRule would need to include capture and backreference if you want to preserve the full URL in the redirection. For example:
RewriteRule ^article/?(.*)$ http://www.example.com/archive/$1 [R=301,NC,L]

How to correctly rewrite URLs?

I am a real noob in IIS URL rewrite module.
I want to rewrite all requests of
127.0.0.1/Content/[anything may come here]
to
127.0.0.1:7078/Content/[anything may come here]
I am very bad in regular expressions and I do not know how to do this
I tried using the wildcard feature in URL Rewrite module and did this:
Requested URL matches the pattern using wildcards
127.0.0.1/Scripts/*
then rewrite to
127.0.0.1:7078/Scripts/*
(Action Type is Rewrite)
I am attached a screenshot. I am not sure I am doing this the right way, because it is not working.
1/ You don't need the host part in the Match URL Pattern : /Scripts/* should work.
2/ If you want to match the wildcard in the Match URL Pattern to the Rewrite URL, you'll have to use something like {R:0} in the Rewrite URL. For example 127.0.0.1:7078/Scripts/{R:0}.
3/ I'm not sure you can rewrite to a different port. If rewrites does not work, try Redirect as Action Type instead.
If you want to hide port 7078, you might need to use Application Request Routing (Reverse Proxy).
Sources : http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module & http://forums.iis.net/t/1165389.aspx?URL+Rewrite+to+specific+port

Need URL Rewrite to replace a string in a url

I am looking and looking and coming up with all different patterns, but no such luck. I require a rewrite rule to handle the a replacement of a string xyzlisting with xyz-listing. It may be anywhere in the url. Is there a straightforward way to write this rule?
I am not so experienced with IIS. However, I can suggest something:
In Apache mod_rewrite, you can use this rule:
RewriteRule (.*)(/\w+)(listing)(.*)$ $1$2-$3$4
This rule will split whatever string in the format anything followed by 'listing' that comes after mydomain.com/ with a '-'. Which results in 'anything-listing', wherever in the URL.
This will only change the last occurrence of this type of string. It will not replace more than one occurrence.
You can import this URl rewrite rule from Apache to IIS using this IIS extension:
IIS Extenson page for url rewrite module
Hope this helps :)

htaccess rewrite regex for pagination urls

I need an expression that would rewrite (actually 301 redirect) old url patterns ended with -0.htm, to new ones -1.htm
so if old url was /path/to/pagination/script-0.htm
new url must be /path/to/pagination/script-1.htm
This must work for any number of folder/subfolder structure.
And must work only for [-0.htm]. Only for urls that end with this pattern.
Any idea on how to accomplsh this?
My htaccess regex knowledge is limited to none.
You could try something like:
RewriteRule ^(.*?)\-0\.htm$ $1-1.htm [R=301,L]
But make sure it's one of the first rules, before the ones that might do a pass-through if a real file.

Remove trailing ? [question mark] from url - empty query string

I am moving a website to a new Open Cart install and I have set up 301 redirects to keep any SEO value.
I had to use the following trick here - http://www.itsadam.co.uk/opencart-301-redirect-not-working-seo-fix-route/ - to work around an annoying issue with the way Open Cart handles url rewrites. So, I have this sort of thing in my .htaccess file currently:
redirect 301 /products-page /products?
However, this now leaves my rewritten urls with a trailing ? - an empty query string effectively:
http://www.mysite.com/products?
Is there a way I can use rewrites to match and ditch any superfluous question marks? (I need to retain any actual query strings).
I encountered the same issue in opencart. Rather than follow the link to your solution do it like this:
RewriteRule ^original-url$ new-url [L,R=301]

Resources