Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2006-01-31 16:57:28

ianseckington
New Member
Registered: 2005-03-13
Posts: 9

IIS and non-messy URLs via 404 error pages.

I’m just posting this for those of you that would like to start using permanent link modes other than ‘?=messy’ if you are running under IIS.

Once you have installed and got TextPattern working with PHP and MySQL, you will need to go onto the server and bring up the IIS management console: right-click ‘My Computer’, select ‘Manage’. In the console, select ‘Services and Applications’, then ‘Internet Information Service’. My TXP install runs at the root level, so I needed to right-click on ‘Default Web Site’ — you will need to find where your TXP index.php lives and right-click on that. Bring up the Properties dialog.

Select the ‘Custom Errors’ tab and find the entry ‘404’. Select this entry and press ‘Edit’. The Message Type needs to be URL and the URL should be the path to your index.php — as my TXP runs at the lowest level, I entered ‘/index.php’ (if your TXP runs from \inetpub\wwwroot\fredstxp, then the URL should be ‘/fredstxp/index.php’).

OK and apply all the changes and then change your TextPattern permanent link mode to whatever you want. IIS generates a 404 error when it can’t find /2006/01/31/an-entry and redirects to index.php which performs its sweet, sweet magic.

Ironically, this is exactly what I use in the C#/ASP.Net stuff I’ve been building for my real job. Oh how I laughed.

As a disclaimer, this has been tested under IIS 6.0 running on a Windows 2003 server box with PHP Version 5.1.2 and MySQL client API version 5.0.18. I also have full admin rights to the machine.

Hope this helps!

Last edited by Sencer (2006-04-12 09:40:43)

Offline

#2 2006-01-31 17:04:53

Sencer
Archived Developer
From: cgn, de
Registered: 2004-03-23
Posts: 1,803
Website

Re: IIS and non-messy URLs via 404 error pages.

Does posting comments work out of the box ith that appraoch?
(It works with lighttpd, for example, but not with Apache, which is why I added an additional “fix” as described in this weblog article )

Offline

#3 2006-01-31 17:19:32

ianseckington
New Member
Registered: 2005-03-13
Posts: 9

Re: IIS and non-messy URLs via 404 error pages.

Ha ha! Of course not (facepalm moment — it has been a really long day)!

Now what I need to do is try to get the install back to a base level (one of my coworkers put a whole new set of templates in, so I don’t want to end up ‘fixing’ anything that may not be wrong in a clean TXP install).

I’m about to leave the office shortly, but I shall have a look tomorrow and report back — as I’ve said I am strictly a .Net hack, but I know enough php to get my hands dirty. I’ll also try out the additional fix in your article. Will report back here when I know more.

Offline

#4 2006-02-01 10:18:02

ianseckington
New Member
Registered: 2005-03-13
Posts: 9

Re: IIS and non-messy URLs via 404 error pages.

Hmm. After much messing around, I think I’m going to try a clean install of TXP on my own machine this lunchtime and see what happens from scratch — it looks like something has gotten truly borked, with none of the links working properly (they all display every article). I don’t know if this is something to do with my coworker fiddling with the templates last night, but I don’t want to keep fiddling with it if it’s not in a known state.

Will keep this post updated with progress!

Offline

#5 2006-02-01 17:14:17

ianseckington
New Member
Registered: 2005-03-13
Posts: 9

Re: IIS and non-messy URLs via 404 error pages.

Blimey. That was an entertaining afternoon.

Okay, I now have what I think to be a good starting point about fixing the whole thing. Tragedy is it involves tampering with publish.php, but doesn’t require any fiddling with config.php. It seems like the root of the problem is the fact that, when you tell IIS to use the funky 404 redirect behaviour, the REQUEST_URI contains something like this: ‘/index.php?404;http://localhost/2006/02/01/hello’ which, it would appear, the publish.php script cannot decipher, so it degrades gracefully to showing all articles.

I added the following block of code starting at line 225 in publish.php in the preText function:

<pre> // Declare a sneaky variable $is_IIS_error_redirect_scheme = false;

