In a previous post, I mentioned that I was investigating a way to use real permalinks without using any of the ISAPI mod_rewrite dlls. For the uninitiated, mod_rewrite is not natively supported in IIS (Window’s server), and to get the same functionality, you have to install separate programs. I’ve done that and that is how this blog has survived on a Windows server for the past year or so.
I was convinced that needn’t be the case though. There must be a way to have permalinks without having to install a whole separate mod_rewrite program (or extension). I detailed my plan in this post.
Well, I finally got around to it – and the plan worked!
Here’s the gist of it. It uses custom 404 redirect to send all “Page Not Found” errors to a script that reads a bunch of regular expressions (don’t worry, you don’t have learn regular expressions to use this) and figures out what page to feed back.
Now, some people may find my solution a bit odd considering it is written in ASP. You used ASP to extend wordpress?? Yes, and it worked :).
More detailed instructions, and a link to the needed files are located here:
Nick Dunn says
Firstly, many thanks for documenting your process. I have just written a similar custom 404 page, but slightly less elegantly than yours. One problem. There’s a textbox in the Admin panel with any rewrite rules in it. Your instructions say it should be beneath the “Update permalink structure” button, but there’s nothing.
Is this a particular plugin/option I need to enable? I have set $use_verbose_rules = true.
Michael Walsh says
This doesn’t seem to be working for me, I can’t see a textarea box with all of the ReWriteRules? I’m runnning WordPress 2.0.2
I’m setting this up in a subdirectory if that makes any difference, so it’s http://www.example.com/blog/
Tom says
I ran into the same thing when I was trying to install it on WordPress 2.0.2. In this new version, it doesn’t show the ReWrite rules when you have the “Common Options” set to anything but “Custom”. Select “Custom”, enter in a permalink structure and it the box with the rules in it should show up.
Mine is:
/archives/%year%/%monthnum%/%day%/%postname%/
Tom says
The sub directory may make a difference. I haven’t tested it out in that situation yet. You may have to modify the rewrite rules to add your sub directory in front of them like so:
Before:
RewriteRule ^archives/author/([^/]+)/(feed|rdf|rss|rss2|atom)/?$ /index.php?author_name=$1&feed=$2 [QSA,L]
RewriteRule ^archives/author/([^/]+)/page/?([0-9]{1,})/?$ /index.php?author_name=$1&paged=$2 [QSA,L]
RewriteRule ^archives/author/([^/]+)/?$ /index.php?author_name=$1 [QSA,L]
RewriteRule ^archives/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$ /index.php?year=$1&monthnum=$2&day=$3&feed=$4 [QSA,L]
After
RewriteRule ^yoursubdirectory/archives/author/([^/]+)/(feed|rdf|rss|rss2|atom)/?$ yoursubdirectory/index.php?author_name=$1&feed=$2 [QSA,L]
RewriteRule ^yoursubdirectory/archives/author/([^/]+)/page/?([0-9]{1,})/?$ /index.php?author_name=$1&paged=$2 [QSA,L]
RewriteRule ^archives/author/([^/]+)/?$ yoursubdirectory/index.php?author_name=$1 [QSA,L]
Nick Dunn says
Cool, it’s working. Ish.
To get the rewrite rules up, I had to edit the “Blog address (URI)” and add a subdirectory, e.g. “myblog.com” to “myblog.com/test/”. The box then showed up on the Permalinks page. I then did a find/replace, removing all “/test” strings, and put my “Blog address (URI)” back to the original URL.
It all seems to be working except for actually viewing a post itself. For example, my permalist structure is:
/%year%/%monthnum%/%postname%/
When viewing a post page itself, a quick debug of the 404 handler shows the following string for the URL variable:
/index.php?year=[yyy]&monthnum=[mm][post-title]
i.e. the post title and month are concatenated.
I’m trying to decipher the list of rewrite rules, but finding it difficult to debug.
Tom says
Can you give me an example of a ReWrite rule you are using?
Nick Dunn says
Sure. I *think* this might be the offending rule. Taken straight from the WP output.
RewriteRule ^([0-9]{4})/([0-9]{1,2})/?$ /index.php?year=$1&monthnum=$2 [QSA,L]
Tom says
The rule you gave me is missing the “PostName” variable. Originally, I was figuring we wouldn’t have to decipher regular expressions to get it working.
From your previous post, it looks like it’s missing this string here: &name=[post-title].
You wrote that it is resolving to: /index.php?year=[yyy]&monthnum=[mm][post-title]
I would try starting over (rather than trying to debug the regular expressions). Just try changing the permalink structure (make sure it’s set to custom), and re uploading the htaccess.txt.
Nick Dunn says
Yep, it’s missing name=[title] but I can’t for life of me see why that rule isn’t being written by WP, especially when it’s in my permalink structure. Never mind, it’s not too tricky to modify the expressions manually to include this.
I shouldn’t be wasting time on this at the moment (Uni finals tomorrow!) so will revisit this discussion in a few weeks when I have more time.
Once again, thanks for the well thought out script, and taking the time to help π
Tom says
No problem. Best of luck on the finals.
Oman says
I tried installing it, and it didnβt work. I am running on a Linux server so I donβt know if that had and effect. If you can help me fix this problem I would greatly appreciate it.
Tom says
Oman – The modification was designed for use on a Windows server. IIS (Internet Information Services) is Microsoft’s web server software. It also relies on ASP, a programming language specific to Windows.
With Linux, you shouldn’t even need this though. You should be able to just copy/paste the rewrite rules into your .htaccess file and it should work.
Larry says
Started getting WP setup and customized late last week on IIS 5. So far, I’m happy with my progress. I stumbled upon your site while trying to figure out my 404 issues. WP is installed on the root of my site.
When trying to test my 404 it seems as though I’m being redirected to WP’s index.php.
Any thoughts or suggestions?
Tom says
Larry – What do you mean by testing your 404?
Are you trying to set up a custom error page in addition to this solution? If so, you need to edit a variable in the file errorpage.asp.
At the very top of the file it says:
Actual404 = "/"
You would change that to be whatever your real 404 page is – for instance, you could have it redirect to “/404Error.html”
Chad says
I’m pretty new to this stuff, and I’m having some trouble with this little redirect bugger. I have followed your instructions but I’m still getting a page that says “The system cannot find the file specified.”
The only way my permalink structure works right is when I’m set to: /blog/?p=123.
Don West says
Hi,
Great solution! Many many thanks for developing it!
I’ve just installed the iis rewrite and it seems to work fine with one exception.
If you go to my site and click the “quick sketches” category, as expected you’ll see the pretty permalink in the url:
http://www.idleminutes.com/archives/category/quick-sketches/
However, if you then scroll down to the end of the last post and click the “older posts” navigation link, the URL changes as follows:
http://www.idleminutes.com/index.php?category_name=quick-sketches/&paged=2
Is that the correct behavior?
Each individual post’s permalink is still a pretty permalink. I’m just wondering if the URL should be as well.
Thanks!
Tom says
First off, I thought I should note that Chad figured out the problem with his script – it had to do with where php was installed on the system. The system cannot find the file specified is a php error, not a wordpress error.
Secondly, Don – Thanks for pointing that out. It does the same thing on my blog unfortunately. I think it has to do with the way the script gets the information to display (it actually acts like a web browser, fetches the page using the index.php?category_name=…, and then displays it under the permalink structure).
This is just me guessing, but I think that because we get the page that way, wordpress thinks we’re not using ther permalink structure and would therefore like our links to be the standard.
You may be able to change this by editing your template, but I can’t help you much there.
So I guess the short answer to your questions is – sorry! π
If you figure out a solution let me know and I’ll include it in the script.
Don says
Thanks Tom,
When you say I might be able to edit my template, what part of the template would I would investigate?
Don says
Tom,
One other thing, if you click any archive post, rather than categories, you’ll get a horizontal nav link at the top of the post.
Those nav links display the proper URL (rewritten).
Any insight on why that works and the other ones don’t?
Tom says
I looked at my own template and this is what I found in archive.php:
<div class="navigation">
<div class="alignleft"><?php posts_nav_link('','','« Previous Entries') ?></div>
<div class="alignright"><?php posts_nav_link('','Next Entries »','') ?></div>
</div>
That led me to think that, although you could fix it in the template area, you may want to fix it in the function itself. The
posts_nav_link()
function is located in wp-includes/template-functions-links.php.I don’t have the time at the moment to go through it fully myself, but if you find anything, let me know.
Thanks!
Tom
Tom says
I think that has to do with the function I mentioned. It may be that the different pages you are referring to are using different functions for their nav_links, though. Take a look at the template-functions-links.php, it may give you some insight.
Darryl says
The instructions in your “WordPress Permalinks in IIS” are great, but I’m just trying to figure out how to apply them to the control panel at my hosting site. The only thing that comes close is an option to Redirect URL, with the following options (_____________ indicates a text box for me to fill in):
1. Redirect from http://www.example.com/_________________
2. To http:// or ftp:// or https:// _________________
3. The client will be sent to (select one from below):
– The exact URL entered above
– A directory below this one
– A permanent redirection for this resource
Any thoughts on what I fill in for 1 and 2, and which option I select for 3?
Any assistance would be greatly appreciated π
Tom says
Hey Darryl,
First off, I took a look at your blog – You’ve got a great design there. Nice use of AJAX too.
As far as your question goes, I don’t think you’re in the right place. You want access to a “Custom 404 Error Page”. The panel you described has to do with redirecting to another site or URL on your site.
Contact your host and see if they support custom error pages. They should be able to. If they don’t have it in their control panel, you can call them and give them the instructions to set the custom 404 error page to “/errorpage.asp” and make sure it’s set to URL, not FILE.
Darryl says
Tom,
Thanks for that. You’re right – I was very much looking in the wrong place. There’s an option in the Control Panel for “Error”. I went in there, set the 404 page to be redirected to /errorpage.asp, and everything worked beautifully.
It was such a relief. I have been trying so many things over the past couple of days, and this is the only spot I found this particular solution of yours. You’re a champ, Tom!
Dcrad says
@Darryl – it sounds like you have the same control panel as me. I’ve been unable to get it working, you say you set 404to be redirected to /errorpage.asp – my hosting doesnt allow me to do that as it seems to not like the “/” – it allows me to use just “errorpage.asp” – but I just a standard 404 type message when I click on my permalinks.
Any ideas?
Dcrad says
Sorry thought I better clarify things a bit in relation to my last post….
I get “HTTP/1.1 404 Object not found” when clicking my permalinks. But what i notice is that regardless of the url given for the errorpage.asp I always get this. I’ve even tried setting my index.php as the 404 page but always get this problem.
Any ideas?
dave sweet says
When I enable this it works as advertised, but it substitutes ? for spaces throughout the text of my entries. If I go back to a default permalink structure, the ?’s go away. Any thoughts on why this would happen?
Tom says
Dave –
I looked at another site that I have this solution installed on and I found that it was doing the same thing as yours (putting ?s in for spaces)
It has to do with the Charset the browser is using to interpret the page. That setting is being wiped out in the transition.
We can, however, set it manually within the page itself. In errorpage.asp add the following line:
Response.Charset = "ISO-8859-1"
This should be entered right before these two lines:
Response.Write ReturnPage(Path)
Response.End()
This fixed it for me. I am debating whether to put it in the original download. My thought is that not everyone will be using that Charset.
Matthew says
Works great! The only hickup I had was that I had a htm 404 page before and this was cached in my browser which seemed all wasnt working. A CTRL+F5 fixed the issue π
Nice work!
modernape says
Hiya,
I installed errorpage.asp and htacess.php, on the same directory that wp is on, a windows server, but I get this error message in my browser window when I click on a permalink, even though it looks fine in the status bar. –
”
Microsoft VBScript runtime error ‘800a139d’
Invalid range in character set
/alcohol-drugs/errorpage.asp, line 84 ”
I’ve set up the 404 redirect as per instructions too, so I’m not sure what else to do?
Thanks very much for your help
Modernape π
Tom says
Modernape – what is your site?
modernape says
Hi Tom, my site is –
http://www.brighteyecounselling.co.uk/alcohol-drugs/
(the alcohol-drugs directory is WP), but as it wasn’t working I’ve reset the 404 page and the WP permalink format back to normal!
Any ideas what was wrong? Or do I need to set it all up again?
Thanks
Tom says
I’m not sure – what was your permalink structure?
modernape says
I had permalinks exactly as you suggested.
Chris says
I’ve made the changes and everything is working… except for the actual archive page. The form for the comments is not displayed at all.
However, if we change the permalink to something else, say, /index.php/%year%/%monthnum%/%day%/%postname%/ — then the comments form does appear.
As soon as I remove “/index.php”, again, the comments form disappears. Any idea what may be wrong?
Tom says
Chris – I had a similar problem recently on this site. I didn\’t have the permalink structure of /index.php/…, but the actual post pages weren\’t working. They were directing to archive pages – for instance the following link would point to the archive page for May 21st.
http://www.keyboardface.com/archives/2006/05/21/wordpress-permalinks-in-iis-without-the-mod_rewrite/
It was, in effect, ignoring the last part of the url. This was because of the order the script parsed the rules. I put out a new version of the script as of a couple of weeks ago. You may try getting the most recent version of errorpage.asp.
If you have the most recent version, I\’m not sure what to say. Maybe, try and take the index.php out of the permalink structure?
If you\’re determined to keep the index.php in the structure, you can try this other solution:
http://blog.taragana.com/index.php/archive/wordpress-tip-on-permalink-options/
FA says
I’ve been trying to get this solution to work as it seems perfect. However, after following the instructions, all I get is a hung site. The home page loads, but articles don’t regardless of what permalink structure I choose. The browser just sits there with the status indicator spinning at the top-right. Accessing htaccess.php directly also hangs.
Any ideas?
Tom says
It would seem that the problem is coming from htaccess.php then. You can try switching it from using htaccess.php to htaccess.txt. Look at the very last section of the instructions page, it should help.
FA says
Tom,
I tried switching to htaccess.txt and that didn’t work. However, I did find some info that fixed my problem. I needed to create a php.ini file and included these lines:
cgi.fix_pathinfo = 1
cgi.force_redirect = 0
My PHP skills are a little weak so I don’t know exactly what the above does, but it came from the WordPress Codex and it works. I now have rewrites working.
My next problem is now when a user posts comments, they are always directed to the home page rather than to the post they replied to. I’m pretty sure this has to do with the threaded comments or subscribe to comments plug-ins I use.
Shell says
I have followed your instructions to the letter and get this message on every page but the home page: The system cannot find the file specified.
Everything is in the root folder and I have added the php.ini file as instructed. Not sure what else to try.
Tom says
Shell –
It most likely is one of two things:
1. php.ini is located in the wrong place.
2. there is something wrong with the regular expressions in htaccess.php or htaccess.txt. To debug it, I’d set the script to use htaccess.txt and then play around with the regular expressions to see which ones it was using.
Stefan says
Hi Tom,
i tried a lot but it doesnt work. When i use the php.ini file all works fine with the /index.php in the url but without /index.php it will not works. what could it be? something i have write down to the htaccess files?
thanks a lot.
Stefan
Tom says
Thanks to Stefan. He pointed out a good alternative to the solution posted here. Here it is:
http://www.simmonsconsulting.com/wordpress/?p=204
Mike says
Works great – thanks!
Julio says
I’m having troubles with international characters.
Here is an example:
http://www.juliozuniga.com/blog/archives/2006/08/07/hola-mundo/
What can i do?
Thanks in advance
Tom says
Julio –
It has to do with the character set the ASP page sends to the browser. You can try changing the character set to be: “ISO-8859-1”. If that doesn’t work, try other character sets until you get the result you’re looking for. Here’s how to do it:
In errorpage.asp add the following line:
Response.Charset = "ISO-8859-1"
This should be entered right before these two lines:
Response.Write ReturnPage(Path)
Response.End()
Sara says
I’m working on a site for a client, that is installed on an IIS server.
I installed errorpage.asp and htaccess.php into my wordpress folder, and changed the setting for the 404 page for my server to wp/errorpage.asp .
I set my permalinks to /%postname%/ and didn’t set any permaliniking rules for a “category base”.
Unfortunately, when I try to click on or enter a permalink, rather than going to the page I’ve chosen, my browser pops up a screen telling me I’m trying to access a binary file and asking me what I want to do with it. (there are no auto-generated permalinks on the front page of the site, but if you click on any of the links at the top of the screen, and then click on any of the sidebar links on any of those inside pages, you’ll see the behavior I’m talking about).
I tried replacing htaccess.php with htaccess.txt, which did not change the situation.
I also tried several different custom and standard permalink options, all to no avail — the problem stays the same each time.
Any advice? Thanks very much!
-Sara
Antoine Khater says
First let me thank you for the great tip, I really like it, Now i am facing 2 problems and I’d really apreciate your help
Please open http://www.adidap.com and http://www.adidap.com/page/2 in 2 different browser sesssions
1) The Β© in main page is looking well, in page 2 it is showing as a ? (this was solved by adding Response.Charset = “ISO-8859-1”
2) The layout in page 2 is shifted π the sidebar is not at its correct place
thanks