Rewrite rule to modify dynamic url's - dynamic

I am trying to create a rewrite rule to modify a dynamic url.
The reference (nie1000857) at the end of the url is the only dynamic part.
"fulldetails?" needs to be inserted as shown below.
https://www.estateagent.net/property-to-rent/nie1000857
https://www.estateagent.net/**fulldetails?**property-to-rent/nie1000857
So far I have:
<rule name="Add fulldetails?" stopProcessing="true">
<match url="^property-to-rent(.?)" />
<action type="Rewrite" url="fulldetails?property-to-rent/"/>
</rule>
However I can't work out how to extract the dynamic part of the url(nie1000857) and add it to the end of the rewrite url.
Any help/advise would be greatly appreciated and apologies if I have missed something obvious within Microsoft's documentation.
I am a total beginner with creating rewrite rules.
Thank you.

I worked it out.
Thought Id post the answer for anyone trying to achieve the same.
<rule name="Add fulldetails?" stopProcessing="true">
<match url="^property-to-rent/(.+)" />
<action type="Rewrite" url="fulldetails?property-to-rent/{R:1}"/>
</rule>

Related

Unable to convert IIS web.config URL Friendly to .htaccess mod_rewrite

I'm migrating a web application from a Windows (IIS) server to a Linux (Apache) and I have been a few days trying to adapt the application, configs, etc. for Linux/Apache.
Everyting is working except the url friendly I was using with Windows/IIS. And I don't know if this is actually a URL Friendly, a simple redirect or anything else.
This is what I want:
I have a 'app.php' file which is like the main application 'index', and shows different 'pages' depending on the old/ugly url parameters:
https://example.com/app.php?qpage=5&qsubpage=2&action=new
Instead of writting this, I want to use friendly urls like this:
https://example.com/section5/?qsubpage=2&action=new
(Please notice that the second and third parameters 'qsubpage' and 'action' are optional!)
Ok, with IIS and this lines in web.config, it works fine:
<rewrite>
<rules>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="app.php?qpage={R:1}" />
</rule>
</rules>
</rewrite>
Then, in 'app.php' file I get the current URI and (optional) parameters to show one content or another.
However, I can't reach to get it working with .htaccess.
This is what I have know. It works fine only if there is just one parameter:
If I go to...
https://example.com/section5
...it shows the correct 'page' correctly.
However, if I go to...
https://example.com/section5/?qsubpage=2
or
https://example.com/section5?qsubpage=2
...'qsubpage' is ignored.
If I go to
https://example.com/section5&qsubpage=2
...I get a '404: url not found' error (however it works in Windows)
This is my .htaccess rule:
RewriteEngine On
RewriteRule ^([A-Z-a-z-0-9]+)/?$ simplygest.php?qpage=$1&qsubpage=$2 [L]
I have tried some online mod_rewrite generators and even web.config to .htaccess converters online with no success.
Thanks!

IIS Rewrite very confusing

concerning IIS Rewrites.
I want to change this url on my local IIS Instance
http://localhost/MySite/health?key=BczI5MyulpRLxI2kiJmIXwLOm78r3qr8z2gwcsYTGR4=&c
to redirect to this url:
http://localhost/MySite/Health.svc/BczI5MyulpRLxI2kiJmIXwLOm78r3qr8z2gwcsYTGR4=/c
As you can see I don't want the incoming request to use
Health.svc/BczI5MyulpRLxI2kiJmIXwLOm78r3qr8z2gwcsYTGR4=/c
instead to use
health?key=BczI5MyulpRLxI2kiJmIXwLOm78r3qr8z2gwcsYTGR4=&c
The Health.svc is the WCF endpoint name, so I just want /health with the key and filter parameter at the end as shown.
Whatever I put in my web config rewrite it still doesn't work. I am rather confused what bit of the url to put in, as the regex seems to be valid as I can test it in IIS and online regex validators.
<rewrite>
<rules>
<rule name="HealthRewrite" stopProcessing="true" enabled="true">
<match url="MySite\/health\?key=([0-9a-zA-Z=]+)&([a-z])" />
<action type="Rewrite" url="MySite/Health.svc/{R:1}/{R:2}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
How can I get this to work? I have got the rewrite module installed as can see it in IIS an also can see the dll is registered.
If you want to match the value of the query string in IIS, you need to use {QUERY_STRING}. Here is a demo:
<rewrite>
<rules>
<rule name="Test">
<match url="(Service1)" />
<conditions>
<add input="{QUERY_STRING}" pattern="(key)=(.*)" />
</conditions>
<action type="Rewrite" url="Service1.svc/{C:2}" />
</rule>
</rules>
</rewrite>
This is my web.config.
This URL:
http://localhost/Service1?key=getdata
will redirect to this url:
http://localhost/Service1.svc/GetData
For your last question, why add the MySite prefix? This is because the URL in the Rewrite URL will be used as the redirect URL. Notice that it uses back-references to preserve and rearrange the original URL pieces captured during pattern match. For Rewrite, all prefixes other than localhost must be provided in the Rewrite URL.
I managed to get it working with going to all sorts of sites as its not obvious at all.
They key seemed to put the conditions in and then a {QUERY_STRING} with regex which can then create the {C:1} and {C:2} groups that are pushed into the new rewrite
<rewrite>
<rules>
<rule name="HealthRewrite" stopProcessing="true" enabled="true">
<match url="^health" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{QUERY_STRING}" pattern="key=([0-9a-zA-Z=]+)&([a-z])" />
</conditions>
<action type="Rewrite" url="/MySite/Health.svc/{C:1}/{C:2}" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
I found it confusing knowing what url to match but with a few simplified tests of just ^health I could see more easily and play around with getting the query string parameters. I had to provide the Rewrite with the /MySite/ prefix which is confusing as the match didn't need that!

