Add slash and cut extension of file (nginx rules)
Ad
I tried to figure out how to add "/" to the end of every url. And this url need to be without extension. For example:
example.com/about.php; example.com/about.html => example.com/about/
I'm dummy in configuration nginx rewrite rules. This is my config:
server{
root /usr/share/nginx/www/example.com;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name example.com www.example.com;
client_max_body_size 20M;
# BEGIN W3TC Browser Cache
gzip on;
gzip_types text/css text/x-component application/x-javascript application/json application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text$;
# END W3TC Browser Cache
location /blog {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
# try_files $uri $uri/ /index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~* ^.+\.(rss|atom|jpg|jpeg|gif|png|ico|rtf|js|css|json)$ {
expires max;
}
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;
}
location / {
try_files $uri $uri.html $uri/ @extensionless-php;
index index.php index.html index.htm;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
}
I founded some solutions but i can't to combine it, because I'm noob in nginx.
Ad
Answer
Ad
You need to change part of your config like that
location / {
rewrite ^(/.*[^/])\.(html|php)$ $1/ permanent;
rewrite ^(/.*[^/])(?!/)$ $1/ permanent;
try_files $uri $uri.html $uri/ @extensionless-php;
index index.php;
}
location @extensionless-php {
rewrite ^(.*)/$ $1.php last;
}
First line rewrite ^(/.*[^/])\.(html|php)$ $1/ permanent;
will redirect all requests with extensions .html or .php
rewrite ^(/.*[^/])(?!/)$ $1/ permanent;
Will add / to requests, which uri end without /
Ad
source: stackoverflow.com
Related Questions
Ad
- → How to give access execute specific php file in server?
- → 301 Redirection from no-www to www in wordpress
- → JS file not being served over HTTPS
- → Redirect 301 htaccess prestashop
- → Add slash and cut extension of file (nginx rules)
- → Laravel group route issue
- → .htaccess affect other file url
- → creating sef links and changing plus sign (+) with dash (-)
- → How i can redirect from "www.mywebsite.com////" to "www.mywebsite.com"?
- → Cannot get my multiple regex working for specific case in URL structure
- → get htaccess to ignore part of a Url
- → .htaccess seo friendly url not found issue
- → how to redirect old url with parameters to new seo url using htaccess
Ad