Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess to web.config for url rewrite and redirection

I have domain xyz.com which is hosted on Windows server.

Code for xyz.com is written in PHP (previously it was in ASP.NET). Database is MySQL (previously it was in SQL server).

Now after re-developing whole website in PHP I came to know that .htaccess will not work on Windows server. I have to play with web.config.

Here is .htaccess code I have used when I was redeveloping website in PHP locally:

RewriteRule index.html index.php

RewriteRule news.html news.php
RewriteRule search-results.html search-results.php

RewriteRule ^([A-Za-z0-9_\-]+).html$ pages.php?pageid=$1&%{QUERY_STRING} [ne]

One weird thing happening

when i add below line of code in web.config it is working perfectly

    <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>

above code redirecting me to google.com, it means that rewrite module is already enabled

but when i add code mentioned below to web.config

 <rules>
            <rule name="REWRITE_TO_PHP">
            <match url="^(.+).html$" />
                <conditions logicalGrouping="MatchAll" />
                <action type="Rewrite" url="pages.php?pageid={R:1}" />
            </rule>

it is giving me error :

HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related configuration data for the page is invalid.

Can anyone help me with creating equivalent web.config code?

like image 671
user2338456 Avatar asked Dec 30 '25 09:12

user2338456


1 Answers

Try this one.

In your web.config file, find

<rewrite>
     <rules>

This and put the codes inside this. <rewrite><rules> .. codes here... </rules></rewrite> tag.

<rule name="rule 1y">
    <match url="index.html"  />
    <action type="Rewrite" url="index.php"  />
</rule>
<rule name="rule 2y">
    <match url="news.html"  />
    <action type="Rewrite" url="news.php"  />
</rule>
<rule name="rule 3y">
    <match url="search-results.html"  />
    <action type="Rewrite" url="search-results.php"  />
</rule>
<rule name="rule 4y">
    <match url="^([A-Za-z0-9_\-]+).html$"  />
    <action type="Rewrite" url="pages.php?pageid={R:1}&amp;%{QUERY_STRING}"  />
</rule>

So file will look like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <configSections>
        <sectionGroup name="system.webServer">
            <sectionGroup name="rewrite">
                <section name="rewriteMaps" overrideModeDefault="Allow" />
                <section name="rules" overrideModeDefault="Allow" />
            </sectionGroup>
        </sectionGroup>
    </configSections>

    <system.webServer>
        <rewrite>
            <rule name="rule 1y">
                <match url="index.html"  />
                <action type="Rewrite" url="index.php"  />
            </rule>
            <rule name="rule 2y">
                <match url="news.html"  />
                <action type="Rewrite" url="news.php"  />
            </rule>
            <rule name="rule 3y">
                <match url="search-results.html"  />
                <action type="Rewrite" url="search-results.php"  />
            </rule>
            <rule name="rule 4y">
                <match url="^([A-Za-z0-9_\-]+).html$"  />
                <action type="Rewrite" url="pages.php?pageid={R:1}&amp;%{QUERY_STRING}"  />
            </rule>
        </rewrite>
    </system.webServer>
</configuration>

Note: Please don't directly replace code with your web.config file. Just put the required lines in your web.config file.

like image 134
I Am Stack Avatar answered Jan 02 '26 00:01

I Am Stack