使用 Nginx 重写仅在 root 上从 URL 中删除 index.php [英] Remove index.php from URL only on root with Nginx rewrite
问题描述
我使用 Wordpress 作为我网站的根目录,并使用 Invision Power Boards 作为论坛.
I'm using Wordpress as root of my website and Invision Power Boards as forum.
http://localhost -> Wordpress
http://localhost/forum -> IPB
我已经使用 Nginx 重写成功地从 Wordpress URL 中删除了index.php",但是当我尝试在 IPB 上使用 SEO 友好 URL 时,nginx 只是返回到 Wordpress 的 404 页面.
I have removed "index.php" from Wordpress URLs successfully with Nginx-rewrite however when I try to use SEO Friendly URLs on IPB, nginx simply returns to Wordpress' 404 page.
我的配置是这样的:
#This removes "index.php" from Wordpress URLs
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
然后我按照此链接修改我的 nginx conf 文件,以便能够使用 IPB 的 SEO 友好 URL:http://www.devcu.com/forums/topic/262-furl-friendly-urls-with-ipb-and-nginx/
Then I follow this link to modify my nginx conf file in order to be able to use SEO friendly URLs of IPB: http://www.devcu.com/forums/topic/262-furl-friendly-urls-with-ipb-and-nginx/
#This part is to be able to use IPB SEO
location /forum/ {
index index.php;
try_files $uri $uri/ /forum/index.php?$uri&$args;
rewrite ^ /index.php? last;
}
当我点击论坛上的链接 (例如:http://localhost/forum/index.php/forum/51-sport/)
nginx 只是将我重定向到 (http://localhost/forum/forum/51-sport/)
显示 Wordpress 404 错误页面.
When I click a link on my forum (for example: http://localhost/forum/index.php/forum/51-sport/)
nginx simply redirects me to (http://localhost/forum/forum/51-sport/)
which displays Wordpress 404 error page.
我对正则表达式知之甚少,因此不胜感激.
I have very little knowledge about regex so any help would be appreciated.
这是我修改后的整个 conf 文件,有点乱我接受.
This is my whole conf file after modifications, little messy I accept that.
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /home/user_name/public_html;
access_log /var/log/nginx/a/access.log;
error_log /var/log/nginx/a/error.log
server_name localhost;
server_tokens off;
location / {
try_files $uri $uri/ @wordpress;
}
location @wordpress {
fastcgi_pass php-fpm;
fastcgi_param SCRIPT_FILENAME /home/user_name/public_html$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_NAME /index.php;
}
location /forum {
try_files $uri $uri/ try_files $uri $uri/ /forum/index.php?q=$uri;
}
location /forum/ {
try_files $uri $uri/ try_files $uri $uri/ /forum/index.php?q=$uri;
}
#location / {
#index index.php index.html index.htm;
#try_files $uri $uri/ /index.php?q=$uri&$args;
#}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ .php$ {
fastcgi_split_path_info ^(/)(/.*)$;
}
# Add trailing slash to */wp-admin and */forum requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9001
#location ~ .php$ {
# fastcgi_split_path_info ^(/)(/.*)$;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /home/user_name/public_html$fastcgi_script_name;
# fastcgi_param PATH_INFO $fastcgi_script_name;
# include /etc/nginx/fastcgi_params;
#REMOVE THIS
#fastcgi_read_timeout 60000;
#fastcgi_send_timeout 6000;
#}
}
自从上一篇文章以来,我一直在使用 IPB 的 SEO 配置,并设法从 URL 中删除了index.php".当然不影响结果.但似乎 location/
决定了要做什么,因此链接被视为 Wordpress 永久链接.
Since the last post, I have played with IPB's SEO configurations and I managed to remove "index.php" from URLs. It doesn't effect the result of course. But it seems that location /
decides what to do and therefore link is being considered as a Wordpress permalink.
编辑 - 解决方案
# Upstream to abstract backend connection(s) for php
upstream php {
# server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9001;
}
server {
## Your website name goes here.
server_name localhost;
## Your only path reference.
root /home/username/public_html;
## This should be in your http block and if it is, it's not needed here.
index index.php;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# This is cool because no php is touched for static content
try_files $uri $uri/ /index.php;
}
location /forum {
try_files $uri $uri/ /forum/index.php;
rewrite ^ /forum/index.php? break;
}
location ~ ^/forum/index.php {
if ($args != "") {
rewrite ^ http://www.google.com/ permanent;
}
try_files $uri $uri/ /forum/index.php;
rewrite ^ /forum/index.php? last;
}
location /forum/admin/ {
try_files $uri $uri/ /forum/admin/index.php;
rewrite ^ /forum/admin/index.php? last;
}
location ~ .php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include /etc/nginx/fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
推荐答案
我也在 nginx 上使用 wordpress 和 ipb,现在正在配置,我在 ipb 配置中添加了 wordpress 永久链接并打开 seo Rewrite URLs, Force Friendly URLs>
I'm too using wordpress and ipb on nginx, now configuring, i added wordpress permalink to ipb config and turn on seo Rewrite URLs, Force Friendly URLs
location / {
try_files $uri $uri/ /index.php?$args;
}
它对我有用
这篇关于使用 Nginx 重写仅在 root 上从 URL 中删除 index.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!