URL rewrite from trailing string to GET variable - url-rewriting

I've searched for this, but can't seem to come up with the right search terms to find - seems it would be a common question.
WIMP, IIS version is 8
I want to come up with a rewrite rule:
Incoming URL: http://example.com/1a2b3cdef
Rewritten to: http://example.com?p=1a2b3cdef
Note, the start of this url will always be "http://example.com/" (not the real domain name), that will never change.
From there my default document of index.php will retrieve the GET variable p.
Any help is greatly appreciated.

This is the configuration that can be applied in web.config.
<defaultDocument>
<files>
<add value="index.php" />
</files>
</defaultDocument>
<rewrite>
<rules>
<rule name="rewrite" enabled="true">
<match url="^[_0-9a-z]+" />
<action type="Rewrite" url="?p={R:0}" />
</rule>
</rules>
</rewrite>

Related

URL rewrite - odd url is produced

My first attempt at doing an URL rewrite.
Usual scenario. Domain name seperate from host.
So I've changed the name servers and domain points to holding page at host so that's ok.
And purchased a domain pointer from the host to allow for a domain to be pointed at a sub folder.
Added the following code to web config file in root folder:
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="xyz.uk" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?xyz.uk" />
</conditions>
<action type="Redirect" url="\xyz\ {R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I get "The page isn't redirecting properly" and my url reads as "http://www.xyz.uk/%5Cxyz%5Cxyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/xyz/"
Googled this and all the examples I've seen are the same as the one I'm using.
Where am I going wrong?
Thanks in advance.
The actual code that works is below.
TIP.............I was helped on another forum and even though I made the changes, the page still wouldn't redirect.
PROBLEM WAS..........the browser cache was remembering the old redirect url!!
CLEAR DOWN your browser cache and any changes can then be seen immediately. Happened in Chrome and Firefox.
Correct code:
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="xyz.uk" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?xyz.uk" />
<add input="{PATH_INFO}" pattern="^/xyz/" negate="true" />
</conditions>
<action type="Redirect" url="xyz/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

url-rewriting omitting directory from url?

Is it possible to remove a directory from URL? for example for the below urls:
http://localhost:50656/umbraco/Surface/HealthInsurance/Application?Pid=26665&Lid=73&Spid=23
http://localhost:50656/umbraco/Surface/HealthInsurance/Results/73
there need to remove umbraco/Surface/ and make it
http://localhost:50656/HealthInsurance/Application?Pid=26665&Lid=73&Spid=23
http://localhost:50656/HealthInsurance/Results/73
Please guide what will be regular expression for this.
In the web server section of your web config you can add the following. It will match the url .*, which is everything and will map it to /umbraco/Surface/{R:0}. The R:0 is the entire captured response. Make sure you have the module installed. Here is a tutorial on how to check if you have your rewrite set and you can test it. http://www.iis.net/learn/extensions/url-rewrite-module/testing-rewrite-rule-patterns
<system.webServer>
<rewrite>
<globalRules>
<rule name="MapUmbarco">
<match url=".*" />
<action type="Rewrite" url="/umbraco/Surface/{R:0}" />
</rule>
</globalRules>
</rewrite>
</system.webServer>

web.config rewrite rule in Child folder

I've a simple site with a web.config file for rewriting URL's
for instance, navigating to www.domain.com/story will show the page at www.domain.com/story.php
I've added a child folder with a copy of the site with a Chinese translation to www.domain.com/zh
I can access the story page at www.domain.com/zh/story.php but not www.domain.com/zh/story
How do I need to configure my web.config file? I've tried copying the web.config file to the /zh folder but this doesn't work, I've also tried copying and changing my match URL from ^story to ^zh/story but this also doesn't have the desired effect. Can someone suggest what is going on please?
Here is my existing web.config in the web root:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to story.php">
<match url="^story" />
<action type="Rewrite" url="story.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Haven't had a chance to try myself, but try something like this:
<rule name="Rewrite to story.php">
<match url="^zh/story" />
<action type="Rewrite" url="zh/story.php" />
</rule>

ASP.NET 4.0 Rewrite Rules in Global.aspx File Issues with rewrite

I have a rewrite rule I'm using in ASP.NET 4.0 on IIS7:
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite default to aspx" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="home.aspx" />
</rule>
</rules>
</rewrite>
<defaultDocument>
<files>
<add value="home.aspx" />
</files>
</defaultDocument>
</system.webServer>
This rule takes: (http:/example.com/aboutus.aspx) and it removes the .aspx from the end of the URL. I'm running into problems with wordpress being installed on my subdomain (http:/www.example.com/blog) I get the following error due to my rewrite rule:
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /blog/.aspx
Does anyone know how I can fix the URL rewrite rule so it safely get to (http://www.example.com/blog/) and not add the .aspx at the end?
Is there some sort of syntax that can search for the blog subdirectory to ignore the directory '/blog/'?
Greatly appreciated! Thank you! :)
If you want your rule to be applied to every urls but the /blog/* ones, then you can use the negate option:
<rule name="Rewrite default to aspx" stopProcessing="true">
<match url="^blog/" ignoreCase="false" negate="true" />
<action type="Rewrite" url="home.aspx" />
</rule>
http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#Rule_pattern_properties

ASP.NET Non existent URL files on server, use Rewrite?

How do you get filenotonserver.html to show up in a browser? and not 404 error. How can I do a URL rewrite to match .html to default2.aspx, or vice versa??
Do this make any sense? I am newbie on URL rewrite for sure...
k, been looking around:
<rewrite>
<rules>
<rule name="SpecificRedirect" stopProcessing="true">
<match url="^page$" />
<action type="Redirect" url="/page.html" />
</rule>
</rules>
</rewrite>
In the "match url" trying to match .html and action type would be default.aspx?p=whatevetmatched.html
Any ideas please?
ok so I got it:
<rewrite>
<rules>
<rule name="Redirect">
<match url="(.*)\.html$" />
<action type="Rewrite" url="default2.aspx?p={R:1}" />
</rule>
</rules>
</rewrite>
And on the default2.aspx reads the request("p") which has the "matched" html name and I then look it up in the database to display the data for that matched html "page"

Resources