NGINX Rewrite help needed

hey!

I have 2 questions for nginx users.

1) I'm trying to setup my joomla server onto my new linode running NGINX and after much (like days) of searching and testing, I finally have a config that works with with SEF url plugins…sorta. I was using an apache system on the old server and it used modrewrite and life was fine in terms of SEF. Since NGINX doesn't have modrewrite, I found something that works BUT it constantly leaves index.php in the urls.

ex: http://mysite.com/index.php/forum

i want it to be just http://mysite.com/forum but without mod_rewrite it doesn't seem to be possible in joomla that i'm aware of. I know in wordpress it IS possible but I have to use a plugin.

Here is my config file:

server {
  listen 80;
  server_name mysite.com www.mysite.com;

            access_log /home/public_html/mysite.com/log/access.log;
            error_log /home/public_html/mysite.com/log/error.log;
root   /home/public_html/mysite.com/public/;

  large_client_header_buffers 4 8k; # prevent some 400 errors

  index           index.php index.html;
  fastcgi_index   index.php;

  location / {
    expires 30d;
    error_page 404 = @joomla;
    log_not_found off;
  }
# Rewrite
location @joomla {
  rewrite ^(.*)$ /index.php?q=last;
}

# Static Files
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
  access_log        off;
  expires           30d;
}
  # PHP
  location ~ \.php {
    keepalive_timeout 0;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include /usr/local/nginx/conf/fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /home/public_html/mysite.com/public/$fastcgi_script_name;
}
}

2) second question should be easy but i can't get it to work.

I want to use the same config I posted above and have either mysite.com or www.mysite.com both forward to mysite.com/portal.

Basically when you hit up the front page with or without the www, it all gets forwarded to a sub directory on the server I called Portal.

I have tried several variations of:

 rewrite ^/(.*) http://www.example.com/portal/$1 permanent;

but it usually ends with firefox telling me there is some crazy loop happening the address bar saying something like mysite.com/portalportalportalportalportal………on and on.

So, any help on either of these issues would be awesome!!

Thanks!!

13 Replies

1) Can you provide the mod_rewrite rules and I'll try and convert them for you.

2)

Try this

rewrite  / /portal permanent;

that should rewrite just / which is the root of the site but it won't rewrite /index.php or /whatever.

awesome thanks :)

########## Begin - Rewrite rules to block out some common exploits
## If you experience problems on your site block out the operations listed below
## This attempts to block the most common type of exploit `attempts` to Joomla!
#
## Deny access to extension xml files (uncomment out to activate)
# <files ~="" "\.xml$"="">#Order allow,deny
#Deny from all
#Satisfy all
#</files>
## End of deny access to extension xml files
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
# Block out any script trying to base64_encode crap to send via URL
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
# Block out any script that includes a 

the rewrite rule didn't work :(

seems like it should be fairly easy to have mysite.com and www.mysite.com both point to the same sub-directory :(

alas it's beyond me.

Are you using apache or nginx?

You want something like:

rewrite ^/(.*) http://www.example.com/portal/$1 permanent;

except with nginx you want to put that in an "if" block and with apache you want to add a rewritecond

Basically, you only want to do that rewrite if the host part of the request is not already www.example.com or the request uri doesn't already start with portal

otherwise, you'll redirect the visitor every they make a request (infinite loop which apparently firefox detected)

yup thats what i want. I'm using nginx so what would be the full bit of code including the if statement.

well, this isn't ideal because potentially it could be 2 redirects, but:

if ($host != www.example.com) {
    rewrite ^/(.*)$ http://www.example.com/$1 permanent;
}
if ($request_uri !~ ^/portal/.*$) {
    rewrite ^/(.*)$ http://www.example.com/portal/$1 permanent;
}

untested but that should work.

didn't work :(

www.example.com takes me to the firefox error page talking about a loop.

example.com takes me to the default "Welcome to nginx!" page.

neither of them redirected to /portal :(

This bit:

########## Begin - Joomla! core SEF Section 
# 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !^/index.php 
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC] 
RewriteRule (.*) index.php 

Should translate into:

if (!-e $request_filename)
{
set $redirect 'y';
}

if ($request_uri ~ (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$)
{
set $redirect $redirect'y';
} 

if ($request_uri ~ ^/index.php)
{
set $redirect '';
}

if ($redirect = 'yy')
{
    rewrite (.*) /index.php;
}

As for your portal problem, does the /portal directory actually exist and if so what's it's index file?

i will try those rules out and get back to you. thanks :D

yes, portal does actually exist. portal is actually where my joomla installation sits (as opposed to my root) so index.php is the main file in there there.

Ohhh well that explains why you're rules aren't working quite right.

What's in the root directory?

in the root is nothing like an index file which is why it gives me a 403 Forbidden error :P I'm debating putting an index.php file with a php 301 redirect in there as a temporary solution to this.

but yeah…nothing in root except a few audio files

Try this for your entire config I've added comments about what does what

server { 
    listen 80;
    server_name mysite.com www.mysite.com;
    access_log /home/public_html/mysite.com/log/access.log; 
    error_log /home/public_html/mysite.com/log/error.log; 
    root   /home/public_html/mysite.com/public/; 

      large_client_header_buffers 4 8k; # prevent some 400 errors 

      index           index.php index.html; 
      fastcgi_index   index.php; 

    #redirect mysite.com and www.mysite.com to /portal
    location = / {
    rewrite (.*) /portal last;
    }

    #pass php files to fastcgi
    location ~ \.php$
    {
                fastcgi_pass 127.0.0.1:9000; 
                fastcgi_index index.php; 
                include /usr/local/nginx/conf/fastcgi_params; 
                fastcgi_param SCRIPT_FILENAME /home/public_html/mysite.com/public/$fastcgi_script_name; 
        #set expires to 0 to stop caching of php files
        expires 0;
    }

    #files in the /portal dir
    location ~ ^/portal
    {
    #set expires to 30d to cache static files
    expires 30d;
    #file doesn't exist, will need to be sent to php
    if (!-e $request_filename) 
    { 
    rewrite ^/portal/(.*)$ /index.php?q=$1 last; 
    } 
    }
} 

I've not tested it (since I don't have joomla) but it "should" work. If it doesn't then I'd have to have a look at your setup to get it to work.

:D all the redirects seem to work perfectly!!!! thank you!!

now i gotta sort out how to make my SEF urls work properly but i think i need someone with joomla background to do that. Basicly, I think i have to trick joomla into think mod_rewrite is there so it takes index.php out of the urls like it does for wordpress.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct