Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Nginx] Enabling Search Engine Friendly URLs
#11
(06-22-2009, 02:03 PM)frostschutz Wrote: If you are using Nginx instead of Apache web server, this tutorial explains how you can enable MyBB's Search Engine Friendly SEF URLs. Apache users do not need to read this tutorial.

This tutorial does not explain how to install and set up Nginx with PHP from scratch; it is expected that you already have everything up and running, and now all that's left are the SEF URLs.

nginx.conf

First you should make a backup copy of your nginx.conf, so you can go back to your working config in case anything goes wrong.

In the server / location section, add these rewrite rules. Replace /MyBB/ with the directory you have MyBB installed. If your MyBB is installed in a top level domain, replace it with /, if it's in /forum replace it with /forum/ and so on.

Please note that every rewrite statement goes in a single line even if it wraps in your browser here.



Code:
location / {
   # ...your location settings are here...

   rewrite ^/MyBB/forum-([0-9]+)\.html$ /MyBB/forumdisplay.php?fid=$1;
   rewrite ^/MyBB/forum-([0-9]+)-page-([0-9]+)\.html$ /MyBB/forumdisplay.php?fid=$1&page=$2;
   rewrite ^/MyBB/thread-([0-9]+)\.html$ /MyBB/showthread.php?tid=$1;
   rewrite ^/MyBB/thread-([0-9]+)-page-([0-9]+)\.html$ /MyBB/showthread.php?tid=$1&page=$2;
   rewrite ^/MyBB/thread-([0-9]+)-lastpost\.html$ /MyBB/showthread.php?tid=$1&action=lastpost;
   rewrite ^/MyBB/thread-([0-9]+)-nextnewest\.html$ /MyBB/showthread.php?tid=$1&action=nextnewest;
   rewrite ^/MyBB/thread-([0-9]+)-nextoldest\.html$ /MyBB/showthread.php?tid=$1&action=nextoldest;
   rewrite ^/MyBB/thread-([0-9]+)-newpost\.html$ /MyBB/showthread.php?tid=$1&action=newpost;
   rewrite ^/MyBB/thread-([0-9]+)-post-([0-9]+)\.html$ /MyBB/showthread.php?tid=$1&pid=$2;

   rewrite ^/MyBB/post-([0-9]+)\.html$ /MyBB/showthread.php?pid=$1;

   rewrite ^/MyBB/announcement-([0-9]+)\.html$ /MyBB/announcements.php?aid=$1;

   rewrite ^/MyBB/user-([0-9]+)\.html$ /MyBB/member.php?action=profile&uid=$1;

   rewrite ^/MyBB/calendar-([0-9]+)\.html$ /MyBB/calendar.php?calendar=$1;
   rewrite ^/MyBB/calendar-([0-9]+)-year-([0-9]+)\.html$ /MyBB/calendar.php?action=yearview&calendar=$1&year=$2;
   rewrite ^/MyBB/calendar-([0-9]+)-year-([0-9]+)-month-([0-9]+)\.html$ /MyBB/calendar.php?calendar=$1&year=$2&month=$3;
   rewrite ^/MyBB/calendar-([0-9]+)-year-([0-9]+)-month-([0-9]+)-day-([0-9]+)\.html$ /MyBB/calendar.php?action=dayview&calendar=$1&year=$2&month=$3&day=$4;
   rewrite ^/MyBB/calendar-([0-9]+)-week-(n?[0-9]+)\.html$ /MyBB/calendar.php?action=weekview&calendar=$1&week=$2;

   rewrite ^/MyBB/event-([0-9]+)\.html$ /MyBB/calendar.php?action=event&eid=$1;
}

Restart Nginx so the changes can take effect.

MyBB settings

In Admin CP, go to 'Configuration' -> 'Server and Optimization options' and set 'Enable search engine friendly URLs?' to 'Enabled'.
(The auto detect option will not work with Nginx.)

Go back into your forum and test if the MyBB SEF URLs work.

