3

I would like to deploy react.js project on iis server at a subdomain.

The works I do below:

  1. BrowserRouter with basename

import { BrowserRouter } from 'react-router-dom';

<BrowserRouter basename="/Subdomain">
   <App></App>
</BrowserRouter>

In App;

<Switch>
  <Route exact path="/" component={Main} />
...
</Switch>
  1. Add 'HomePage' at package.json

    "homepage": "https://domain/subdomain/"

  2. Install URL Rewrite for IIS and create web.config

https://www.iis.net/downloads/microsoft/url-rewrite

web.config

<?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
      <rewrite>
        <rules>
          <rule name="Rewrite Text Requests" stopProcessing="true">
            <match url=".*" />
            <conditions>
              <add input="{HTTP_METHOD}" pattern="^GET$" />
              <add input="{HTTP_ACCEPT}" pattern="^text/html" />
              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            </conditions>
            <action type="Rewrite" url="/index.html" />
          </rule>
        </rules>
      </rewrite>
    </system.webServer>
    </configuration>

Although I did iisreset, still getting error (404 - File or directory not found.) when open page at Link in BrowserRouter.

Thank you for your help.

1
  • did you found any solution ? Commented Nov 16, 2021 at 11:41

2 Answers 2

2

Include your subdomain in the action of web.config. It works.

<action type="Rewrite" url="/subdomain/index.html" />

And you might need to place the web.config inside your subdomain folder. Not in the root folder.

Sign up to request clarification or add additional context in comments.

Comments

0

I have refresh and subdomain problems. This configuration solves my problems.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
  <rewrite>
    <rules>
      <rule name="Rewrite Text Requests" stopProcessing="true">
        <match url=".*" />
        <conditions>
          <add input="{HTTP_METHOD}" pattern="^GET$" />
          <add input="{HTTP_ACCEPT}" pattern="^text/html" />
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        </conditions>
        <action type="Rewrite" url="/ui/index.html" />
      </rule>
    </rules>
  </rewrite>
  <httpErrors errorMode="Custom" existingResponse="Replace">   
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" path="/ui" responseMode="ExecuteURL" />           
  </httpErrors>
  </system.webServer>
</configuration>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.