简体   繁体   English

如何使用htaccess从子URL重写到子目录?

[英]How to rewrite from sub URL to sub directory with htaccess?

My backend is PHP and in the frontend I want to have a reactJS compiled project that I want to allow user access to via example.com/app and that compiled project's index.html file is placed in /var/www/html/reactjs/build/index.html and I need to somehow rewrite all example.com/app requests to that directory otherwise the reactJS app won't work properly / at all.我的后端是 PHP,在前端我想要一个 reactJS 编译项目,我希望允许用户通过example.com/app访问,并且该编译项目的index.html文件放在/var/www/html/reactjs/build/index.html并且我需要以某种方式将所有example.com/app请求重写到该目录,否则 reactJS 应用程序将无法正常工作/根本无法正常工作。

This is what I've tried to do and not working for me:这就是我尝试做的,但对我不起作用:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/app /reactjs/build/index.html [QSA,L]

IDK what else to try, I'd love to get a direction on how to solve this issue :) IDK还有什么可尝试的,我很想知道如何解决这个问题的方向:)

RewriteRule ^/app /reactjs/build/index.html [QSA,L]

In .htaccess the URL-path matched by the RewriteRule pattern does not start with a slash, so this would need to be rewritten:.htaccess ,由RewriteRule模式匹配的 URL-path 不以斜杠开头,因此需要重写:

RewriteRule ^app /reactjs/build/index.html [L]

The QSA flag is also not required here.这里也不需要QSA标志。

UPDATE: site I'm using is a wordpress site更新:我使用的网站是一个 wordpress 网站

These directives need to go before the WordPress front-controller, otherwise these requests for /app/... will be routed to WordPress instead.这些指令需要WordPress的前端控制器之前去,否则这些请求/app/...将被路由到WordPress来代替。 For example:例如:

# ReactJS routing...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^app /reactjs/build/index.html [L]

# BEGIN WordPress
:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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