简体   繁体   English

Next.JS - `.htaccess` 文件?

[英]Next.JS - `.htaccess` file?

I'm just starting out with my first Next.JS app.我刚刚开始使用我的第一个 Next.JS 应用程序。 I've used npx create-next-app and have made a few pages, when I realized that I'm not sure how to use a .htaccess file.当我意识到我不确定如何使用.htaccess文件时,我使用了 npx npx create-next-app并制作了几页。 I'm used to Apache taking care of this stuff for me, and simply putting my .htaccess file into my Next.JS app's root directory unsurprisingly didn't seem to cut it.我已经习惯了 Apache 为我处理这些事情,并且只是将我的.htaccess文件放入我的 Next.JS 应用程序的根目录中,不出所料似乎并没有削减它。 How would I go about setting up a .htaccess file similar to the following?我 go 如何设置类似于以下内容的.htaccess文件?

RewriteEngine on

RewriteRule ^profile/([a-z0-9]+) profile.html

.htaccess files are specific to Apache, so without Apache you can't use them. .htaccess文件是特定于 Apache 的,因此如果没有 Apache,您将无法使用它们。 You can use Apache as a proxy to your node.js app , but you would still not use a .htaccess file;您可以使用Apache 作为您的 node.js 应用程序的代理,但您仍然不会使用.htaccess文件; you could configure RewriteRules in your Apache config but there is no need to when you can handle all your routing directly in your application logic.您可以在 Apache 配置中配置 RewriteRules,但是当您可以直接在应用程序逻辑中处理所有路由时就不需要了。

In node.js you don't need a separate web server like Apache.在 node.js 中,您不需要像 Apache 这样的单独 Web 服务器。 Your program can be long-running, bind to a port, and listen and respond to requests which is the main functionality that a web-server normally provides.您的程序可以长时间运行、绑定到端口、侦听和响应请求,这是 Web 服务器通常提供的主要功能。

Next.JS has documentation for setting up custom routing here: https://nextjs.org/docs/#custom-server-and-routing Next.JS 有在此处设置自定义路由的文档: https ://nextjs.org/docs/#custom-server-and-routing

You have to learn to use pm2 :你必须学会​​使用 pm2 :

  1. You should be able to install nvm in your ubuntu, centos etc.. via ssl: from https://github.com/nvm-sh/nvm您应该能够通过 ssl 在您的 ubuntu、centos 等中安装 nvm:来自https://github.com/nvm-sh/nvm
  2. nvm will give you possibilities to install node nvm 将为您提供安装节点的可能性
  3. After you complete the installation of node, install pm2 globally,完成node安装后,全局安装pm2,
  4. https://pm2.keymetrics.io/ https://pm2.keymetrics.io/
  5. At root dir of project create file : ecosystem.config.js在项目的根目录创建文件:ecology.config.js

ecosystem.config.js :生态系统.config.js :

module.exports = {
apps : [
    {
        name: "your_server_name",
        script: "./server.js",
        watch: true,
        env_development: {
            "PORT": 3000,
            "NODE_ENV": "development"
        },
        env_production: {
            "PORT": 8001,
            "NODE_ENV": "production",
        }
    }
]}

.htaccess look like this : .htaccess 看起来像这样:

DirectoryIndex disabled
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:8001/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:8001/$1 [P,L]

Login to your site over SSH:通过 SSH 登录到您的站点:

ssh name@IP then password

How to run pm2 .如何运行 pm2 。

pm2 start ecosystem.config.js --env production

pm2 start ecosystem.config.js --env development

Which files u need in server :你在服务器中需要哪些文件:

服务器上的文件

If your server is already using Apache and it has mod_rewrite enabled, you can use this .htaccess :如果您的服务器已经在使用 Apache 并且启用了mod_rewrite ,您可以使用这个.htaccess

<IfModule mod_rewrite.c>

  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule . /index.html [L]

</IfModule>

Source 来源

If You use next export to SSG on your project This .htaccess file will fix redirection problem如果您在项目中使用下一个导出到 SSG这个.htaccess文件将修复重定向问题

RewriteEngine On
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

The top answer didn't help me as I was using multiple levels of params....最佳答案对我没有帮助,因为我使用了多个级别的参数....

Imagine the page pages/{...complex}.tsx , where complex is ["a","b","c"] .想象页面pages/{...complex}.tsx ,其中 complex 是["a","b","c"] The following helped me:以下帮助了我:

DirectorySlash Off
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ $1.html

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

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