IIS URL rewrite for multiple slashes - .htaccess

I am having problem to rewrite a url that may or may not include a secondary pattern.
This is a rewrite mod for a login system.
I want to rewrite
"contoso.com/register/1/"
and
"contoso.com/register/2/{key}"
However, I found that if I have
^register/([1-2]+).*
I am not going to get the key for step 2 of the registration.
And if I use
^register/([1-2]+)/([_0-9a-zA-Z-]+).*
for my mask. It is not going to rewrite "contoso.com/register/1/" .
Will it be possible to rewrite in just one rule? Or I must have two separate rules for this scenario?
It will be good to help with the IIS presentation. But if you know the method on .htaccess. I am also able to convert myself. Many thanks!

I have done some research on Regex and found that a /* mask will make the rule work while it excludes "/" being detected.
The mask I am having now is
^register/([1-2]+)/*([_0-9a-zA-Z-]+)
Which will take step 1 as {R:1} and key after slash as {R:2}

Related

Rewrite rules for making multiple paths work

I have a requirement to make the following paths work.
Depending on what the url consists of, they are mapped to go to different java classes.
/books/
/books/science/
/books/science/fiction/
/books/science/fiction/kids/
So, I have given the rewrite rules in my configuration file as:
^/books$
^/books/(.*)$
^/books/(.*)/(.*)$
^/books/(.*)/(.*)/(.*)$
but the moment I give a url something like this
http://localhost/books/science/fiction/kids/12345
instead of getting captured by the fourth rewrite rule, it is captured by the second one which is not what I want.
Can someone please tell me how to achieve this? Thanks in advance
^/books$ /webapp/wcs/stores/servlet/ABCController?resultsFor=allCategories [PT,QSA]
^/books/(.*)$ /webapp/wcs/stores/servlet/XYZController?make=$1&resultsFor=category [PT,QSA]
^/books/(.*)/(.*)$ /webapp/wcs/stores/servlet/ABCDController?format=$1-$2&resultsFor=subCategory [PT,QSA]
^/books/(.*)/(.*)/(.*)$ /webapp/wcs/stores/servlet/ASDFController?resultsFor=product [PT,QSA]
instead of getting captured by the fourth rewrite rule, it is captured by the second one
That’s because the dot matches any character, so slashes as well.
Replacing it by a character class allowing anything but a slash (and demanding at least one character out of that class, so + instead of *) should fix that: ([^/]+)
Another way would be to reverse the order of your rules … You should always try and write them in order from most to least specific anyway.

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 with optional parameters

I have little understanding of HTACCESS, even though I have spent a long time looking into it. I think the real reason is because regular expressions go above and beyond my capacity.
Currently, in HTACCESS, I have use this line:
RewriteRule ^website/([^/]+)(/([^/]+))?$ controller/url.php?method=$1&param=$3
This will allow users to have a nice url that looks like www.website.com/website/method/optional_parameter
How could I add a second and third optional parameter to the HTACCESS rewrite line?
The easiest way would be to allow the second group to contain the rest of the URL after your "method" group.
RewriteRule ^website\/([^/]+)\/(.*)$ controller/url.php?method=$1&params=$2
Then use PHP to split params at each /
Hope that helps.

help regarding dynamic redirect rule in htaccess

I need ur help for given subject.
I am playing with htaccess rules first time in life.
here is the scene -
i want to redirect the urls -
http://www.abc.com/var1
http://www.abc.com/var2
to follwing urls -
http://www.abc.com/index.php?u=var1
http://www.abc.com/index.php?u=var2
In this case the values var1 & var2 can be anything (i.e. string which will contain only alphanumeric characters.)
One more thing -
I want to skip this rule if the url is -
http://www.abc.com/SKIPME
Please help me to write this rule!
Regards,
Shahu!
You would be better off defining a URI schema that tells you when something WILL be rewritten. For example...
http://www.abc.com/site/var1
http://www.abc.com/site/var2
That way, you can ensure you only ever apply the rule if the psuedo "site/" directory is browsed and not affect any other URI. This is the rewrite rule based on the above schema.
RewriteEngine on
RewriteRule ^site/([^/\.]+)/?$ index.php?u=$1 [L,NC,QSA]
Any other address, other than "/site/.../" would be unaffected by this rule, which means you don't need to worry about setting some addresses to be avoided. This keeps things as simple as possible.
You don't have to use "site" - you can use whatever name fits your purpose.

Avoiding sub-directory request rewrites with Apache mod_rewrite

I want to a rewrite rule such that if a user goes to the URL example.org/stuff/junk.jpg the rule will process and end up at re-writer.php but if the user goes to example.org/stuff/hackingisawesome/junk.jpg the rule will not be triggered and they will get a standard 404 (or a page, if one should exist).
I can't tell, based on the environmental variables, if this is possible without some fairly fancy regex.
So does anyone know of either:
a) a way this is already built into the mod_rewrite syntax, or
b) a good, reliable way of handling this with regular expressions?
Links to documentation or tutorials welcome. I'm just feeling clueless on where to go next.
Oh, and I can imagine the ways I could simply have the script that the rule redirects to simply deliver the 404, but I'd rather only use the rule when the conditions exist.
Try this:
RewriteRule ^stuff/[^/]+$ re-writer.php
This will rewrite all requests to /stuff/… with only one additional path segment to re-writer.php.

Resources