简体   繁体   中英

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

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.

Courtesy of: http://cbsa.com.br/tools/online-convert-htaccess-to-web-config.aspx

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

After doing R&D for long time and trying various different ways I found the solution ,here is content of my whole web.config

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



    <system.webServer>
        <defaultDocument enabled="true">
            <files>
                <clear />
                <add value="index.php" />

            </files>
        </defaultDocument>

        <rewrite>
            <rules>

            <rule name="Imported Rule 3" stopProcessing="true">
                <match url="^index\.html$" ignoreCase="false" />
                <action type="Rewrite" url="index.php" appendQueryString="false" />
            </rule> 

            <rule name="Imported Rule 2" stopProcessing="true">
                <match url="^news\.html$" ignoreCase="false" />
                <action type="Rewrite" url="news.php" appendQueryString="true" />
            </rule>

            <rule name="Imported Rule 4" stopProcessing="true">
                <match url="^(.*)\.html$" ignoreCase="false" />
                <action type="Rewrite" url="pages.php?pageid={R:1}" appendQueryString="false" />
            </rule> 





            </rules>
        </rewrite>

    </system.webServer>




    <system.data>
        <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.3.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
    </system.data>
</configuration>

It will be very useful for the person who is trying to host PHP website on Window Server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM