我有一个在核心PHP开发的静态网站(没有框架使用),我也安装了一个博客使用wordpress在同一个域下的子目录。
Like : example.com/blog
由于主站点是使用静态php文件构build的,因此按照SEO推荐,我写了一个简单的重写规则来从URL删除.php扩展名。
所以
http://example.com/abc.php will rewrite to http://example.com/abc
现在到了实际的问题。
当我尝试login到http://example.com/blog/wp-admin并把用户名和密码redirectwp-login.php,但由于上述重写规则它更改为wplogin,并导致找不到redirect404页面在主要站点。
重写规则Nginx的vhost-conf是:
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;
我知道这可能不是最好的方法,但我已经尝试了几乎几个小时stackexchange几乎所有的答案,最后得到这个使用上面的工作。
我的问题是,如何从这种redirect排除wp-admin? 否则有人可以build议任何替代良好的redirect/重写规则,以避免这种冲突?
完整的Nginxconfiguration:
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; } }
谢谢!
试试这个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; } }
但是,这种方法的问题是可能会有其他的副作用。 我build议你使用带有扩展名的原始.php文件,或者在自己原始的PHP文件中实现一个和Wordpress类似的友好URL系统。
或者甚至更好,在WP中实现你自己的PHP文件。
我自己正在研究这个问题 – 同时我发现我可以使用/blog/index.php/wp-admin/而不是/ blog / wp-admin /
这不是一个解决scheme,而是重新configuration过程中的一种姑息