使用nginx服务器将URL重写为index.php

使用nginx服务器将URL重写为index.php,php,.htaccess,nginx,redirect,mod-rewrite,Php,.htaccess,Nginx,Redirect,Mod Rewrite,我需要帮助在nginx服务器上配置我的网站。之前我使用apache,所以在index.php上使用htaccess重定向。但我只是知道nginx不支持htaccess。我是nginx服务器的新手。让我举例说明 当我使用apache时,我的htaccess如下- <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILE

我需要帮助在nginx服务器上配置我的网站。之前我使用apache,所以在index.php上使用htaccess重定向。但我只是知道nginx不支持htaccess。我是nginx服务器的新手。让我举例说明

当我使用apache时,我的htaccess如下-

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ index.php [L]
</IfModule>
location / {
    # Check if a file or directory index file exists, else route it to index.php.
    try_files $uri $uri/ /index.php;
}
我还尝试了此处建议的解决方案:

我的要求是重写所有这些URL:

https://example.com/xyz 
https://example.com/abc
https://example.com/pqr

https://example.com/index.php
我的文件夹结构-


提前感谢。

如果您只想在文件不存在时重写(而不是重定向),则应使用“try\u files”,例如:

location / {
    try_files $uri $uri/ /index.php =404;
}

然后nginx将首先测试请求是否映射到一个文件,然后测试它是否映射到包含索引文件的目录,然后在文档根目录中查找index.php。它将根据发现的内容解决请求。如果没有匹配项,它将返回404。

Hey@code-a1,感谢您的回复。是的,我想重写URL,但不重定向。很抱歉,混淆了。我已经更新了我的问题。我也尝试过你的解决方案,但似乎不起作用。知道我做错了什么吗?@LyricxMint文件index.php存在,您正在编辑正确的文件?与apache不同,您不需要在中创建.htaccess文件directory@code-这里有一个index.php文件。我正在对我的/etc/nginx/sites available/default文件进行所有更改,事实上我的基本url工作正常,请参见这里的xdownloader.com。但是,当我尝试访问子url链接时,它的工作方式不是:xdownloader.com/tiktok-video-downloader。请建议?子URL不起作用,因为没有文件:您使用的是laravel或类似的框架吗?如果不是,则必须装入一个名为“tiktok video downloader”的文件夹,并在此处插入index.phpfile@code-1不,我没有使用laravel。它是一个简单的核心php。你的意思是说我需要为每个URL创建index.php?我正在处理来自位于根文件夹上的单个index.php的所有请求。所以我只需要将每个请求url重写为index.php即可。
location / {
    try_files $uri $uri/ /index.php =404;
}