Sitefinity Blog URL Format

I am looking for guidance on an issue trying to change the blog url format to make the categories SEO friendly.
ISSUE
I am trying to have our blog categories URL changed from
https://example.com/blog/-in-category/categories/automotive
to
https://example.com/blog/automotive
Format: [domain]/[blogname]/[category]
I’ve added a custom blog provider and can access the categories with the above format, however, the hierarchical widget still shows the original url.
I did add an outbound rewrite rule that did update the widgets URLs to the correct format, however, it killed the Sitefinity backend (can’t access Pages, Blog Post Content). Scriptresource.axd and Webresource.axd through a 404.
Here is the out bound rule..
<outboundRules>
<rule name="Cat Rewrite Rule">
<match filterByTags="A" pattern="/blog/-in-category/categories/([^$]+)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern="\.axd" negate="true" />
</conditions>
<action type="Rewrite" value="/blog/{R:1}" />
</rule>
<preConditions>
<preCondition name="IsHtml">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html"/>
</preCondition>
</preConditions>
</outboundRules>
Errors from backend when trying to access the Pages:
Will a custom blog taxonomy evaluator solve what I am needing to accomplish (which I’m not sure how to do)?
Thanks for your help!
In your case, problem is that you forgot to use precondition. Working example is:
<outboundRules>
<rule name="Cat Rewrite Rule" preCondition="IsHtml">
<match filterByTags="A" pattern="/blog/-in-category/categories/([^$]+)" />
<action type="Rewrite" value="/blog/{R:1}" />
</rule>
<preConditions>
<preCondition name="IsHtml">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html"/>
</preCondition>
</preConditions>
</outboundRules>
P.S. But I am totally agree with #Veselin Vasilev, that cleaner approach is to build custom widget.
You can find source code of built-in widgets here:
Blogs: https://github.com/Sitefinity/feather-widgets/tree/master/Telerik.Sitefinity.Frontend.Blogs
Taxonomies: https://github.com/Sitefinity/feather-widgets/tree/master/Telerik.Sitefinity.Frontend.Taxonomies
I would probably create 2 custom MVC widgets that will handle this scenario:
First would be one that gets all categories and renders the links using the format you want, e.g. BlogCategories widget.
It will just generate a list of categories with links of the likes of "/blog/[category]"
Second widget would be a blog list controller that will have the category as a parameter and will get all blog posts that have that category.
It is a bit of work, but much cleaner than having url rewrite rules I think.

Converting web.confg into .htaccess

I am trying to convert from using web.config on IIS to .htaccess on apache.
Although, i've got everything working, but got stuck on one thing.
<rule name="Imported Rule 2">
<match url="^blog/([a-zA-Z0-9_-]+)(|/)$" ignoreCase="false" />
<action type="Rewrite" url="index.php?url=blog&id={R:1}" appendQueryString="false" />
</rule>
I am trying to convert the above code into .htaccess.
If anybody could help, it would be gratefully appreciated.
Try to add this config to .htaccess
RewriteEngine on
RewriteRule ^blog/([a-zA-Z0-9_-]+)(|/)$ index.php?url=blog&id=$1

IIS URL Rewrite rule not being triggered

I am pretty sure that I have my WCF URL Rewrite Rule coded correctly in my Web.config file:
<rewrite>
<rules>
<rule name="RemoveSVC" stopProcessing="true">
<match url="MyWebSvc/([a-zA-Z]+)-svc/(.*)" />
<action type="Rewrite" url="MyWebSvc/{R:1}.svc/{R:2}" appendQueryString="true" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
The rewrite rule seems to be working in the "Test pattern..." widget. For instance:
http://localhost/MyWebSvc/thinga-svc/majigs
gets correctly mapped to:
{R-1} thinga
{R-2} majigs
And I would like the URL to be re-written to:
http://localhost/MyWebSvc/thinga.svc/majigs
Seems simple enough, however, when I try to test the URL from a browser:
http://localhost/MyWebSvc/thinga.svc/majigs
works, but
http://localhost/MyWebSvc/thinga-svc/majigs
does not appear to be re-written, and I get a 404 response code. Do I have to configure the rule to be triggered with some other setting?