0
votes

I'm having to deploy a PHP website to a windows server, and I'm having trouble re-writing the .htaccess to a web.config file. I'm not very knowledgeable in url rewriting rules btw.

I have 3 .htaccess files, one for each directory.

root
 |-- app
    |-- .htaccess
 |-- public
    |-- .htaccess
 .htaccess

For the root, I have this rule, which I believe redirects all traffic to the public folder where the index.php file is:

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteRule ^$ public/ [L]
   RewriteRule (.*) public/$1 [L]
</IfModule>

For that rule, in the web.config file I tried the code below, which apparently works, and redirects to the public folder, so the url is like this 'websitename/public/:

<rule name="Imported Rule 1" stopProcessing="true">
   <match url="^$" ignoreCase="false" />
   <action type="Rewrite" url="public/" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
   <match url="(.*)" ignoreCase="false" />
   <action type="Rewrite" url="public/{R:1}" />
</rule>

For the App folder, I have this rule, which forbids the access to that directory (I don't know if it is needed when migrating to a windows server though):

Options -Indexes

And finally, for the .htaccess at the public folder, which I believe redirects all traffic and query strings to the index.php file, so I can have something like 'website/resources/articles/name', without the '/public' path:

<IfModule mod_rewrite.c>
   Options -Multiviews
   RewriteEngine On
   RewriteBase /newtemplate/public
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
 </IfModule>

I tried this code in the web.config, with no success:

<rule name="Imported Rule 1" stopProcessing="true">
   <match url="^(.+)$" ignoreCase="false" />
   <conditions>
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
      <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
  </conditions>
  <action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
</rule>

I don't know how to achieve that with the web.config rules.

Any suggestion is appreciated.

1
You can directly tell me which url you want to rewrite to another url, I can write a demo for you as a reference. about how to use the iis url rewrite you can refer to this link: Creating Rewrite Rules for the URL Rewrite Module.samwu
Hey, thanks. Actually all pages redirect to index.php inside the "public" folder. So I have something like "websitename.com/public/index.php?url=resources", where the url should become "websitename.com/resources".Fernando Pereira

1 Answers

0
votes

You can try this rule:

<rule name="test" enabled="true" stopProcessing="true">
  <match url=".*" />
    <conditions >
      <add input="{HTTP_HOST}" pattern="^websitename.com$" negate="true" />
    </conditions>
  <action type="Redirect" url="http://websitename.com/resources" redirectType="Permanent" />
</rule>