Troubleshooting


  • Nginx refuses to start
    You probably made an error when editing the config file; Nginx should tell you where the error is, correct it.
  • I get 404 errors when clicking on thread / forum links
    Your rewrite rules are not working. Maybe you used the wrong path (in the example /MyBB/). Replace it with the correct path, for example / or /forum/. Also check if you installed Nginx with PCRE and HttpRewriteModule support.



Hi frost,
  • Please help, I get the error as you said:
Restart Nginx so the changes can take effect. =>> DONE
  • I get 404 errors when clicking on thread / forum linksYour rewrite rules are not working. Maybe you used the wrong path (in the example /MyBB/). Replace it with the correct path, for example / or /forum/.  => It's OK! I've checked it.
  • Also check if you installed Nginx with PCRE and HttpRewriteModule support. => It's OK too.
In Admin CP, go to 'Configuration' -> 'Server and Optimization options' and set 'Enable search engine friendly URLs?' to 'Enabled' => DONE

  • I am using VPS:
    • 512MB Ram

    • 20GB SSD Disk

    • CentOS 6.5 x64
I installed Centminmod.

When I using this:

Code:
       # GOOGLE SEO PLUGIN 1.6.8
       # Google SEO workaround for search.php highlights:

       # Make this rule the first rewrite rule in your .htaccess!
       rewrite ^/([^&]*)&(.*)$ http://windowstech.net/$1?$2 permanent;

       # Google SEO Sitemap:
       rewrite ^/((?i)sitemap-([^./]+)\.xml)$ /misc.php?google_seo_sitemap=$2;

       # Google SEO URL Forums:
       rewrite ^/((?i)Forum-([^./]+))$ /forumdisplay.php?google_seo_forum=$2;

       # Google SEO URL Threads:
       rewrite ^/((?i)Thread-([^./]+))$ /showthread.php?google_seo_thread=$2;

       # Google SEO URL Announcements:
       rewrite ^/((?i)Announcement-([^./]+))$ /announcements.php?google_seo_announcement=$2;

       # Google SEO URL Users:
       rewrite ^/((?i)User-([^./]+))$ /member.php?action=profile&google_seo_user=$2;

       # Google SEO URL Calendars:
       rewrite ^/((?i)Calendar-([^./]+))$ /calendar.php?google_seo_calendar=$2;

       # Google SEO URL Events:
       rewrite ^/((?i)Event-([^./]+))$ /calendar.php?action=event&google_seo_event=$2;

and click Apply in the Google SEO plugin, my site can rewrite URL as: domain.com/Thread-... or domain.com/Forum-... =>> OK
but when I using your rules:

Code:
    rewrite ^/forum-([0-9]+)\.html$ /forumdisplay.php?fid=$1;
   rewrite ^/forum-([0-9]+)-page-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2;
   rewrite ^/thread-([0-9]+)\.html$ /showthread.php?tid=$1;
   rewrite ^/thread-([0-9]+)-page-([0-9]+)\.html$ /showthread.php?tid=$1&page=$2;
   rewrite ^/thread-([0-9]+)-lastpost\.html$ /showthread.php?tid=$1&action=lastpost;
   rewrite ^/thread-([0-9]+)-nextnewest\.html$ /showthread.php?tid=$1&action=nextnewest;
   rewrite ^/thread-([0-9]+)-nextoldest\.html$ /showthread.php?tid=$1&action=nextoldest;
   rewrite ^/thread-([0-9]+)-newpost\.html$ /showthread.php?tid=$1&action=newpost;
   rewrite ^/thread-([0-9]+)-post-([0-9]+)\.html$ /showthread.php?tid=$1&pid=$2;
   rewrite ^/post-([0-9]+)\.html$ /showthread.php?pid=$1;
   rewrite ^/announcement-([0-9]+)\.html$ /announcements.php?aid=$1;
   rewrite ^/user-([0-9]+)\.html$ /member.php?action=profile&uid=$1;
   rewrite ^/calendar-([0-9]+)\.html$ /calendar.php?calendar=$1;
   rewrite ^/calendar-([0-9]+)-year-([0-9]+)\.html$ /calendar.php?action=yearview&calendar=$1&year=$2;
   rewrite ^/calendar-([0-9]+)-year-([0-9]+)-month-([0-9]+)\.html$ /calendar.php?calendar=$1&year=$2&month=$3;
   rewrite ^/calendar-([0-9]+)-year-([0-9]+)-month-([0-9]+)-day-([0-9]+)\.html$ /calendar.php?action=dayview&calendar=$1&year=$2&month=$3&day=$4;
   rewrite ^/calendar-([0-9]+)-week-(n?[0-9]+)\.html$ /calendar.php?action=weekview&calendar=$1&week=$2;
   rewrite ^/event-([0-9]+)\.html$ /calendar.php?action=event&eid=$1;

