0

I need a help to configure my website on nginx server. Earlier i was using apache so there i was using htaccess to redirect on index.php. But i just got to know that nginx don't support htaccess. I am new in nginx server. Let me make it more clear with example.

When i was using apache then my htaccess was like below-

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ index.php [L]
</IfModule>

I need to do the same thing in nginx server now. Please anyone suggest me how i can do this. I tried to figure it out myself and tried many thing but nothing works eg-

I tried this-

location / {
    # Check if a file or directory index file exists, else route it to index.php.
    try_files $uri $uri/ /index.php;
}

I also tried the solution suggest here: How to enable URL rewrite in Ubuntu, nGinx and codeigniter?

location ~* \.php$ {
     fastcgi_pass unix:/run/php/php5.6-fpm.sock;
     root /var/www/html/;
     try_files $uri =404;
     fastcgi_index  index.php;
     fastcgi_param   SCRIPT_FILENAME $request_filename;
     include fastcgi_params;
     fastcgi_buffer_size 128k;
     fastcgi_buffers 256 4k;
     fastcgi_busy_buffers_size 256k;
     fastcgi_temp_file_write_size 256k;
}

My requirement is to rewrite all these urls :

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

To

https://example.com/index.php

My Folder Structure- folder-structer.png

Thanks in advance.

1 Answers1

0

If you want to rewrite only (not redirect) when the file doesn't exist you should use "try_files", for example:

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

Then nginx will first test if the request maps to a file, then if it maps to a directory containing an index file, then look for index.php in the document root. It will resolve the request according to what it found. If there were no matches, it will return a 404.

symcbean
  • 47,736
  • 6
  • 59
  • 94
code-a1
  • 71
  • 1
  • 5
  • Hey @code-a1, Thanks for your reply. Yes I want to rewrite the URL not redirect sorry for confussion. I have updated my question. I was also tried your solution but it's not seems to be working. Any idea what I am making wrong? – Lyricx Mint Apr 03 '21 at 05:56
  • @LyricxMint the file index.php exist and you are editing the correct file? Unlike apache, you don't just need to create an .htaccess file in the directory – code-a1 Apr 03 '21 at 12:41
  • @code-1 index.php file is there. I am making all the changes in my /etc/nginx/sites-available/default file In fact my base url is working fine see this here thexdownloader.com. But when i am trying access sub url link it not working like: thexdownloader.com/tiktok-video-downloader. Please suggest? – Lyricx Mint Apr 04 '21 at 15:40
  • sub urls don't work because there aren't the files: are you using laravel or similar frameworks? If not you must crate, for example, a folder named "tiktok-video-downloader" and insert here the index.php file – code-a1 Apr 04 '21 at 15:59
  • @code-1 No i am not using laravel. Its a simple core php. What are you saying you mean to say i need to create index.php for every urls? I am handling all my request from a single index.php which is located on the root folder. So i just need to rewrite every request url to that index.php only. – Lyricx Mint Apr 04 '21 at 17:31
  • I have updated my question by attaching the folder structure. Please see – Lyricx Mint Apr 04 '21 at 17:37
  • `location / { try_files $uri $uri/ /index.php?$query_string; } error_page 404 /index.php;` – code-a1 Apr 04 '21 at 20:51
  • try this if you get the page uri in your index.php file with get – code-a1 Apr 04 '21 at 20:52