I have a domain that is hosted by discountasp.net (a standard .net web host). Call it http://example.com.
The domain hosts a default website, plus there are a couple separate web applications.
The website was created in Wordpress, and it lives in a /wordpress subdirectory of the domain's root directory on the host.
I want to make it so that there is no /wordpress in the URL the user sees in the browser. Previously, there was an index.html file in the root directory that would redirect requests for http://example.com/ to http://example.com/wordpress/. But that of course is just a redirect and leaves http://example.com/wordpress/ in the browser URL bar.
I thought I could achieve what I wanted with URL rewrite rules in my Web.config file in the root directory. Below is the text of that file.
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="wordpress redirect a"> <match url="wordpress/(.*)"/> <action type="Rewrite" url="{R:1}"/> </rule> <rule name="wordpress redirect b"> <match url=".*"/> <conditions> <add input="{REQUEST_URI}" pattern=".*nm.*" matchType="Pattern" negate="true" ignoreCase="false"/> <add input="{REQUEST_URI}" pattern=".*survey.*" matchType="Pattern" negate="true" ignoreCase="false"/> </conditions> <action type="Rewrite" url="wordpress/{R:0}"/> </rule> </rules> </rewrite> </system.webServer> </configuration>
The logic of the file is:
The first rule: if the url path (the part after the host:port/) starts with
wordpress/
, it removes thewordpress/
and continues processing rules with just the part that came after thewordpress/
.The second rule: for any url path that does not contains the special web application strings "nm" or "survey", put
wordpress/
in front on the path.
This works, in the sense that if I put http://example.com/ into the browser bar, it shows the website correctly, and also this rewriting does not interfere with the web application calls such as http://example.com/nm/...
However, it still leaves the /wordpress in the URL the user sees (i.e., when I put http://example.com/ in the browser bar, I end up with http://example.com/wordpress/ in the browser bar after whatever rewrite/redirect it does. It may be that I'm just misunderstanding what rewrite rules do. But my understanding (and what I was trying to achieve with these rules) was that any incoming URL (except my special web application URLs) that does not include wordpress/
would be handled internally as if it did include /wordpress
, but then returned to the client as the original URL not including /wordpress
. So, not a redirect, just some internal fakery. And then the first rule is there because the links within the website would include /wordpress
, and since I don't want to get /wordpress/wordpress
, I need to strip it before adding it.
But obviously I'm misunderstanding something.
Another clue is that the second rule doesn't even seem to be doing its job when I use the url http://example.com/about/. The second rule should make this operate as if it were http://example.com/wordpress/about/, but instead I get a 404 not found error. But if I put http://example.com/wordpress/about/ in the browser bar, it shows that web page correctly. So I'm confused.