I'm trying to change the way pagination looks like, from: example.com/category?page=1 to example.com/category/page1/
For these purpose in .htaccess I was using this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((.*)/page([0-9]+)/)$ index.php?q=$2&page=$3 [L,QSA]
But I don't understand how to do it in nginx, I tried this:
location / {
if (!-e $request_filename){
rewrite ^/((.*)/page([0-9]+)/)$ /index.php?q=$2&page=$3 break;
}
}
And it doesn't work, it just breaks everything (getting 404 at categories)
Just use this
location / {
if (!-e $request_filename){
rewrite ^/((.*)/page([0-9]+)/)$ /index.php;
}
}
Related
I am trying to get a few .htaccess rules converted to NGINX, but it's not going very well. I have to crawl here and beg for assistance :(~ Paypal-beer to anyone who solves it.
I have tried some online converters, but the code looks messy, and I don't quite understand what it's doing. The rules I would like to have converted:
# Rewrite any calls to html|json|xml|atom|rss
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+)\.(html|json|xml|atom|rss)$ $1/ [L]
# Rewrite any calls to /render to the image resizer
RewriteCond %{REQUEST_URI} render/
RewriteRule ^render/. app/parsers/slir/ [L]
# Rewrite routes to application index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?/$1/ [L,QSA]
Appreciate any help.
I believe it is sorted. Inside a NGINX server {} block, I included the following:
location / {
if (!-e $request_filename){
# Rewrite any calls to html|json|xml|atom|rss if a folder matching * exists
rewrite (.+)\.(html|json|xml|atom|rss)$ $1/ last;
# Rewrite any calls to /render to the X3 image resizer
rewrite ^/render/. /app/parsers/slir/ last;
# Rewrite routes to X3 application index.php if they are non-existent files/dirs
rewrite ^(.*)$ /index.php?$1 last;
}
}
https://gist.github.com/mjau-mjau/6dc1948284c90d167f51f1e566a8457b
I'm brand new to nginx and I am trying to convert the app I wrote over from Apache as I need the ability to serve a lot of clients at once without a lot of overhead!
I'm getting the hang of setting up nginx and FPM/FastCGI PHP but I can't wrap my head around nginx's rewrite format just yet. I know you have to write some simple script that goes in the server {} block in the nginx config but I'm not yet familiar with the syntax.
Could anyone with experience with both Apache and nginx help me convert this to nginx format? Thanks in advance!
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^school\.dev$ [NC]
RewriteRule ^(.*)$ http://www.school.dev/$1 [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [NE,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
Here is the nginx conversion for your htaccess.
# nginx configuration
location / {
if ($http_host ~* "^school\.dev$"){
rewrite ^(.*)$ http://www.school.dev/$1 redirect;
}
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?$query_string break;
}
}
Question is about translating RazorCMS (razorcms.co.uk) rewrite rule but I think answer can be general.
I have two .htacces files. First is in main directory (/). I translated:
RewriteRule ^(.*)$ index.php?path=$1 [L,QSA]
to
location / { try_files $uri $uri/ #rewrite; }
location #rewrite { rewrite ^/(.*)$ /index.php?path=$1; }
There is no problem with this one.
Second .htacces file is in subdirectory (/rars).
Content inside is:
RewriteRule ^login/u/(.*)/p/(.*)$ index.php?login=1&u=$1&p=$2 [L,QSA]
RewriteRule ^login index.php?login=1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?path=$1 [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L,QSA]
I tried to translate it to different rules:
location /login { rewrite ^/login/u/(.*)/p/(.*)$ /index.php?login=1&u=$1&p=$2 break; }
location /login { rewrite ^/login /index.php?login=1 break; }
location /rars { if (!-e $request_filename){ rewrite ^(.*)$ /index.php?path=$1; } }
...but it doesn't work.
How can I translate .htaccess rules to Nginx rewrites if second .htaccess is in subdirectory? Can you point me in right direction?
Nginx doesn't have per directory contexts, so you need to give the full path in the location. Try:
location /rars/login { rewrite ^/rars/login/u/(.*)/p/(.*)$ /rars/index.php?login=1&u=$1&p=$2 break; }
location /rars/login { rewrite ^/rars/login$ /rars/index.php?login=1 break; }
location /rars { if (!-e $request_filename){ rewrite ^/rars/(.*)$ /rars/index.php?path=$1; } }
I'm attempting to convert the following (PHPVibe) .htaccess rule to gninx:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^embed/([^/]*)/$ /embed.php?id=$1 [L]
RewriteRule ^feed(.*)$ feed.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?rp=$1 [L]
</IfModule>
<ifModule mod_headers.c>
Header set Access-Control-Allow-Origin: *
</ifModule>
This is what I have so far:
rewrite ^/embed/([^/]*)/$ /embed.php?id=$1 last;
rewrite ^/feed(.*)$ /feed.php last;
if (!-f $request_filename){
set $rule_2 1$rule_2;
}
if (!-d $request_filename){
set $rule_2 2$rule_2;
}
if ($rule_2 = "21"){
rewrite ^/(.*)/?$ /index.php?rp=$1 last;
}
The above worked for the most part, except for the fact that it is returning 404 on my non mod_rewrite dependent pages.
Try inserting the following code before your other RewriteRules
RewriteRule ^moderator - [NC]
It should cause the rewrite engine to skip over any URLs that begin with /moderator/
location / {
try_files $uri $uri/ #index;
}
location /embed/ {
rewrite ^/embed/([^/]+)/$ /embed.php?id=$1;
}
location /feed {
rewrite ^ /feed.php;
}
location #index {
rewrite ^/(.*)/?$ /index.php?rp=$1;
}
location ~ \.php$ {
# fastcgi directives
}
So, after some tweaking, I've finally figured out the proper rewrite directives for nginx. In case anyone is wondering.
rewrite ^/embed/([^/]*)/$ /embed.php?id=$1 last;
rewrite ^/feed(.*)$ /feed.php last;
if (!-e $request_filename)
{
rewrite ^/(.*)/?$ /index.php?rp=$1 last;
}
So im trying to move an application from apache to NginX but I've discovered i need to re write the .htaccess file, I've had a good look around but finally I need to ask for some help.
Below is the .htaccess file from apache, im trying to redirect all requests to index.php except for requests to /html, /css, /images, /php.
Thanks for your help in advance!
Bill
RewriteEngine on
RewriteRule ^html [L,NC]
RewriteRule ^css [L,NC]
RewriteRule ^images [L,NC]
RewriteRule ^php [L,NC]
RewriteRule ^.*$ index.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !directory/(.*)$
RewriteCond %{REQUEST_URI} !(.*)$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
So far I have the below and the requests for the html, css, images and php files are working great.
But when i make a request for www.domain.com/blah/blah/blah/ im getting a 404, what i really want is for the URL to reamin as www.domain.com/blah/blah/blah/ but load the index.php file
location ~ directory/(.*)$ {
}
location ~ (.*)$ {
}
location /html {
rewrite ^/html / break;
}
location /css {
rewrite ^/css / break;
}
location /images {
rewrite ^/images / break;
}
location /php {
rewrite ^/php / break;
}
location / {
rewrite ^(.*)$ /index.php break;
if (!-e $request_filename){
rewrite ^(.*)$ http://www.domain.com/$1 redirect;
}
}
Try something along these lines for filtering requests to content in specific folders:
RewriteEngine On
# if not requesting content in one of the specified folders
RewriteCond %{REQUEST_FILENAME} !^(html|css|images|php)/ [NC]
# redirect visitor to the index page
RewriteRule ^(.*)$ index.php [L,QSA]
The exclamation mark ( ! ) before the rewrite condition means a negation, so if the requested filename DOES NOT start with html or css or ...., apply the rewrite rule.