I have created some PHP app on Azure but this is my first time i am using IIS for my PHP application. I know how to use .htaccess for url rewrite on Apache and there is no problem. But how to accomplish that on Azure IIS. Here is my .htaccess for Apache.
.htaccess
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
You need to use your Web.config file and use the <system.webServer> and <rewrite> rules within this.
You will need to use the following inside your web.config to replicate the code you have above from your .htaccess:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Proper web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="false" />
<defaultDocument>
<files>
<clear />
<add value="index.php" />
<add value="Default.htm" />
<add value="Default.asp" />
<add value="index.htm" />
<add value="Default.aspx" />
</files>
</defaultDocument>
<rewrite>
<rules>
<rule name="ModX IIS7 Rule 1 (By Simon Fraser)" stopProcessing="true">
<match url=".*" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_USER_AGENT}" pattern="^.*internal\ dummy\ connection.*$" />
</conditions>
<action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
</rule>
<rule name="ModX IIS7 Rule 2 (By Simon Fraser)" stopProcessing="true">
<match url="^(manager|assets)" ignoreCase="false" />
<action type="None" />
</rule>
<rule name="ModX IIS7 Rule 3 (By Simon Fraser)" stopProcessing="true">
<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" />
</conditions>
<action type="Rewrite" url="index.php?q={R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Related
I wanted to convert this configuration file into a htacess file.
I'm looking for a way to convert my rewrite rules to an .htaccess file. I couldn't find any tool to automatically do this and all I can get myself is 500 Internal Server Errors.
The web.config file looks like this:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^index\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^module=([^=&]+)$" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="true" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?module={R:1}" />
</rule>
<rule name="RedirectUserFriendlyURL2" stopProcessing="true">
<match url="^index\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^module=([^=&]+)&page=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL2" stopProcessing="true">
<match url="^([^/]+)/page/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?module={R:1}&page={R:2}" />
</rule>
<rule name="RedirectUserFriendlyURL3" stopProcessing="true">
<match url="^index\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^module=([^=&]+)&p=([^=&]+)&id=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL3" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?module={R:1}&p={R:2}&id={R:3}" />
</rule>
<rule name="RedirectUserFriendlyURL4" stopProcessing="true">
<match url="^index\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^module=([^=&]+)&p=([^=&]+)&id=([^=&]+)&page=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}/{C:3}/{C:4}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL4" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/([^/]+)/page/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?module={R:1}&p={R:2}&id={R:3}&page={R:4}" />
</rule>
<rule name="RedirectUserFriendlyURL5" stopProcessing="true">
<match url="^index\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^module=([^=&]+)&p=([^=&]+)&date=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
</rules>
<outboundRules>
<rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)index\.php\?module=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/" />
</rule>
<rule name="OutboundRewriteUserFriendlyURL2" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)index\.php\?module=([^=&]+)&(?:amp;)?page=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/page/{R:3}/" />
</rule>
<rule name="OutboundRewriteUserFriendlyURL3" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)index\.php\?module=([^=&]+)&(?:amp;)?p=([^=&]+)&(?:amp;)?id=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/{R:3}/{R:4}/" />
</rule>
<rule name="OutboundRewriteUserFriendlyURL4" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)index\.php\?module=([^=&]+)&(?:amp;)?p=([^=&]+)&(?:amp;)?id=([^=&]+)&(?:amp;)?page=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/{R:3}/{R:4}/page/{R:5}/" />
</rule>
<rule name="OutboundRewriteUserFriendlyURL5" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)index\.php\?module=([^=&]+)&(?:amp;)?p=([^=&]+)&(?:amp;)?date=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/{R:3}/{R:4}/" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
<urlCompression doStaticCompression="true" />
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.24:00:00" />
</staticContent>
</system.webServer>
</configuration>
So my solution was like this.
RewriteRule ^index\.php$ {C:1}
RewriteRule ^([^/]+)/?$ index.php?module=$1
RewriteRule ^index\.php$ {C:1}/{C:2}
RewriteRule ^([^/]+)/page/([^/]+)/?$ index.php?module=$1&page=$2
RewriteRule ^index\.php$ {C:1}/{C:2}/{C:3}
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?module=$1&p=$2&id=$3
RewriteRule ^index\.php$ {C:1}/{C:2}/{C:3}/{C:4}
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/page/([^/]+)/?$ index.php?module=$1&p=$2&id=$3&page=$4
RewriteRule ^index\.php$ {C:1}/{C:2}/{C:3}
Bus I'm having some problems with this code. Some rules are really not working. Will you help me to solve this problem.
Thanks in advance!
first you should check mod_rewrite.c is active in your server or not , then turn it on and start re-write urls like this :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?module=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ /index.php?module=$1&p=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?module=$1&p=$2&id=$3 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/page/([^/]*)$ /index.php?module=$1&p=$2&id=$3&page=$4 [L]
</IfModule>
I uploaded my website to a server that is using windows server 2008 r2 and Im getting a problem ( I think its caused because of the .htaccess files ) I get internal 500 error message, im using xampp
My website its not directly on htdocs folder, its on htdocs/server/ maybe that can be an issue?
Also heres my .htaccess file
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Thanks
I don't think Windows Server will obey your .htaccess.
Try define the same logic in your webconfig file.
First of all, you need to direct it to have as your website root, the public folder.
And then inside this folder, try this web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<defaultDocument>
<files>
<clear />
<add value="index.php" />
<add value="default.aspx" />
<add value="Default.htm" />
<add value="Default.asp" />
<add value="index.htm" />
<add value="index.html" />
</files>
</defaultDocument>
<handlers accessPolicy="Read, Execute, Script" />
<rewrite>
<rules>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Best wishes
What IIS URL Rewrite rule set in web.config is equivalent to this mod_rewrite rule set provided in Question2Answer's default .htaccess?
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
The rule differs from a typical WordPress-style rewrite in that nested path elements need to be passed through. Following is an example of a test URL that Question2Answer uses to verify rewriting is working, when Question2Answer is deployed into a qa directory on the server (deployment to the web root fails similarly):
http://localhost:32568/qa/url/test/%24%26-_~%23%25%5C%40%5E%2A%28%29%3D%21%28%29%5D%5B%60%27%3B%3A%7C%22.%7B%7D%2C%3C%3E%3F%23+%CF%80%C2%A7%C2%BD%D0%96%D7%A9?dummy=¶m=%24%26-_%7E%23%25%5C%40%5E%2A%28%29%3D%21%28%29%5D%5B%60%27%3B%3A%7C%22.%7B%7D%2C%3C%3E%3F%23+%CF%80%C2%A7%C2%BD%D0%96%D7%A9
This is what IIS Manager's "Import mod_rewrite rules" feature came up with:
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="." ignoreCase="false" />
<conditions>
<add input="{URL}" pattern="^(.*)//(.*)$" ignoreCase="false" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{C:1}/{C:2}" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^.*$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" url="index.php?qa-rewrite={R:0}&{QUERY_STRING}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I also added this so that the test URLs would fail in the same way as the actual navigation URLs:
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
There is no answer to a question covering this on question2answer QA's site. Hopefully someone experienced in IIS URL Rewrite here will know.
This configuration works:
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="DeduplicateSlashes" stopProcessing="true">
<match url="." ignoreCase="false" />
<conditions>
<add input="{URL}" pattern="^(.*)//(.*)$" ignoreCase="false" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{C:1}/{C:2}" />
</rule>
<rule name="CleanRouting" stopProcessing="true">
<match url="^.*$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?qa-rewrite={R:0}&{QUERY_STRING}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
<!-- Double escaping is needed for URLs with '+', e.g. for the account page for a username with a space. -->
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>
</configuration>
There I'm really stuck here, and its driving me insane. Ive searched all over but I can't see what I'm doing wrong here.
I'm moving an application that was previously on a windows server with ISAPI_ModRewrite so this worked with htaccess, I'm trying to move this to a windows-server 2008 server which I cant have ISAPI installed on as its a shared hosting environment.
Below is what I'm trying to do but this is just returning a 500 internal server error any ideas what I'm doing wrong? this is the htaccess code I'm trying to convert...
# PRODUCT PAGE
RewriteRule ^([0-9]*)/(.*)/? /prodpage.asp?productid=$1 [L]
# OTHER PAGES
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ /$1.asp [QSA]
RewriteRule ^([^/]+)/([0-9]+)-([0-9]+)/$ /$1.asp?pricerange=$2-$3
RewriteRule ^([^/]+)/([0-9]+)-([0-9]+)/([a-z])/$ /$1.asp?pricerange=$2-$3&sort=$4
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
And this is my web.config file...
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Product Page" stopProcessing="true">
<match url="^([0-9]*)/(.*)/?$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="prodpage.asp?productid={R:1}" appendQueryString="true" />
</rule>
<rule name="Other Page1" stopProcessing="true">
<match url="^([^/]+)/$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.asp" appendQueryString="true" />
</rule>
<rule name="Other Page2" stopProcessing="true">
<match url="^([^/]+)/([0-9]+)-([0-9]+)/$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.asp?pricerange={R:2}-{R:3}" appendQueryString="true" />
</rule>
<rule name="Other Page3" stopProcessing="true">
<match url="^([^/]+)/([0-9]+)-([0-9]+)/([a-z])/$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.asp?pricerange={R:2}-{R:3}&sort={R:4}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
fixed this sorry, thought i would post my answer for those that may need help in future.
i needed to replace the & symbol with & as its xml. Thanks anyway :)
I have a problem similer to this post
I have tried this
RewriteCond %{HTTP_HOST} ^myApplication.com
RewriteCond %{REQUEST_URI} !myApplication/
RewriteRule ^(.*)$ myApplication/$1 [L]
RewriteCond %{HTTP_HOST} www.myApplication.com
RewriteCond %{REQUEST_URI} !myApplication/
RewriteRule ^(.*)$ myApplication/$1 [L]
but the problem is url is working only with www.myApplication.com/myApplication but I want to hide myApplication subfolder .
I will really appreciate any help on this.
UPDATE:
I have tried this which is giving 500 server error.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="mod_rewrite" path="*" verb="*" modules="IsapiModule" scriptProcessor="path to scriptproc" resourceType="Unspecified" requireAccess="None" preCondition="bitness32" />
</handlers>
<rule name="Rewrite to myApplicaton" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)myApplication\.com$" />
<add input="{REQUEST_URI}" pattern="^myApplication/" negate="true" />
</conditions>
<action type="Rewrite" url="/myApplication/{R:0}" />
</rule>
</system.webServer>
</configuration>
The following should do what you want:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to myApplicaton" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)myApplication\.com$" />
<add input="{REQUEST_URI}" pattern="^myApplication/" negate="true" />
</conditions>
<action type="Rewrite" url="/myApplication/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>