wordpress - 使用 Nginx 重写仅在 root 上从 URL 中删除 index.php

标签 wordpress url-rewriting seo nginx ipb

我使用 Wordpress 作为我网站的根目录,使用 Invision Power Boards 作为论坛。

http://localhost -> Wordpress
http://localhost/forum -> IPB

我已经使用 Nginx-rewrite 成功地从 Wordpress URL 中删除了“index.php”,但是当我尝试在 IPB 上使用 SEO 友好 URL 时,nginx 只是返回到 Wordpress 的 404 页面。

我的配置是这样的:

#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/

#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 错误页面。

我对正则表达式知之甚少,因此不胜感激。


这是我修改后的整个 conf 文件,有点乱我接受。

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 永久链接。


编辑 - 解决方案

    # 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,现在正在配置,我将 wordpress 永久链接添加到 ipb 配置并打开 seo 重写 URL,强制友好 URL

location / {
try_files $uri $uri/ /index.php?$args;
}

对我有用

关于wordpress - 使用 Nginx 重写仅在 root 上从 URL 中删除 index.php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10591258/

相关文章:

php - 在 php 站点中重写,斜线被省略?

类似于 Wordpress 的 PHP 短 URL

Ajax 网站,历史和 Seo 问题

seo - 多个谷歌网站管理员帐户是否会对单个网站造成伤害?

python - 如何在 wagtail 站点地图上将协议(protocol)更改为 https?

php - 如何覆盖自定义提要的 WP Admin 联合提要限制?

wordpress - 如何防止 WordPress 从 URL 查询字符串中剥离 "at"符号 (@)?

php - Wordpress 不允许 PHP 错误

apache - 使用 .htaccess 从子域的目录中获取内容

php - 在 Woocommerce 中,如何获取每个变体旁边的价格?