Remove "/Public/index.php" from url with htaccess - php

I created a mvc painting that works nginx perfectly with this configuration:
location / {
root html/Public;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME C:/nginx/html/Public/$fastcgi_script_name;
include fastcgi_params;
}
}
I need an htaccess for cpanel that removes the /Public/index.php but maintains the integrity of the folders inside Public for example /Template/image/slide.jpg

Related

Nginx + Laravel - Moving blog from subdomain to /blog

My server application uses Laravel, and I have until recently had a wordpress blog hosted at blog.server.com
I wish to change this to server.com/blog
I have updated the main nginx config by adding the following:
location ^~ /blog {
root /home/ploi/blog.server.com/public;
index index.php index.html index.htm;
try_files $uri $uri/ /blog/index.php?$query_string;
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_buffers 16 16k;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
This kind of works. It loads the new /blog url and posts, but:
No static assets get loaded (js, png, css, etc)
server.com/blog/wp-admin/ loops infinitely
How can I solve this?
You could try this, worked for me:
location /blog {
alias /home/ploi/blog.server.com/public;
try_files $uri $uri/ #blogrewrite;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
location #blogrewrite {
rewrite /blog/(.*)$ /blog/index.php?/$1 last;
}
I used a "named location" (#blogrewrite), but frankly I don't know why this was necessary, but it avoided the recursion.

Not working Bootstrap site on nginx + php-fpm

There are next folders and files in my site-directory:
|-css/
|-js/
|-fonts/
|-index.php
I see my index.php without styles
nginx conf file:
server {
listen 80;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name xx.xx.xxx.xxx;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass phpfpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
How I need to write my static locations in nginx?
server {
listen 80;
root /var/www/phpMyAdmin;
index index.php index.html index.htm;
server_name phpmyadmin.dev;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
If your static directories is in the same directory as index.php you can add them like
<link rel="shortcut icon" href="/css/style.css">
or
<link rel="shortcut icon" href="your_domain/css/style.css">
If you want a special directory for these files you can add a location to your nginx configuration. And you can add a custom cache policy for them.
server {
listen 80;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name xx.xx.xxx.xxx;
location / {
try_files $uri /index.php$is_args$args;
}
// You can add a custom location for a directory
location /css {
root different_root_for_css;
}
// You can add a custom location for file types
location ~* \.(js|jsx|jpg|jpeg|png|gif|ico|css)$ {
// root path for these files
root different_root_for_these_files;
//Custom cache policy
expires 365d;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass phpfpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
You can check nginx resources for more information.
Another method; some websites use subdomains for static files. This way if you plan to use a cdn provider, you can change your subdomain dns and it will work same.

Nginx location configuration (subfolders)

lets say I've a path like:
/var/www/myside/
that path contains two folders... let's say
/static and /manage
I'd like to configure nginx to have an access to:
/static folder on / (eg. http://example.org/)
this folder has some .html files.
/manage folder on /manage (eg. http://example.org/manage) in this case this folder contains Slim's PHP framework code - that means the index.php file is in public subfolder (eg. /var/www/mysite/manage/public/index.php)
I've tried a lot of combinations such as
server {
listen 80;
server_name example.org;
error_log /usr/local/etc/nginx/logs/mysite/error.log;
access_log /usr/local/etc/nginx/logs/mysite/access.log;
root /var/www/mysite;
location /manage {
root $uri/manage/public;
try_files $uri /index.php$is_args$args;
}
location / {
root $uri/static/;
index index.html;
}
location ~ \.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
}
}
The / works correctly anyway manage doesn't. Am I doing something wrong? Does anybody know what should I change?
Matthew.
To access a path like /var/www/mysite/manage/public with a URI like /manage, you will need to use alias rather than root. See this document for details.
I am assuming that you need to run PHP from both roots, in which case you will need two location ~ \.php blocks, see example below. If you have no PHP within /var/www/mysite/static, you can delete the unused location block.
For example:
server {
listen 80;
server_name example.org;
error_log /usr/local/etc/nginx/logs/mysite/error.log;
access_log /usr/local/etc/nginx/logs/mysite/access.log;
root /var/www/mysite/static;
index index.html;
location / {
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
location ^~ /manage {
alias /var/www/mysite/manage/public;
index index.php;
if (!-e $request_filename) { rewrite ^ /manage/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
}
The ^~ modifier causes the prefix location to take precedence over regular expression locations at the same level. See this document for details.
The alias and try_files directives are not together due to this long standing bug.
Be aware of this caution in the use of the if directive.

Setting up Wordpress and Rails in Nginx

I have nginx set up for Rails application that has been working beautifully for me. Now I want to move my Wordpress blog from blog.website.com to website.com/blog, so web crawlers treat it as part of the site.
I created a symbolic link in my Rails app's public/ directory and added the following to my nginx configuration:
# Rails server
server {
root /project/path/current/public;
server_name project.com;
passenger_enabled on;
rails_env production;
client_max_body_size 20m;
if ($http_x_forwarded_proto != 'https') {
return 301 https://$host$request_uri;
}
location /blog {
index index.html index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
if (!-e $request_filename) {
rewrite ^.+?(/blog/.*.php)$ $1 last;
rewrite ^ /blog/index.php last;
}
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
This works great.
But what I'd like to do is skip the symbolic link part. I'd like nginx to route directly to the wordpress directory. I tried changing the /blog block to:
location ^~ /blog {
root /home/ubuntu/apps/blog;
index index.html index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
if (!-e $request_filename) {
rewrite ^.+?(/blog/.*.php)$ $1 last;
rewrite ^ /blog/index.php last;
}
}
This works, as in I'm being correctly redirected to the wordpress folder but my server doesn't know what to do with php files and sends them to my browser as files to download.
I tried nesting the \.php$ location inside the /blog location but it results in 404 error.
What's wrong with the following piece and how to fix it?
location ^~ /blog {
root /home/ubuntu/apps/blog;
index index.html index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
if (!-e $request_filename) {
rewrite ^.+?(/blog/.*.php)$ $1 last;
rewrite ^ /blog/index.php last;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
I haven't fixed the snippet from the question but I managed to solve the problem with nginx configuration without using a symbolic link by using two server blocks.
server {
location / {
proxy_pass http://localhost:82/;
}
location /blog {
root /var/proj/blog/current;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
# Rails server
server {
listen 82;
root /var/proj/site/current/public;
server_name site.com;
passenger_enabled on;
rails_env staging;
client_max_body_size 20m;
# other configuration
}

Clean URLs Nginx Rewrites

After searching the interweb for hours and none of the "guides" working I turn to you guys and girls for help.
I am attempting to clean all my urls for all files in any directory so www.foo.com/foo.php becomes www.foo.com/foo.
Also I want to tidy up requests such as www.foo.com/foo.php?foo=foo to www.foo.com/foo/foo
Here is my current failed attempt:
server {
# Change these settings to match your machine
listen 80 default_server;
server_name example.com www.example.com;
# Everything below here doesn't need to be changed
access_log /var/log/nginx/example.access.log;
error_log /var/log/nginx/example.error.log;
root /var/www/example.com;
try_files $uri $uri/ #extensionless-php;
index index.html index.htm index.php;
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
# The next two lines should go in your fastcgi_params
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
}
It works but I can still access the old .php url aswell which as far as im aware is bad for SEO? as its duplicate content?
Thanks in Advance
Danny

Categories