I have been trying to rewrite my php pages so that they display without their extensions in the browser. For example, a link on my server at www.example.com loads a page called about-us.php and in the browser, the user would see www.example.com/about-us.
I have been trying to follow these tutorials to try and get this to work, but sadly they are not working.
Tweak Talk Link
Stack Overflow Link
Here is my nginx sites-available/default file content.
# Server Block
server {
# Listening Ports
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
# Root Location Initialization
root /home/zach/Documents/Workspaces/www;
index index.php index.html index.htm;
# Server Name
server_name localhost;
# Root Location
location / {
try_files $uri $uri/ #extensionless-php;
}
# Error pages
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /home/zach/Documents/Workspaces/www;
}
# PHP Handling
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Extensionless PHP
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
}
All of my links to php pages still display the .php extension in the browser, does anyone have any solutions to my problem?
Add the following line to the exact spot where you think it'd form a loop! :-)
if ($request_uri ~ ^/([^?]*)\.php($|\?)) { return 302 /$1?$args; }
More info here: nginx remove .php and .html file extension
Related
I'm trying to change from plain http://sitename/?p=123 to http://sitename/postname with rewrite rule but it gives error 404 Not found. This is my nginx server block code:
server {
listen 80;
listen 443 ssl;
root /var/www/sitename/html;
index index.php index.html index.htm;
server_name sitename www.sitename;
client_max_body_size 10M;
# Certificates handled by CertBot
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
server_name sitename www.sitename;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ /index.php$is_args$args;
# rewrite ^ http://$server_name$request_uri permanent;
rewrite ^/(.*)$ http://$server_name/$1 permanent;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
# root /data/www;
# index index.html index.htm;
}
}
I want to use rewrite so I redirect https of front-end to http and wp-admin and wp-login as https which is handled by Wordpress.
How can I fix it?
Created a separate similar server block with 443 listen. Included the cert lines and removed the location from 443 block.
I have a wordpress site with the permalinks set to post name. All the pages do not appear and return a 404 except for the index. If I set this to plain, then they appear, but many resources are missing, and I'm not sure why.
So just to be clear, if permalink is plain, it works but then there are many errors as there is a 404 on multiple files (except on the index).
If permalink is postname then the entire page is a 404.
Here is my htaccess file, my nginx config, and my php-fpm config.
Can anyone see the problem?
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
NGINX CONF
server {
listen 80;
server_name www.example.com example.com;
root /var/www/example;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name www.example.com example.com;
root /var/www/example;
index index.php;
ssl_certificate /etc/nginx/ssl/example.com/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com/example.com.key;
access_log /var/log/nginx/example.log main;
error_log /var/log/nginx/example_error.log;
location / {
root /var/www/example;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
PHP-FPM
[example]
listen = 127.0.0.1:9001
listen.allowed_clients = 127.0.0.1
user = nginx
group = nginx
...and some more I don't think is relevant but I can supply if asked for.
Thank you for all your help!
Without seeing the actual error or which files are 404'ing, I think you may be missing try_files in your nginx config. I'd check out this:
http://nginxlibrary.com/wordpress-permalinks/
Also, nginx doesn't need or want .htaccess:
https://www.nginx.com/resources/wiki/start/topics/examples/likeapache-htaccess/
Also, I'd check to make sure your URL are set correctly in your DB.
(I'd post the link but I don't have the rep)
I'd use wp-cli to do a search-replace. More than once I've forgotten to do this moving from dev to staging and had a wtf moment. :)
Hopefully this all helps!
* Edited to help a bit more *
After looking more into those tutorials, they leave out some finer points. Without going to far into it, try using something closer to this:
listen 443 ssl;
root /var/www/example;
index index.php index.html index.htm;
server_name www.example.com example.com;
ssl_certificate /etc/nginx/ssl/example.com/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com/example.com.key;
access_log /var/log/nginx/example.log main;
error_log /var/log/nginx/example_error.log;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
This is a mix of what you have and the configuration I run. Let me know how it does for you.
I installed October CMS on my Nginx server, installed fine. Front end works with index.php and the style is correct. However backend does not. It just gives me a 404 page error. I've followed everything on the docs correctly, tried different sites-avaliable config files and they don't seem to work but give a 502, which I've checked the logs too and nothing in there.
This is my one3.com config file:
server {
listen 80;
root /storage/www/one3community.com;
index index.php index.html index.htm;
listen 443;
ssl on;
ssl_certificate /ssl_keys/one3community.com/public.pem;
ssl_certificate_key /ssl_keys/one3community.com/private.pem;
# Make site accessible from http://localhost/
server_name www.one3community.com;
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
index index.php;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php5.5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Converted default apache .htaccess file to Nginx. Loaded into sites-available .conf file restarted nginx. Now up and running.
** Had to replace every break with last in nginx file otherwise causes file download **
I am trying to deploy my Codeigniter 3 HMVC website in nginx (AWS). I have tried all the solutions I found in SO but none of them worked. It is been 6 hours now. Please someone help m e out here.
I have installed nginx and tested a "Hello World" in a PHP file and it works fine.
(All the static files in assets/ folder are served in the browser. Only the routing seems to cause the issue for other urls)
This is the configs in my /etc/nginx/sites-enabled/blahblah.lk
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/blah/public/;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name blahblah.lk;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php;
}
location ~* ~/(assets|files|robots\.txt){}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/html;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
If nginx is running properly, it might be a codeigniter setup issue.
Check the rewrite rules and make sure your controllers have uppercase-first filenames, while the controller subfolders are all-lowercase. That kept me from going for hours.
So while following this tutorial to implement two website on my NginX server.
Lin to the Website
I created two folder as follow
sudo nano /etc/nginx/sites-available/ideconnect.com
my file look like this
server {
listen 80;
listen [::]:80;
root /var/www/ideconnect.com/html;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name ide-portal.com www.ide-portal.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Then the other website
sudo nano /etc/nginx/sites-available/iclock.com
server {
listen 80 ;
listen [::]:80;
root /var/www/iclock.com/html;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name iclock.in www.iclock.in;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Also I have created the SYMLINKS properly
sudo ln -s /etc/nginx/sites-available/ideconnect.com /etc/nginx/sites-enabled/ideconnect.com
sudo ln -s /etc/nginx/sites-available/iclock.com /etc/nginx/sites-enabled/iclock.com
After this when I got to my ip address I can only see the default Nginx Page also now the info.php configuration page is also not visible.
Any help would be appreciated
Thanks
There are two things you can check to begin with:
Did you restart nginx?
Are you sure your nginx configuration is set up to include .com files? I think the default is to include all .conf files. You can check that in your nginx configuration file and then adapt that or modify the extension of your symlinks.