how to enable mod_rewrite on iis server for php pages

Recently, I am working with one project which is hosted on the windows IIS server. I used .htaccess for URL rewrite rules for SEO(Search Engine Optimization) purpose.

I see that its not working on the IIS while it is working perfect on my local apache server. I see that may be URL Rewrite module may not be installed on the IIS server.

After looking on the IIS configuration, I knew that URL Rewrite module is installed but its not working similar to apache mod_rewrite. I need to enable mod_rewrite on IIS server same way its works in Apache server. After spending lots of time, I found the solution for this.

I will show you simple way using that you can get similar functionality like Apache on IIS Server.

You need to create web.config file in your root directory using below code.

Your web.config file code to test:

 
	<?xml version="1.0" encoding="UTF-8"?>
	<configuration>
		<system.webServer>
			<rewrite>
				<rules>
					<clear />
					<rule name="Redirect to google.com" stopProcessing="true">
						<match url="^google$" />
						<action type="Redirect" url="http://www.google.com/" appendQueryString="false" />
					</rule>
				</rules>
			</rewrite>
		</system.webServer>
	</configuration>

Once you did this than its time to test that its working or not?
To test try to open http://www.domain.com/google in browser where domain.com is your website’s domain name. If you are redirected to Google.com website means your URL rewrite module installed and working same like apache mod_rewrite.

This is just to check that Microsoft URL Rewrite module installed on your IIS server or not, if its installed than change your web.config file with below code:

Your final web.config file code:

 
	<?xml version="1.0" encoding="UTF-8"?>
	<configuration>
	  <system.webServer>
		<rewrite>
		  <rules>
			<rule name="Security Rule" stopProcessing="true">
			  <match url="^(.*)$" ignoreCase="false" />
			  <conditions logicalGrouping="MatchAny">
				<add input="{QUERY_STRING}" pattern="mosConfig_[a-zA-Z_]{1,21}(=|\%3D)" ignoreCase="false" />
				<add input="{QUERY_STRING}" pattern="base64_encode.*\(.*\)" ignoreCase="false" />
				<add input="{QUERY_STRING}" pattern="(\&lt;|%3C).*script.*(\>|%3E)" />
				<add input="{QUERY_STRING}" pattern="GLOBALS(=|\[|\%[0-9A-Z]{0,2})" ignoreCase="false" />
				<add input="{QUERY_STRING}" pattern="_REQUEST(=|\[|\%[0-9A-Z]{0,2})" ignoreCase="false" />
			  </conditions>
			  <action type="CustomResponse" url="index.php" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
			</rule>
			<rule name="SEO Rule">
			  <match url="(.*)" ignoreCase="false" />
			  <conditions logicalGrouping="MatchAll">
				<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
				<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" pattern="" ignoreCase="false" />
				<add input="{URL}" negate="true" pattern="^/index.php" ignoreCase="false" />
				<add input="{URL}" pattern="(/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$" />
			  </conditions>
			  <action type="Rewrite" url="index.php" />
			</rule>
		  </rules>
		</rewrite>
	 </system.webServer>
	</configuration>

if you get Cakephp : 404 – File or directory not found error on the windows server than please check Microsoft URL Rewrite module installed on your IIS server or not? If not than you need to enable mod_rewrite on iis server to run your cakephp project.