0

I want to retrict access to my whole wordpress except for the homepage and one other page. The problem I found is that the other page is accessible but not the homepage.

My wordpress installation is behind an nginx reverse proxy

location /wordpress/ {
    proxy_pass              http://192.168.1.12/;
    proxy_set_header      X-Forwarded-For $remote_addr;
    proxy_set_header      X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-Proto       $scheme;
}

On another nginx server

server {
listen       80 default_server;
server_name  192.168.1.12;

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

access_log      /var/log/nginx/default.access.log;
error_log       /var/log/nginx/default.error.log;

## WordPress Perm links config ##
location / {
try_files $uri $uri/ /index.php?$args;
}

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_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location ~ /\.ht {
deny all;
}
}

The wordpress wp-config.php was modified for ssl

if ( (!empty( $_SERVER['HTTP_X_FORWARDED_HOST'])) ||
     (!empty( $_SERVER['HTTP_X_FORWARDED_FOR'])) ) {
    $_SERVER['HTTPS'] = 'on';
}

The theme's function.php was modified to restrict access

function restrict_access_if_logged_out(){
  global $wp;
  if (!is_user_logged_in() && !is_home() &&  ($wp->query_vars['pagename'] != 'portail-identification') && ($wp->query_vars['pagename'] != 'portail-stagiaire') ){
       wp_safe_redirect(wp_login_url(get_permalink()));
       exit;
  }
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );

Also created same question on https://stackoverflow.com/questions/53157349/restrict-access-if-logged-out-except-for-wordpress-homepage

0

Is your "home page" displaying a list of the site's blog posts, or is it set to display a specific page? If the latter, you need to use is_front_page() instead of is_home().

  • 1
    Thank you for the specification! The "portail-identification" is the page I want it to show. So is_front_page() was the answer! – Carobell Nov 5 '18 at 15:42

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.