my site cannot rewrite, I get 404 Not Found - nginx Error.


Now I want rewrite URL like: domain.com/forum-123.html or domain.com/thread-123.html and the URL like: domain.com/Forum-... or domain.com/Thread-... can be redirected to the URL like: domain.com/forum-123.html or domain.com/thread-123.html 


Please help me to resolve this problem! Many thanks in advance!
Reply
#12
(06-22-2009, 02:03 PM)frostschutz Wrote: If you are using Nginx instead of Apache web server, this tutorial explains how you can enable MyBB's Search Engine Friendly SEF URLs. Apache users do not need to read this tutorial.

This tutorial does not explain how to install and set up Nginx with PHP from scratch; it is expected that you already have everything up and running, and now all that's left are the SEF URLs.

nginx.conf

First you should make a backup copy of your nginx.conf, so you can go back to your working config in case anything goes wrong.

In the server / location section, add these rewrite rules. Replace /MyBB/ with the directory you have MyBB installed. If your MyBB is installed in a top level domain, replace it with /, if it's in /forum replace it with /forum/ and so on.

Please note that every rewrite statement goes in a single line even if it wraps in your browser here.


Code:
location / {
   # ...your location settings are here...

   rewrite ^/MyBB/forum-([0-9]+)\.html$ /MyBB/forumdisplay.php?fid=$1;
   rewrite ^/MyBB/forum-([0-9]+)-page-([0-9]+)\.html$ /MyBB/forumdisplay.php?fid=$1&page=$2;
   rewrite ^/MyBB/thread-([0-9]+)\.html$ /MyBB/showthread.php?tid=$1;
   rewrite ^/MyBB/thread-([0-9]+)-page-([0-9]+)\.html$ /MyBB/showthread.php?tid=$1&page=$2;
   rewrite ^/MyBB/thread-([0-9]+)-lastpost\.html$ /MyBB/showthread.php?tid=$1&action=lastpost;
   rewrite ^/MyBB/thread-([0-9]+)-nextnewest\.html$ /MyBB/showthread.php?tid=$1&action=nextnewest;
   rewrite ^/MyBB/thread-([0-9]+)-nextoldest\.html$ /MyBB/showthread.php?tid=$1&action=nextoldest;
   rewrite ^/MyBB/thread-([0-9]+)-newpost\.html$ /MyBB/showthread.php?tid=$1&action=newpost;
   rewrite ^/MyBB/thread-([0-9]+)-post-([0-9]+)\.html$ /MyBB/showthread.php?tid=$1&pid=$2;

   rewrite ^/MyBB/post-([0-9]+)\.html$ /MyBB/showthread.php?pid=$1;

   rewrite ^/MyBB/announcement-([0-9]+)\.html$ /MyBB/announcements.php?aid=$1;

   rewrite ^/MyBB/user-([0-9]+)\.html$ /MyBB/member.php?action=profile&uid=$1;

   rewrite ^/MyBB/calendar-([0-9]+)\.html$ /MyBB/calendar.php?calendar=$1;
   rewrite ^/MyBB/calendar-([0-9]+)-year-([0-9]+)\.html$ /MyBB/calendar.php?action=yearview&calendar=$1&year=$2;
   rewrite ^/MyBB/calendar-([0-9]+)-year-([0-9]+)-month-([0-9]+)\.html$ /MyBB/calendar.php?calendar=$1&year=$2&month=$3;
   rewrite ^/MyBB/calendar-([0-9]+)-year-([0-9]+)-month-([0-9]+)-day-([0-9]+)\.html$ /MyBB/calendar.php?action=dayview&calendar=$1&year=$2&month=$3&day=$4;
   rewrite ^/MyBB/calendar-([0-9]+)-week-(n?[0-9]+)\.html$ /MyBB/calendar.php?action=weekview&calendar=$1&week=$2;

   rewrite ^/MyBB/event-([0-9]+)\.html$ /MyBB/calendar.php?action=event&eid=$1;
}

