大网数据广告活动
服务器运维问答

how to run next.js using NODE and react.js at the same time on apache server

· 52CE 我爱测

I have to run 2 different apps on the same domain at the same time. I want to forward all requests to localhost:3000, if url contains /presscenter, if not, default should be my react.js application. I found this one here for apac…

问题原文

原帖链接 · Y U K I M U R A · CC BY-SA 4.0

I have to run 2 different apps on the same domain at the same time. I want to forward all requests to localhost:3000, if url contains /presscenter, if not, default should be my react.js application. I found this one here for apache virtual host:

RewriteEngine On

# Rewrite rule for /presscenter requests
RewriteCond %{REQUEST_URI} ^/presscenter [NC]
RewriteRule ^(.*)$ http://localhost:3000/$1 [P]

# Rewrite rule for all other requests
RewriteCond %{REQUEST_URI} !^/presscenter [NC]
RewriteRule ^(.*)$ /index.html [L]

but at the same time, if I want my react.js application to work properly, I also need to have this .htaccess:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://my.website.ge/$1 [R,L]

#php_value short_open_tag 1
RewriteEngine on
Options +FollowSymlinks
ErrorDocument 404 /404.php
RewriteCond %{QUERY_STRING} ^fbclid=(.*)$


RewriteRule  ^pcms/? - [L]

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

these two rules are overwriting themselves(I guess), and the website is not getting served properly. what changes should I make?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

I'm assuming you have already enabled/configured mod_proxy (and related modules) in the server config.

Try the following instead:

Options +FollowSymlinks -MultiViews

ErrorDocument 404 /404.php

DirectoryIndex index.html

RewriteEngine On

# HTTP to HTTPS redirect
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://my.website.ge%{REQUEST_URI} [R=301,L]

# Rewrite rule for /presscenter requests
RewriteRule ^presscenter http://localhost:3000%{REQUEST_URI} [P]

# No further processing if URL starts "/pcms" and "fbclid" URL param at start of qs
RewriteCond %{QUERY_STRING} ^fbclid=
RewriteRule ^pcms - [L]

# Front-controller for react.js app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.html [L]

I've simplified some of the regex, eg. ^pcms is the same as ^pcms/? - whether that is really the intention or not it's not clear. (Although I suspect you probably intended it to match pcms or pcms/<something>, in which case the regex should be ^pcms($|/) instead.)

A little curious is that you also have /404.php (a PHP script?), although this is rarely used. The ErrorDocument 404 is only relevant for requests that start /pcms and have a fbclid URL parameter at the start of the query string and which do not map to a physical file or directory.


# Rewrite rule for all other requests
RewriteCond %{REQUEST_URI} !^/presscenter [NC]
RewriteRule ^(.*)$ /index.html [L]

This is not required (but it is also incorrect) since the request is rewritten to index.html by a later rule. If you rewrite any request that does not start /presscenter then it will essentially result in a rewrite loop since it rewrites everything - including all static assets - to itself (index.html).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-04-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。