0

These are my web.config rules:

<rule name="Remove Trailing Slash">
  <match url="(.*)(/|\\)$"/>
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
  </conditions>
  <action type="Redirect" url="{R:1}"/>
</rule>
<rule name="anything" stopProcessing="true">
  <match url="^(.*?)$"/>
  <conditions>
    <add input="{DOCUMENT_ROOT}/{R:1}" matchType="IsFile" negate="true"/>
  </conditions>
  <action type="Rewrite" url="/core/handler.php?url={R:1}"/>
</rule>

And these are my .htaccess rules:

# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)(/|\\)$ $1 [R]

# everything
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)$ /core/handler.php?url=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)$ /core/handler.php?url=$1&%{QUERY_STRING} [L]

They should behave the same, yet they don't, but I can't figure out what I'm doing wrong. I'm using the IIS rewrite module for my local website testing, but the server I'm hosting on uses apache and therefore .htaccess.

The point of this is to use stupidly complicated php handler to do all of the operations are usually done by the server (I would delve into the reasons but that's not the point of this question). The rules are written to rewrite the requested url so that the server serves the handler, which serves the correct content, while keeping the same face.

To summarise:

web.config:
input: http://local.com/folder_name
output: http://local.com/folder_name
internal: http://local.com/core/handler.php?url=folder_name

.htaccess:
input: http://server.com/folder_name
output: http://server.com/folder_name/?url=folder_name
internal: http://server.com/core/handler.php?url=folder_name

Any idea what went wrong?

1 Answer 1

1

Apache doesn't like directories that don't end with trailing slashes. The reason is because it exposes the directory contents even if you have an index file. So by default, mod_dir will always redirect requests for directories that don't have a trailing slash. To counter this, you can turn off this redirect, but be warned that with it off, there's the danger of exposing directory contents.

Also, your last two rules can be combined into one by using the QSA flag:

DirectorySlash Off

# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)/$ /$1 [L,R]

# everything
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)$ /core/handler.php?url=$1 [L,QSA]
Sign up to request clarification or add additional context in comments.

4 Comments

Will it expose directory contents if I have directory browsing disabled?
Also, for everything redirecting to the handler page, why would it be able to expose anything else?
@Aarilight You can't see contents if directory browsing is disabled, and you can't see directories if accessing directories directly gets routed to the handler. It's just something to keep in mind (like if later you change your setup and forget to turn DirectorySlash back on, or you migrate your htaccess file somewhere else and change the rules or have different settings)
All righty, thanks for that information. The QSA thing is really handy to know too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.