Restart Nginx so the changes can take effect.

MyBB settings

In Admin CP, go to 'Configuration' -> 'Server and Optimization options' and set 'Enable search engine friendly URLs?' to 'Enabled'.
(The auto detect option will not work with Nginx.)

Go back into your forum and test if the MyBB SEF URLs work.

Troubleshooting

  • Nginx refuses to start
    You probably made an error when editing the config file; Nginx should tell you where the error is, correct it.
  • I get 404 errors when clicking on thread / forum links
    Your rewrite rules are not working. Maybe you used the wrong path (in the example /MyBB/). Replace it with the correct path, for example / or /forum/. Also check if you installed Nginx with PCRE and HttpRewriteModule support.

In my nginx.conf, there is no location. Here is my nginx.conf:

Code:
#user  nobody;
worker_processes  2;


#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid  /usr/local/apps/nginx/var/log/nginx.pid;

events {
worker_connections  1024;
}

error_log /usr/local/apps/nginx/var/log/error_log debug;


http {

pagespeed on;
pagespeed FetchWithGzip on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
pagespeed EnableFilters rewrite_javascript;
pagespeed EnableFilters rewrite_css;
pagespeed EnableFilters flatten_css_imports;
pagespeed EnableFilters elide_attributes;
pagespeed EnableFilters combine_javascript;
pagespeed EnableFilters remove_comments;
pagespeed EnableFilters rewrite_images;
pagespeed EnableFilters defer_javascript;
pagespeed EnableFilters collapse_whitespace;
pagespeed EnableFilters lazyload_images;
include    mime.types;
default_type  application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
      '$status $body_bytes_sent "$http_referer" '
      '"$http_user_agent" "$http_x_forwarded_for"';

access_log /usr/local/apps/nginx/var/log/web.access.log  main;

sendfile  on;
#tcp_nopush  on;

#keepalive_timeout  0;
keepalive_timeout  65;

# enable gzip compression
gzip on;
   gzip_disable "msie6";

   gzip_comp_level 6;
   #gzip_comp_level 9;
   gzip_min_length  1100;
   gzip_buffers 16 8k;
   gzip_proxied any;
   gzip_types       text/plain application/ecmascript application/postscript text/csv application/xml text/css text/x-c text/js text/xml application/javascript application/x-javascript text/javascript application/json application/xml+rss;
# end gzip configuration

client_max_body_size 200M;

# If your domain names are long, increase this parameter.
server_names_hash_bucket_size 64;

include /usr/local/apps/nginx/etc/conf.d/*.conf;
}

When i click the thread link, i am going forum homepage.

How i fix Google SEO problem?
Reply
#13
Hi,

What is in your "/usr/local/apps/nginx/etc/conf.d/" directory? There should be at least one configuration file setting up a vhost to serve, even if its just the default vhost.
Reply
#14
(12-07-2014, 10:42 PM)Euan T Wrote: Hi,

What is in your "/usr/local/apps/nginx/etc/conf.d/" directory? There should be at least one configuration file setting up a vhost to serve, even if its just the default vhost.

webuzoVH.conf, common, 50x.html, 500.html

Where is problem?

Thanks for answer.
Reply
#15
What is in the webuzoVH.conf file?
Reply
#16
I added these two lines to make Archive mode work:
Code:
rewrite ^/forum/archive/index.php/forum-([0-9]+)\.html$ /forum/archive/index.php?forum-$1.html;
rewrite ^/forum/archive/index.php/thread-([0-9]+)\.html$ /forum/archive/index.php?thread-$1.html;

My entire config looks like this (and it works flawlessly with MyBB):
Code:
server {
        server_name
                www.mysite.com
        ;

        listen 80 default;
        listen 443 ssl;
        
        ssl_certificate ssl/mysite.com.crt;
        ssl_certificate_key ssl/mysite.com.key;
        
        return 301 https://mysite.com$request_uri;
}

server {
        server_name
                mysite.com
        ;

        listen 443 ssl default;

        ssl_certificate ssl/mysite.com.crt;
        ssl_certificate_key ssl/mysite.com.key;

        root /home/nginx/mysite.com/public;
        index index.html index.php;

        access_log /home/nginx/mysite.com/logs/access.log;
        error_log /home/nginx/mysite.com/logs/error.log;

        include php.conf;
        include mime.types;

        location /forum/ {
                rewrite ^/forum/forum-([0-9]+)\.html$ /forum/forumdisplay.php?fid=$1;
                rewrite ^/forum/forum-([0-9]+)-page-([0-9]+)\.html$ /forum/forumdisplay.php?fid=$1&page=$2;
                rewrite ^/forum/thread-([0-9]+)\.html$ /forum/showthread.php?tid=$1;
                rewrite ^/forum/thread-([0-9]+)-page-([0-9]+)\.html$ /forum/showthread.php?tid=$1&page=$2;
                rewrite ^/forum/thread-([0-9]+)-lastpost\.html$ /forum/showthread.php?tid=$1&action=lastpost;
                rewrite ^/forum/thread-([0-9]+)-nextnewest\.html$ /forum/showthread.php?tid=$1&action=nextnewest;
                rewrite ^/forum/thread-([0-9]+)-nextoldest\.html$ /forum/showthread.php?tid=$1&action=nextoldest;
                rewrite ^/forum/thread-([0-9]+)-newpost\.html$ /forum/showthread.php?tid=$1&action=newpost;
                rewrite ^/forum/thread-([0-9]+)-post-([0-9]+)\.html$ /forum/showthread.php?tid=$1&pid=$2;
                rewrite ^/forum/post-([0-9]+)\.html$ /forum/showthread.php?pid=$1;
                rewrite ^/forum/announcement-([0-9]+)\.html$ /forum/announcements.php?aid=$1;
                rewrite ^/forum/user-([0-9]+)\.html$ /forum/member.php?action=profile&uid=$1;
                rewrite ^/forum/calendar-([0-9]+)\.html$ /forum/calendar.php?calendar=$1;
                rewrite ^/forum/calendar-([0-9]+)-year-([0-9]+)\.html$ /forum/calendar.php?action=yearview&calendar=$1&year=$2;
                rewrite ^/forum/calendar-([0-9]+)-year-([0-9]+)-month-([0-9]+)\.html$ /forum/calendar.php?calendar=$1&year=$2&month=$3;
                rewrite ^/forum/calendar-([0-9]+)-year-([0-9]+)-month-([0-9]+)-day-([0-9]+)\.html$ /forum/calendar.php?action=dayview&calendar=$1&year=$2&month=$3&day=$4;
                rewrite ^/forum/calendar-([0-9]+)-week-(n?[0-9]+)\.html$ /forum/calendar.php?action=weekview&calendar=$1&week=$2;
                rewrite ^/forum/event-([0-9]+)\.html$ /forum/calendar.php?action=event&eid=$1;
                rewrite ^/forum/archive/index.php/forum-([0-9]+)\.html$ /forum/archive/index.php?forum-$1.html;
                rewrite ^/forum/archive/index.php/thread-([0-9]+)\.html$ /forum/archive/index.php?thread-$1.html;
        }
        location ~ /forum/(inc) {
                deny all;
        }
}
Reply
#17
Just in case anyone gets this far, you've activated the "Enable search engine friendly URLs in Archive?" option and find that pagination on the archives aren't working, you can ADD the following lines:

Code:
rewrite ^/forums/archive/index.php/forum-([0-9]+)-([0-9]+)\.html$ /forums/archive/index.php?forum-$1-$2.html;
rewrite ^/forums/archive/index.php/thread-([0-9]+)-([0-9]+)\.html$ /forums/archive/index.php?thread-$1-$2.html;

The full set of mybb seo friendly rewrite rules for nginx should look like this:

Code:
rewrite ^/forums/forum-([0-9]+)\.html$ /forums/forumdisplay.php?fid=$1;
rewrite ^/forums/forum-([0-9]+)-page-([0-9]+)\.html$ /forums/forumdisplay.php?fid=$1&page=$2;
rewrite ^/forums/thread-([0-9]+)\.html$ /forums/showthread.php?tid=$1;
rewrite ^/forums/thread-([0-9]+)-page-([0-9]+)\.html$ /forums/showthread.php?tid=$1&page=$2;
rewrite ^/forums/thread-([0-9]+)-lastpost\.html$ /forums/showthread.php?tid=$1&action=lastpost;
rewrite ^/forums/thread-([0-9]+)-nextnewest\.html$ /forums/showthread.php?tid=$1&action=nextnewest;
rewrite ^/forums/thread-([0-9]+)-nextoldest\.html$ /forums/showthread.php?tid=$1&action=nextoldest;
rewrite ^/forums/thread-([0-9]+)-newpost\.html$ /forums/showthread.php?tid=$1&action=newpost;
rewrite ^/forums/thread-([0-9]+)-post-([0-9]+)\.html$ /forums/showthread.php?tid=$1&pid=$2;
rewrite ^/forums/post-([0-9]+)\.html$ /forums/showthread.php?pid=$1;
rewrite ^/forums/announcement-([0-9]+)\.html$ /forums/announcements.php?aid=$1;
rewrite ^/forums/user-([0-9]+)\.html$ /forums/member.php?action=profile&uid=$1;
rewrite ^/forums/archive/index.php/forum-([0-9]+)\.html$ /forums/archive/index.php?forum-$1.html;
rewrite ^/forums/archive/index.php/forum-([0-9]+)-([0-9]+)\.html$ /forums/archive/index.php?forum-$1-$2.html;
rewrite ^/forums/archive/index.php/thread-([0-9]+)\.html$ /forums/archive/index.php?thread-$1.html;
rewrite ^/forums/archive/index.php/thread-([0-9]+)-([0-9]+)\.html$ /forums/archive/index.php?thread-$1-$2.html;

I've attached this as a .txt file for ease.


Attached Files
.txt   mybb-rewrite-rules.txt (Size: 1.43 KB / Downloads: 48)
Reply
#18
This user has been denied support. This user has been denied support.
FYI, those archive URLs aren't rewrites, they're PATH_INFO, which can be set up to work with nginx, but it's tricky. You find many nginx+PATH_INFO guides with Google but most of them have issues. So if you don't need general PATH_INFO support and those rewrites work for you...

http://kbeezie.com/php-self-path-nginx/
Reply
#19
Guys I followed the tutorial but SEO friendly URL are not working. My site is in root folder. The site is still showing old style URLS for e.g.

http://www.mysite.com/forumdisplay.php?fid=7

I have enabled all options in Google SEO section as shown in screenshot below.
[Image: ei9lsI1.jpg]


Following screenshot shows setting for .htaccess file but since I am using nginx hence I ignored it.

[Image: S7gQIM2.jpg]

Finally have I added these lines in location section in /etc/nginx/sites-available/mysite



Code:
server {
        listen 80;
        listen [::]:80;
        root /var/www/mysite/html;
        index index.php index.html index.htm;
        server_name www.mysite.com mysite.com;

        location / {
                try_files $uri $uri/ = 404;
        }

        location ~ \.php$ {
    
               rewrite ^/forum-([0-9]+)\.html$ /forumdisplay.php?fid=$1;
                rewrite ^/forum-([0-9]+)-page-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2;
               #rest of the rules are defined in file but not shown here
   
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
        }

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

Whether I add those lines in first location section or second or last one it doesn't matter. Also restarted nginx.

How to fix this issue?
Reply
#20
This user has been denied support. This user has been denied support.
"URL is passive. Apply changes to core files to activate" "Click to Apply changes to core files." You have to click the Apply link in there.

If the URLs don't show up in the first place, it's a problem with Google SEO setup. If the URL show up but don't actually work, it's a problem with your rewrites.

Note this thread isn't about Google SEO at all. It's about MyBB's forum-123.html URLs. There is an nginx example included in the Google SEO package.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)