我有一个wordpress网站,只有一个私人页面(只能由登录用户查看)
我在主题函数中使用以下代码。每次将页面URL放在浏览器上时,php都会重定向到登录页面。
add_action( \'wp\', \'redirect_private_page_to_login\' );
function redirect_private_page_to_login(){
$queried_object = get_queried_object();
if (
isset( $queried_object->post_status ) &&
\'publish\' === $queried_object->post_status &&
! is_user_logged_in()
) {
wp_safe_redirect( wp_login_url( get_permalink( $queried_object->ID ) ) );
exit;
}
}
当我将页面URL放在浏览器上时
示例:https://website.com/page1 我应该被重定向到:
https://website.com/wp-login.php?redirect_to=https%3A%2F%2Fwebsite.com%2Fpage1%2F
它可以工作,但是
Sometimes 我被重定向到
https://website.com/index.php . Edge和safari浏览器经常出现这种情况
(that is my issue).
我认为页面重定向有问题。可能是我的.htaccess . 我想和你分享,希望有人能帮我。
Note: 我使用SecuPress作为安全插件。我也有一个子域名,为什么你会发现测试。网站中的com。htaccess
HTACCESS
# BEGIN SecuPress no_x_powered_by
<IfModule mod_headers.c>
Header unset X-Powered-By
</IfModule>
# END SecuPress
# BEGIN SecuPress readme_discloses
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*/)?(readme|changelog|debug)\\.(txt|md|html|log)$ - [R=404,L,NC]
</IfModule>
# END SecuPress
# BEGIN SecuPress wp_version
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^readme\\.html$ - [R=404,L,NC]
</IfModule>
# END SecuPress
# BEGIN SecuPress bad_url_access
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !wp-includes/js/tinymce/wp-tinymce\\.php$
RewriteRule ^(php\\.ini|wp-config\\.php|wp-includes/.+\\.php|wp-admin/(admin-functions|install|menu-header|setup-config|([^/]+/)?menu|upgrade-functions|includes/.+)\\.php)$ [R=404,L,NC]
</IfModule>
# END SecuPress
# BEGIN SecuPress directory_listing
<IfModule mod_autoindex.c>
Options -Indexes
</IfModule>
# END SecuPress
# BEGIN SecuPress php_disclosure
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} \\=PHP[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} [NC]
RewriteRule .* - [F]
</IfModule>
# END SecuPress
# BEGIN SecuPress bad_file_extensions
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^wp-content/uploads/.*\\.(\\.9|73i87a|386|aaa|abc|aepl|aru|atm|aut|bat|bhx|bin|bkd|blf|bll|bmw|boo|bps|bqf|breaking_bad|btc|buk|bup|bxz|ccc|ce0|ceo|cfxxe|chm|cih|cla|cmd|com|coverton|cpl|crinf|crjoker|crypt|crypted|cryptolocker|cryptowall|ctbl|cxq|cyw|czvxce|darkness|dbd|delf|dev|dlb|dli|dll|dllx|dom|drv|dx|dxz|dyv|dyz|ecc|enciphered|encrypt|encrypted|enigma|exe1|exe_renamed|exx|ezt|ezz|fag|fjl|fnr|fuj|fun|good|gzquar|ha3|hlp|hlw|hsq|hts|iva|iws|jar|kcd|kernel_complete|kernel_pid|kernel_time|keybtc@inbox_com|kimcilware|kkk|kraken|lechiffre|let|lik|lkh|lnk|locked|locky|lok|lol!|lpaq5|magic|mfu|micro|mjg|mjz|nls|oar|ocx|osa|ozd|p5tkjw|pcx|pdcr|pgm|php|php2|php3|pid|pif|plc|poar2w|pr|pzdc|qit|qrn|r5a|rdm|rhk|rna|rokku|rrk|rsc_tmp|s7p|scr|scr|shs|ska|smm|smtmp|sop|spam|ssy|surprise|sys|tko|tps|tsa|tti|ttt|txs|upa|uzy|vb|vba|vbe|vbs|vbx|vexe|vxd|vzr|wlpginstall|wmf|ws|wsc|wsf|wsh|wss|xdu|xir|xlm|xlv|xnt|xnxx|xtbl|xxx|xyz|zix|zvz|zzz)$ - [R=404,L,NC]
</IfModule>
# END SecuPress
# BEGIN SecuPress hotlink
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} \\.(jpg|jpeg|png|gif)$ [NC]
</IfModule>
# END SecuPress
# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Protect wp-config.php
<files wp-config.php>
order allow,deny
deny from all
</files>
# Redirection to HTTPS
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
RewriteCond %{HTTP_REFERER} !^http://test.website.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://test.website.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://website.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://website.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.test.website.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.test.website.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.website.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.website.com$ [NC]
RewriteCond %{HTTP_REFERER} !^https://test.website.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^https://test.website.com$ [NC]
RewriteCond %{HTTP_REFERER} !^https://website.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^https://website.com$ [NC]
RewriteCond %{HTTP_REFERER} !^https://www.test.website.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^https://www.test.website.com$ [NC]
RewriteCond %{HTTP_REFERER} !^https://www.website.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^https://www.website.com$ [NC]
RewriteRule .*\\.(jpg|jpeg|gif|png|bmp)$ - [F,NC]
# BEGIN MINIORANGE MEDIA RESTRICTION
# The directives (lines) between `BEGIN MINIORANGE MEDIA RESTRICTION` and `END MINIORANGE MEDIA RESTRICTION` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
RewriteCond %{REQUEST_FILENAME} ^.*(png|jpg|pdf|doc|docx|xlsx|xls|mp4|ppt|pptx|mov|mpeg|avi|m4v|svg|wmv|bmp)$ [OR]
RewriteCond %{REQUEST_URI} protectedfiles
RewriteCond %{HTTP_COOKIE} !^.*wordpress_logged_in.*$ [NC]
RewriteRule . - [R=403,L]
# END MINIORANGE MEDIA RESTRICTION
# BEGIN cPanel-generated php ini directives, do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor)
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI)
<IfModule php7_module>
php_value upload_max_filesize 25M
php_value post_max_size 128M
php_flag display_errors Off
php_value max_execution_time 2000
php_value max_input_time 600
php_value max_input_vars 4000
php_value memory_limit 512M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php74"
php_flag zlib.output_compression Off
</IfModule>
<IfModule lsapi_module>
php_value upload_max_filesize 25M
php_value post_max_size 128M
php_flag display_errors Off
php_value max_execution_time 2000
php_value max_input_time 600
php_value max_input_vars 4000
php_value memory_limit 512M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php74"
php_flag zlib.output_compression Off
</IfModule>
# END cPanel-generated php ini directives, do not edit
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
提前感谢您的帮助。
当做
最合适的回答,由SO网友:MrWhite 整理而成
# Redirection to HTTPS
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
这条规则放错地方了。它需要在
# BEGIN WordPress
注释标记。即,在WordPress前控制器之前。
目前,这将导致重定向到/index.php
如果请求HTTP。
# BEGIN SecuPress hotlink
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} \\.(jpg|jpeg|png|gif)$ [NC]
</IfModule>
# END SecuPress
并且该块不完整/错误。除非您知道缺少的指令,否则必须删除此块。
幸运的是,它是;仅限“;做下一个RewriteRule
(在WordPress代码块中)多余,但它可能会破坏您的网站。