// If IIS is in ‘url_rewrite’ mode (that is, custom-handling 404s // it will return a string in the format // ‘/path-to-txp/index.php?404;http://server/path-to-txp/2006/02/01/page-name’ // we need to chop off everything grotty up to and including the // server name/path to textpattern. if( stristr($out[‘request_uri’], ’404;’) ) { $pos = strpos($out[‘request_uri’], $siteurl); if( $pos != false ) { // Skip over the siteurl part of the request $pos = $pos + strlen($siteurl); $len = strlen($out[‘request_uri’]) – $pos; $out[‘req’] = $req = substr( $out[‘request_uri’], $pos, $len );

// As used in Comment.php (and possibly other places) $GLOBALS[‘pretext’][‘request_uri’] = $req;

$is_IIS_error_redirect_scheme = true; } }

// define the useable url, minus any subdirectories. // this is pretty fugly, if anyone wants to have a go at it – dean $out[‘subpath’] = $subpath = preg_quote(preg_replace(“/http:\/\/.(\/.)/Ui”,”$1”,hu),”/”);

// IAS if( $is_IIS_error_redirect_scheme == false ) { $out[‘req’] = $req = preg_replace(“/^$subpath/i”,”/”,serverSet(‘REQUEST_URI’)); }
</pre>

this should replace the original lines (it still includes them):

<pre> // define the useable url, minus any subdirectories. // this is pretty fugly, if anyone wants to have a go at it – dean $out[‘subpath’] = $subpath = preg_quote(preg_replace(“/http:\/\/.(\/.)/Ui”,”$1”,hu),”/”);

$out[‘req’] = $req = preg_replace(“/^$subpath/i”,”/”,serverSet(‘REQUEST_URI’)); </pre><br/>

Official caveat: I’m bloody awful at PHP, and I should really have been doing something else this afternoon, but hopefully it can be rewritten and beautified and prove useful to any IIS users out there.

I have tested this on the two installations I’ve got here and it works for all of the permanent link schemes and both the comment styles.

Last edited by ianseckington (2006-02-01 17:19:33)

Offline

#6 2006-02-02 08:33:58

Sencer
Archived Developer
From: cgn, de
Registered: 2004-03-23
Posts: 1,803
Website

Re: IIS and non-messy URLs via 404 error pages.

when you tell IIS to use the funky 404 redirect behaviour, the REQUEST_URI contains something like this: ’/index.php?404;http://localhost/2006/02/01/hello’

Interesting. I am wondering whether this is server-specific configuration or the “default” setup for IIS in those cases. Do you happen to have a link to some documentation on this?

Offline

#7 2006-02-02 09:33:17

ianseckington
New Member
Registered: 2005-03-13
Posts: 9

Re: IIS and non-messy URLs via 404 error pages.

From Configuring Custom Error Messages in IIS 5.0

Mapping to a URL

Mapping to a file does a very good job of replacing the nasty generic HTTP error. What if you wanted to create an application to handle these errors and do more than just showing a graceful error page, for example: sending an email alert to the administrator as soon as this error occurs?

Mapping to a URL is different from the file method in handling the HTTP error. If a 404 error occurs when mapping to a URL, the script is invoked as a URL, which passes context as part of the query string. For example, if you have an .asp file called Handle404.asp, which is defined to handle HTTP 404 errors. When a user tries to access a file that does not exist on your server, say a file called missing.htm, then the ASP file will be invoked as if the user had typed:

http://www.reskit.com/handle404.asp?404;http://www.reskit.com/missing.htm

in the Web browser.

I’ve seen this behaviour myself since IIS 5.0, but I suspect it probably goes back even further.

If you want to explore this any further, let me know.

Offline

#8 2006-02-02 10:48:33

Sencer
Archived Developer
From: cgn, de
Registered: 2004-03-23
Posts: 1,803
Website

Re: IIS and non-messy URLs via 404 error pages.

Ian, this is very helpful. Thank you!

