I'm just starting out with my first Next.JS app. 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. 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. How would I go about setting up a .htaccess file similar to the following?
RewriteEngine on
RewriteRule ^profile/([a-z0-9]+) profile.html
If You use next export to SSG on your project
This .htaccess file will fix redirection problem
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]
.htaccess files are specific to Apache, so without Apache you can't use them. You can use Apache as a proxy to your node.js app, but you would still not use a .htaccess file; 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.
In node.js you don't need a separate web server like Apache. 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.
Next.JS has documentation for setting up custom routing here: https://nextjs.org/docs/#custom-server-and-routing
You have to learn to use pm2 :
You should be able to install nvm in your ubuntu, centos etc.. via ssl:
from https://github.com/nvm-sh/nvm
nvm will give you possibilities to install node
After you complete the installation of node, install pm2 globally,
https://pm2.keymetrics.io/
At root dir of project create file : ecosystem.config.js
ecosystem.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 :
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 name#IP then password
How to run 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:
<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
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"]. The following helped me:
DirectorySlash Off
RewriteEngine On
RewriteRule ^$ index.html [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)(?<!\.html)\/?$ $1.html [QSA,L]
ErrorDocument 404 /404.html
Related
I'm using a shared hosting service that always has Apache web server running, so I can't run my Node.js application directly on port 80. Instead, as I've been told by my host, I need to use .htaccess to redirect incoming requests to my Node.js app, which is currently running on port 50000. Here's the .htaccess file they told me to use:
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:50000 [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:50000/$1 [P,L]
This works well enough, except that when I try to go to mydomain.com, the Node app is seeing a request for /index.php. The tech support for my host seems to be as confused as I am. If I go to mydomain.com/test then Node.js app sees /test, so Apache seems to only be adding index.php on the root URL. Could this be an Apache caching issue from someone accessing the URL prior to the .htaccess file and Node.js app being set up?
UPDATE
At this point, no one seems to have a clue what is going on, so I'm just going to add an 'index.php' route to my Node app. Thanks to everyone who took a look and tried to help out.
You might have DirectoryIndex set up for index.php in apache conf file which may be the reason you are getting index.php automatically, what you can do is to set DirectoryIndex to some filename which may not exist or if it is apache 2.4 use DirectoryIndex disabled in your .htaccess.
This is actually what you are going to want to put in your /public_html directory
In the .htaccess file in the code below you will see
http://127.0.0.1:50000
(50000) is the port you are sending it too. There are 2 spots where you make that update.
Also update the example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
DirectoryIndex disabled
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:50000 / [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:50000 /$1 [P,L]
This is a specific configuration for an apache server with nodejs.
i'm trying to add alongside an old Wordpress website a route that servers a NodeJS Instance.
The thing is that I want to run both Wordpress and Node.js on the same server but on different routes.
Wordpress is running here http://example.com
Node is running here http://example.com:61000/oferta-de-pret-traduceri
.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/oferta-de-pret-traduceri$ http://127.0.0.1:61000/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/oferta-de-pret-traduceri/(.*)$ http://127.0.0.1:61000/$1 [P,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I don't know Wordpress that well nor do I know .htaccess like I should in this case and I don't want to move everything to NGINX unless I have to.
I want my Node.js instance to run on http://example.com/oferta-de-pret-traducerii not on http://example.com:61000.
Also Wordpress trows a 404 error if I try to access http://example.com/oferta-de-pret-traduceri
Hope someone has encountered this kind of situation and has an easy solution.
Thank you very much in advance !
You might be better off with ProxyPassMatch https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypassmatch
ProxyPassMatch "^/oferta-de-pret-traduceri$" "http://127.0.0.1:61000/$1"
The docs for mod_rewrite say this:
Consider using either ProxyPass or ProxyPassMatch whenever possible in
preference to mod_rewrite.
If you cannot use ProxyPass, try this rule:
RewriteRule ^oferta-de-pret-traduceri http://127.0.0.1:61000$1 [P,L]
I used this tool to test: http://htaccess.mwl.be/
If your node app expects 'oferta-de-pret-traduceri' to be in the URL, you will need this rule:
RewriteRule ^oferta-de-pret-traduceri http://127.0.0.1:61000/oferta-de-pret-traduceri$1 [P,L]
This is the architecture of my website :
/
app/
index.php
...
libs/
lib1/
file.php
lib2/
...
I need to access index.php by this url : domain.com/index.php
I tried this :
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)app
RewriteRule ^(.*)$ app/$1 [L]
It works, but inside my index.php, I call for example :
include('../libs/lib1/file.php');
It's doesn't work because this path refer to root now...
And I can't access to domain.com/libs anymore, because it's looking for domain.com/app/libs.
How can I do ?
The include() shouldn't care what the path the browser sees. That should be based on the local filesystem on the server. But your rules are affecting direct access to the libs, so try adding a few more conditions:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^app/
RewriteRule ^(.+)$ app/$1 [L]
RewriteRule ^$ app/index.php [L]
This makes it so requests for existing files or content won't get routed through the app folder.
I'm working on a project hosted on the Cloud9 IDE. I had a simple set of mod_rewrite rules set up, but they no longer work after c9's new version rollout. It took me forever to iron out these rules (I'm a novice at best at this) I'm confused as to why these rules no longer work (AFAIK, the new c9 version should not have affected mod_rewrite rules).
Here are the rules (located in the root .htaccess)
RewriteEngine on
Options FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_URI} ^/css/.*$ [OR]
RewriteCond %{REQUEST_URI} ^/img/.*$ [OR]
RewriteCond %{REQUEST_URI} ^/js/.*$
RewriteCond Astralis/resources%{REQUEST_URI} -f
RewriteRule ^(.+)$ Astralis/resources/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The goal is pretty straightforward... All requests going to /css/... /img/... or /js/... should serve up the associated file within Astralis/resources (after checking that the file exists). Otherwise, redirect the rest of the traffic to index.php.
The problem I am experiencing is that all requests to resources (css, img, js) are returning 404's. If I move the /css, /img, and /js folders from within Astralis/resources back to the root directory, all the resources load properly. This problem started happening after c9's new version, with no changes to the .htaccess file, the codebase, or directory structure.
Any clue as to what is going on? How do I debug this kind of stuff? Any general tips/tricks for writing mod_rewrite rules would also be appreciated. Thanks.
Since Apache needs full path of the file in order to return true using -f you will need to use %{DOCUMENT_ROOT}/ before your file path.
Have it this way:
RewriteEngine on
Options FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(css|img|js)/ [NC]
RewriteCond %{DOCUMENT_ROOT}/Astralis/resources%{REQUEST_URI} -f
RewriteRule ^(.+)$ Astralis/resources/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
i'm trying to remove index.php form an URL:
this works
http://server/bw/index.php/test
this doesn't work
http://server/bw/test
i try to change .htaccess and watching on web i see that it should be like this:
RewriteEngine On
RewriteBase /bw/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
i try editing it in this way:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
or in this way:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /bw/index.php [QSA,L]
or in this way:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
But when i try to access to http://server/bw/test it says me:
Not Found
The requested URL /bw/test was not found on this server.
Apache/2.2.15 (CentOS) Server at server Port 80
I check that inside my httpd.conf LoadModule rewrite_module modules/mod_rewrite.so is enable.. i don't know what to do now..
how can i solve? please help me!
Try this, which used e.g. in WordPress
RewriteRule . index.php [L]
or this, which is used by e.g. Lavavel PHP Framework
RewriteRule ^(.*)$ index.php/$1 [L]
You might also consider adding
RewriteCond %{REQUEST_FILENAME} !-d
before the RewriteRule to also exclude existing directories, not only existing files. But that's up to you.
In my case I updated AllowOverride All ,
then run sudo a2enmod rewrite to avoid Internal 500 error then restart Apache service apache2 restart
For me, I got things to work using this line from Dehalion's answer:
RewriteRule . index.php [L]
So the index.php (or any xyz.php) file is not seen in the request url
http://localhost/demo1/mycompany/hello/Jim
With the following caveats:
You have this route defined:
$app->get('/mycompany/hello/:name', doHello );
The root element (for the route /mycompany/..) is also the name of the file.
That is, the route exists in a file called "mycompany.php"
Yes, it's a bit of a hack ... but since I find apache config confusing/intimidating :) ... I figure this solution is stable enough to satisfy the requirements.