1

I have a static website developed in core PHP (No framework used) and I have also installed a blog using wordpress in a subdirectory under same domain.

Like : example.com/blog

Since the main site is built using static php files, as per SEO recommendation, I have written a simple rewrite rule to remove .php extension from urls.

So

http://example.com/abc.php will rewrite to http://example.com/abc

Now coming to actual problem.

When i try to login to http://example.com/blog/wp-admin and put username and password it redirects wp-login.php but due to above rewrite rule it changes to wp-login and causes redirect 404 page not found on main site.

Rewrite rule Nginx vhost- conf is:

location ~ \.php$ {
    if ($request_uri ~ (.*)\.php$) {
        return 301 $1;
    }

    try_files $uri =404;

    expires off;

    fastcgi_read_timeout 900s;
    fastcgi_index index.php;
    fastcgi_intercept_errors on;
    fastcgi_pass 127.0.0.1:9001;

I know it may not be the best way to do this but i have tried almost all the answers on stackexchange for hours and finally get this working using above.

My question is that how can i exclude wp-admin from such redirection? or else someone can suggest any alternate good redirect/rewrite rule to avoid such collision?

Complete Nginx Config:

server {
    listen 80; # Default listen port
    if ($host = www.example.com) {
        rewrite ^/(.*)$ http://example.com/$1 permanent;
    }
    server_name example.com www.example.com;
    root  /home/example.com/public_html/;
    index index.php index.html;
    access_log /var/log/nginx/skpat77-access.log;
    error_log /var/log/nginx/skpat77-error.log;
    gzip on; # Turn on gZip
    gzip_static on;
    gzip_comp_level 9;
    gzip_min_length  1400;
    gzip_vary  on;
    gzip_types  text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    try_files $uri $uri/ /index.php?$args;


    #error 500 502 503
    error_page 500 502 503 /500x.html;
    location = /500x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        if ($request_uri ~ (.*)\.php$) {
            return 301 $1;
        }

        try_files $uri =404;

        expires off;

        fastcgi_read_timeout 900s;
        fastcgi_index index.php;
        fastcgi_intercept_errors on;
        fastcgi_pass 127.0.0.1:9001;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }

    location /blog/ {
        index index.php index.html index.htm;
        try_files $uri $uri/ /blog/index.php;
    }
}

Thanks!

  • What is your complete nginx configuration? – Tero Kilkanen Aug 4 '16 at 11:19
  • @Tero- I have updated Nginx config in question. – Sachin Singh Aug 4 '16 at 12:35
  • Still not able to find a better solutions for this. – Sachin Singh Aug 5 '16 at 7:26
1

Try this configuration:

server {
    # Handle redirects to www.example.com
    server_name example.com;
    return 301 http://www.example.com$uri;
}

server {
    listen 80; # Default listen port
    server_name www.example.com;
    root  /home/example.com/public_html/;
    index index.php index.html;
    access_log /var/log/nginx/skpat77-access.log;
    error_log /var/log/nginx/skpat77-error.log;
    gzip on; # Turn on gZip
    gzip_static on;
    gzip_comp_level 9;
    gzip_min_length  1400;
    gzip_vary  on;
    gzip_types  text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    # Add .php extension to list of files to go through before trying WP
    try_files $uri $uri.php $uri/ /index.php?$args;


    #error 500 502 503
    error_page 500 502 503 /500x.html;
    location = /500x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        if ($request_uri ~ (.*)\.php$) {
            return 301 $1;
        }

        try_files $uri =404;

        expires off;

        fastcgi_read_timeout 900s;
        fastcgi_index index.php;
        fastcgi_intercept_errors on;
        fastcgi_pass 127.0.0.1:9001;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }

    location /blog/ {
        index index.php index.html index.htm;
        try_files $uri $uri.php $uri/ /blog/index.php;
    }
}

However, the problem with this approach is that there can be other side-effects. I recommend that you use the raw .php files with the extension, or implement a similar friendly URL system as in Wordpress in your own raw PHP files.

Or even better, implement your own PHP files in WP.

0

I am looking into this issue myself - in the meantime I have found that I can login using /blog/index.php/wp-admin/ rather than /blog/wp-admin/

This is not intended as a solution, rather a palliative during the reconfig process

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.