简体   繁体   中英

removing .html extensions and redirecting them in Wordpress with NGINX and wp super cache

I have oldsite.com which is redirecting to newsite.com. The site is run on Wordpress, NGINX and I am using wp supercache. I am having an issue where I am unable to get single page redirects working for html extensions. I have tried quite a solutions and nothing seems to be taking. so an example would be

oldsite.com/about.html redirects to -> newsite.com.about/

I have tried quite a few solutions with no luck. any help is appreciated. cheers

here is my config

fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";


            server {
                # Base info
                    listen 80 default_server;
                    listen [::]:80 default_server ipv6only=on;

                    root /var/www;
                    index index.php index.html index.htm;

                # Make site accessible from http://localhost/
                    server_name newsite.com;

                # Browser caching
                    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
                        expires 365d;
                    }

                    location ~*  \.(pdf)$ {
                        expires 30d;
                    }

                    location ~* \.(htm|html)$ {
                        expires 30d;
                    }

                # Nginx Error Handling  
                    error_page 404 /404.html;
                    error_page 500 502 503 504 /50x.html;
                    location = /50x.html {
                    root /var/www;
                    }

                # Base server side caching  
                    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;
                        fastcgi_cache MYAPP;
                        fastcgi_cache_valid 200 60m;
                    }

                # Security
                    # Block download agent
                         if ($http_user_agent ~* LWP::Simple|wget|libwww-perl) {
                                return 403;
                         }

                    # Block some nasty robots
                         if ($http_user_agent ~ (msnbot|Purebot|Baiduspider|Lipperhey|Mail.Ru|scrapbot) ) {
                                return 403;
                         }

                    # Deny referal spam
                         if ( $http_referer ~* (jewelry|viagra|...) ) {
                                return 403; 
                         }

                    # Prevent hotlinking
                    location ~ .(gif|png|jpe?g)$ {
                         valid_referers none blocked newsite.com *.newsite.com;
                         if ($invalid_referer) {
                                return   403;
                        }
                    }

                # WP Super Cache rules.
                # Based on http://codex.wordpress.org/Nginx
                # Designed to be included from a 'wordpress-ms-...' configuration file.

                    set $cache_uri $request_uri;

                    # POST requests and urls with a query string should always go to PHP
                        if ($request_method = POST) {
                                set $cache_uri 'null cache';
                        }

                        if ($query_string != "") {
                                set $cache_uri 'null cache';
                        }   

                    # Don't cache uris containing the following segments
                        if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
                                set $cache_uri 'null cache';
                        }   

                    # Don't use the cache for logged in users or recent commenters
                        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
                                set $cache_uri 'null cache';
                        }

                        if (!-e $request_filename) {
                            rewrite ^(.+)$ /index.php?q=$1 last;
                        }

                    # Use cached or actual file if they exists, otherwise pass request to WordPress
                        location / {
                             try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php?$args;
                        }

            }

            #redirects
            server {
                    server_name 000.000.000.00;
                    return 301 $scheme://newsite.com$request_uri;
            }

            server {
                    server_name oldsite.com;
                    return 301 $scheme://newsite.com$request_uri;  
            }

            server {
                    server_name www.oldsite.com;
                    return 301 $scheme://newsite.com$request_uri; 
            }

May be this is a solution to your answer. Please edit your .htaccess file and insert following code

  # Redirect to HTML if it exists.
  # e.g. example.com/foo will display the contents of example.com/foo.html
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}.html -f
  RewriteRule ^(.+)$ $1.html [L,QSA]

For details visit the link Github

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM