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;
}
Related
I am trying to convert .htaccess file to a nginx server config.
There is default client side site which is served from - /frontend/public - this works fine.
There is admin CMS side site which is served from - /backend/public - this doesn't work
If I go to /cms then I get redirected to /cms/login which is correct. Nginx displays 404 error though which is not correct.
If I remove login requirement and go to /cms then it displays the web page as expected, but url changes from /cms to /backend/public.
I am not sure what I got wrong with the /cms and /backend/public in the nginx config.
nginx config:
server {
listen 80;
root /home/anon/web/project;
index index.php index.html index.htm index.nginx-debian.html;
server_name project.test;
location ~ ^/cms$ {
rewrite ^(.*)$ /cms/ redirect;
}
location /cms {
rewrite ^/cms(.*)$ /backend/public/$1 last;
}
location /backend/public {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php;
}
}
location / {
if (!-e $request_filename) {
rewrite ^/(.*)/$ /$1 redirect;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /frontend/public/$1 break;
}
rewrite ^/[a-zA-Z0-9_/\-'"$]*$ /frontend/public/index.php last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_buffering off;
}
}
.htaccess file I am trying to convert into nginx config:
Options -Indexes
RewriteEngine on
RedirectMatch 301 ^/cms$ /cms/
RewriteRule ^cms(.*)$ backend/public/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/frontend/public/%{REQUEST_URI} -f
RewriteRule ^(.+)$ frontend/public/$1 [L]
RewriteRule ^[a-zA-Z0-9_/\-'"$]*$ frontend/public/index.php [L]
I'm trying rewrite my nginx to use http://domain.com/main instead of using http://domain.com/?page=main
I've done:
try_files $uri $uri/ /index.php?page=$uri;
Returns blank page
rewrite ^/(\w+)$ /index.php?page=$1 break;
rewrite ^/(\w+)+\/$ /index.php?page=$1 break;
if ($http_host !~ "^$"){
rewrite ^(.*)$ http%1://www.$http_host$request_uri redirect;
}
Generates the url: http://domain.com/main/http://domain.com/main/http://domain.com/main
Here is the Apache .htaccess:
RewriteEngine on
RewriteRule ^(\w+)$ index.php?page=$1 [L,NC,QSA]
RewriteRule ^(\w+)+\/$ index.php?page=$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I am assuming that you have a working configuration for URIs of the form: http://domain.com/?page=main
The try_files $uri $uri/ /index.php?page=$uri; may not work as expected as it will generate:
http://domain.com/?page=/main
Note the extra /. So you may need to use a rewrite to extract part of the URI not including the leading /. For example:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/(.*)$ /index.php?page=$1 last;
}
location \.php$ { ... }
Note the last suffix rather than the break suffix, as the target URI needs to be processed in another location. See this document for details.
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 have this .htaccess rewrite rules
AddDefaultCharset UTF-8
RewriteEngine On
RewriteRule ^(administrator) - [L]
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
and the nginx version rewrite rules
charset utf-8;
rewrite ^/$ /public/ last;
rewrite /(.*) /public/$1 last;
location ~* ^/(administrator) {
break;
}
The current .htaccess redirect all requests to /public folder except /administrator request.
With the nginx rules the [domain.tld/rss.php not working] [domain.tld/administrator working] [domain.tld working] [File not found.]
My application stracture is
.
..
index.php [require public/index.php]
administrator/index.php
public/index.php
public/rss.php
public/css
public/js
Sepcifiy an index file : index index.php.
By the way you should put rewrite in locations to avoid the test for every request. It's also better for readability/maitainability.
server {
server_name domain.tld;
root /path/to/root;
location ~ /(administrator|public) {
index index.php;
...
}
location / {
rewrite ^/$ /public/ last;
rewrite ^(.*)$ /public/$1 last;
}
}
I want to convert a simple .htaccess file (rules) to be used with nginx. I try to access an online tool after googleit but that website is down. So, if anyone cand help, i would appreciate. Thank you. Here is my .htaccess file:
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
# we skip all files with .something
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Please, try these rules:
# Deny all files that started with dot
location ~ /\. {
deny all;
}
# Try $uri - is file, $uri/ - is dir, index.html - caching, $uri.html caching, index.php -last
location / {
try_files $uri $uri/ index.html $uri.html index.php;
}
Please, try these rules:
# nginx configuration
location ~ \.html$ {
}
location / {
if ($request_uri ~ "\..+$"){
rewrite ^/$ /index.html;
}
rewrite ^/([^.]+)$ /$1.html;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php break;
}
}