views:

207

answers:

1

First look at this url:

http://stackoverflow.com/questions/tagged/xoxoxo/

This directory does not exists but somehow stackoverflow can pass that last directory as a parameter to his base script.

Is this possible to configure IIS or Apache to do so? How?

+3  A: 

The mechanism behind this kind of behaviour is called url-rewriting and can be implemented in Apache with the mod_rewrite-modules and in IIS with either Helicons ISAPI_Rewrite Lite (or one of the non-free alternatives offered by Helicon) for IIS 5.1 and 6 or with the Microsoft URL Rewrite Module for IIS 7.

For example the following settings will ensure that every request that cannot be matched on an existing file or directory will be transfered to the index.php file.

mod_rewrite (.htaccess in your document root directory or somewhere in your httpd.conf)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR] // IF is file (with size > 0)
RewriteCond %{REQUEST_FILENAME} -l [OR] // OR is symbolic link
RewriteCond %{REQUEST_FILENAME} -d      // OR is directory  
RewriteRule ^.*$ - [NC,L]               // DO NOTHING
RewriteRule ^.*$ index.php [NC,L]       // TRANSFER TO index.php

*ISAPI_Rewrite Lite* (in the appropriate dialog of your IIS settings)

// uses same syntax as mod_rewrite
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Microsoft URL Rewrite Module (in your web.config in the document root directory or seomewhere in the configuration tree)

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="MatchExistingFiles" stopProcessing="true">
                    <match url="^.*$" />
                    <conditions logicalGrouping="MatchAny">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" pattern="" ignoreCase="false" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" pattern="" ignoreCase="false" />
                    </conditions>
                    <action type="None" />
                </rule>
                <rule name="RemapMVC" stopProcessing="true">
                    <match url="^.*$" />
                    <conditions logicalGrouping="MatchAll" />
                    <action type="Rewrite" url="index.php" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
Stefan Gehrig