Given that the 404-way of achieving clean urls is still somewhat experimental I am not sure where this will lead us. But it looks like it would be a nice short-term solution to create a plugin which a) does something to modify the IIS-404-url to something that textpattern can handle and b) defines the constant necessary to work around the comment problems (see step 2 in my weblog post above) – or do comments work for you know, even without that last change?

Would you be able to test such a plugin if I emailed it to you?
(It will take me a while – if anybody else wants to do it, go ahead! ).

Offline

#9 2006-02-02 10:53:40

ianseckington
New Member
Registered: 2005-03-13
Posts: 9

Re: IIS and non-messy URLs via 404 error pages.

Hi Sencer,

commenting does currently work with my hack included in the scenarios I’ve tried — although I must admit that I’ve not had time to properly test this — obviously any time that any other routine tries to use the request_uri variable straight from the server variables the call will fail, so there may well be lurking gotchas!

If you get a plugin put together, I’d be delighted to test it out for you on the set up I have here. Just wish I knew enough PHP/was less of a hack to be able to help in the coding!

Offline

#10 2006-02-23 14:09:45

chrismc
Plugin Author
Registered: 2004-09-13
Posts: 37
Website

Re: IIS and non-messy URLs via 404 error pages.

Hi,

I’ve been trying this method so I can get clean URLs working on IIS. I couldn’t get any of the above to work “straight-off” – for some reason, a port number gets inserted along the way, throwing everything off (e.g. index.php?404;http://site.com:80/development/article/first-post/).

I slightly modified things (see below), but comments do not work. Any ideas why this might be?

<code>
// If IIS is in ‘url_rewrite’ mode (that is, custom-handling 404s // it will return a string in the format // ‘/path-to-txp/index.php?404;http://server/path-to-txp/2006/02/01/page-name’ // we need to chop off everything grotty up to and including the // server name/path to textpattern. $iisreq = $out[‘request_uri’]; //for checking if a 404 $out[‘request_uri’] = str_replace(array(‘404;’,’:80’),’‘,$out[‘request_uri’]); //strip 404 & port number if( stristr($iisreq, ’404;’) ) { $pos = strpos($out[‘request_uri’], $siteurl); if( $pos != false ) { // Skip over the siteurl part of the request $pos = $pos + strlen($siteurl); $len = strlen($out[‘request_uri’]) – $pos; $out[‘req’] = $req = substr( $out[‘request_uri’], $pos, $len );

// As used in Comment.php (and possibly other places) $GLOBALS[‘pretext’][‘request_uri’] = $req;

$is_IIS_error_redirect_scheme = true; } }
</code>

btw, I was able to condense all your code into this (replacing dean’s original line around about 229)
<code>
$out[‘req’] = $req = substr(preg_replace(“/^$subpath/i”,”/”,str_replace(array(‘404;’,’:80’),’‘,$out[‘request_uri’])), strlen($siteurl));
// As used in Comment.php (and possibly other places) $GLOBALS[‘pretext’][‘request_uri’] = $req;
</code>

Although as mentioned, comments don’t work…

Offline

#11 2006-02-23 15:30:59

jdueck
Plugin Author
From: Minneapolis, MN
Registered: 2004-02-27
Posts: 147
Website

Re: IIS and non-messy URLs via 404 error pages.

This caught my eye. One potential problem with this whole approach that I’ve never seen addressed is that all your hits are logged as a 404 error. How do you avoid skewing the logs??

As mentioned elsewhere, I’ve found that using ISAPI Rewrite works well for clean urls on IIS. I have been able to make it work with section/id/title format – If anyone needs info on that I can send them a sample httpd.ini file.

Offline

#12 2006-02-23 15:38:43

Sencer
Archived Developer
From: cgn, de
Registered: 2004-03-23
Posts: 1,803
Website

Re: IIS and non-messy URLs via 404 error pages.

Hi chrismc,

I responded to comments working/not working in my first response above (post #2). If you follow the link to the webog article you’ll find a solution (as I wrote in post #8 in this thread: “b) defines the constant necessary to work around the comment problems (see step 2 in my weblog post above”).

I hope that helps.

Offline

Board footer

Powered by FluxBB