Resolved .htaccess equivalent commands of Nginx configuartion

costas1

Member
Messages
146
Reaction score
4
Points
18
As I'm not familiar with Nginx server, does anyone have an idea of what would be the equivalent of the following commands:

location rest.php/ {
try_files $uri $uri/ /rest.php?$query_string;
}


for an .htaccess file?
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
252
Points
63
x10 doesn't use Nginx and is .htaccess compatible. Is this a question related to your account?
 

spacresx

Community Advocate
Community Support
Messages
2,203
Reaction score
196
Points
63
@garrettroyce
I think what they are asking is what is the apache equivalent
to nginx's code of

location rest.php/ {
try_files $uri $uri/ /rest.php?$query_string;
}


like what would they use for apache to do the same thing.
ive never used nginx before so i have no clue what they
would use in htaccess
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
252
Points
63
Yep. I read that wrong. My apologies.

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /rest.php?%{QUERY_STRING} [L]

Here's a summary of the processing:
* If a file matching the request does not exist, continue processing (else serve that file)
* If a directory matching the request does not exist, continue processing (else serve that directory)
* If the request matches the expression ^ (literally matches anything), then rewrite the request as /rest.php with the original query string. Stop processing more rules.

You can view it in action here. However, there is no way to simulate a file or directory existing, so it will always rewrite to /rest.php

https://htaccess.madewithlove.be?share=a9099413-7143-4eed-93e9-488689df7019
 

costas1

Member
Messages
146
Reaction score
4
Points
18
Thanks for your response. I'm trying to solve a problem with MediaWiki's 1.35 VisualEditor that gives a 404 error.

Someone solved it this way with Nginx: https://phabricator.wikimedia.org/T263928#6518894

I also have this code into my .htaccess file for short URLs:
Code:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/index.php [L]

Is there a way to combine it with your code for rest.php, maybe something like an if else statement?

edit: Maybe a combination with this code is necessary: https://www.mediawiki.org/w/index.p...d=vv4jnai847r5f6u2#flow-post-vv4jnai847r5f6u2

Maybe the word VisualEditor that the user has in the command in bold should be used in the RewriteRule as the pattern that should be handled by the rest.php file.

(By the way, unfortunately the solution in the last link, does not work for me and leads to a 403 error, so I'm trying to follow the other way.)
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
252
Points
63
Interesting. I'm noticing that the rule asks NGinx to match the directory rest.php, but how could it be a directory? Maybe that's the bug they're trying to fix.

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /de/rest.php/ /de/rest.php?%{QUERY_STRING} [L]
RewriteRule /en/rest.php/ /en/rest.php?%{QUERY_STRING} [L]
RewriteRule /ru/rest.php/ /ru/rest.php?%{QUERY_STRING} [L]
RewriteRule rest.php/ /rest.php?%{QUERY_STRING} [L]

This more closely follows the whole bug fix as you linked.

As I read it, the second link doesn't apply to this block, but you can add the bold text after RewriteEngine On
 

costas1

Member
Messages
146
Reaction score
4
Points
18
Interesting. I'm noticing that the rule asks NGinx to match the directory rest.php, but how could it be a directory? Maybe that's the bug they're trying to fix.

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /de/rest.php/ /de/rest.php?%{QUERY_STRING} [L]
RewriteRule /en/rest.php/ /en/rest.php?%{QUERY_STRING} [L]
RewriteRule /ru/rest.php/ /ru/rest.php?%{QUERY_STRING} [L]
RewriteRule rest.php/ /rest.php?%{QUERY_STRING} [L]

This more closely follows the whole bug fix as you linked.

As I read it, the second link doesn't apply to this block, but you can add the bold text after RewriteEngine On

The second link offers another suggested solution:
Code:
RewriteCond %{REQUEST_URI} !^(static)
RewriteCond %{HTTP_USER_AGENT} !^(VisualEditor)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/wiki/index.php [L]

For me the code above solves the 404 error, but leads to a 403. I haven't persisted in solving the 403 error and I haven't put much research into it.

The last code you suggested is a solution for user's that have a wiki farm with multiple languages. I have a single wiki, so your initial code should be all right, if it turns out it works.

My question is how should I combine your initial code (slightly modified):

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule rest.php/ /rest.php?%{QUERY_STRING} [L]


with the code I already have in my .htaccess for short URLs:

Code:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/index.php [L]


Is for example the following code OK?

Code:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/index.php
RewriteRule rest.php/ /rest.php?%{QUERY_STRING} [L]
 

spacresx

Community Advocate
Community Support
Messages
2,203
Reaction score
196
Points
63
ive never tried running anything rest with apache,
but then ive never used 2 rewrite rules like that either.

if you use the code from garrettroyce what happens,
you get the 403 ? 403 is a forbidden error i believe.
(guessing without looking it up)

also note, i saw that someone posted this,
"not able to edit the nginx or Apache base configuration."
Im not sure if this would be the same issue you'd have.
 
Last edited:

costas1

Member
Messages
146
Reaction score
4
Points
18
ive never tried running anything rest with apache,
but then ive never used 2 rewrite rules like that either.

if you use the code from garrettroyce what happens,
you get the 403 ? 403 is a forbidden error i believe.
(guessing without looking it up)

also note, i saw that someone posted this,
"not able to edit the nginx or Apache base configuration."
Im not sure if this would be the same issue you'd have.


I'm getting a 403 error with this code (https://www.mediawiki.org/w/index.p...d=vv4jnai847r5f6u2#flow-post-vv4jnai847r5f6u2):
Code:
RewriteCond %{REQUEST_URI} !^(static)
RewriteCond %{HTTP_USER_AGENT} !^(VisualEditor)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/index.php [L]

Probably the code above solves the 404 error, but in my case a 403 arises.

I also get a 403 error with my code (that uses @garrettroyce's code):
Code:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/index.php
RewriteRule rest.php/ /rest.php?%{QUERY_STRING} [L]

I'm not a 100% sure if my code is OK, but I can't just use the code that @garrettroyce gave without the rule for the short URLs, because simply I don't want to disable short URLs. If that's the equivalent of the Nginx solution, then I just should troubleshoot the 403 problem, because maybe indeed the 404 is correctly solved and the 403 is another unrelated issue.
 

spacresx

Community Advocate
Community Support
Messages
2,203
Reaction score
196
Points
63
its just me but id be cautious of code suggested offsite.
(code provided on forums other than x10 hosting)
i say that because ive noticed some posters operating environments,
at that link you showed, which is not the same as x10
and may result in different results.
just saying.

but if @garrettroyce 's code resolves the 404 as you said,
use it then just troubleshoot the 403.
 

costas1

Member
Messages
146
Reaction score
4
Points
18
its just me but id be cautious of code suggested offsite.
(code provided on forums other than x10 hosting)
i say that because ive noticed some posters operating environments,
at that link you showed, which is not the same as x10
and may result in different results.
just saying.

but if @garrettroyce 's code resolves the 404 as you said,
use it then just troubleshoot the 403.

I will use it. I'm just wondering if the order I have combined it with my short URLs' rewrite rules is right.
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
252
Points
63
You need to reorder the rules. You want the most specific rule to match, and then finish the chain. Then the next most specific, and so on. The last rule should be the index, as it's the least specific.

Both the 2 sets of RewriteCond are different ways of saying the same thing. Only one or the other is necessary. It seems strange the query string isn't preserved. I'm providing what you asked for (#1) and what may work if #1 doesn't (#2)

Code:
#1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule rest.php/ /rest.php?%{QUERY_STRING} [L]
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/index.php [L]

Code:
#2
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule rest.php/ /rest.php?%{QUERY_STRING} [L]
RewriteRule ^ index.php?%{QUERY_STRING} [L]

Please provide the HTTP error log from DirectAdmin for further troubleshooting. The issue looks like missing files, which are logged. So if it's looking for rest.php/something/file.html, instead of saying the file does not exist, it will serve rest.php or if it's looking for something.php it will serve index.php

We also need to see if any files have wrong permissions, causing 4XX errors, or missing indexes.
 

costas1

Member
Messages
146
Reaction score
4
Points
18
None of the above versions with the index.php rule at the bottom works. The pages load without any styling. When the rest.php rule is the last one the 404 goes away and I get a 403 error.

The same thing happens with the code that was posted on MediaWiki community:

Code:
RewriteCond %{REQUEST_URI} !^(static)
RewriteCond %{HTTP_USER_AGENT} !^(VisualEditor)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/wiki/index.php [L]

It also gives a 403 error.

I guess both codes deal successfully with the 404 error.


I will report the 403 error to MediaWiki and then I will report my progress here.
 

costas1

Member
Messages
146
Reaction score
4
Points
18
By the way, here is the log for the 403 error:

Code:
162.158.156.190 - - [08/Oct/2020:15:18:06 -0400] "GET /Main_Page HTTP/1.1" 200 8475 "https://[domain]/Main_Page?action=edit&veswitched=1" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.228 (Edition avira-2)"
162.158.156.190 - - [08/Oct/2020:15:18:09 -0400] "GET /load.php?lang=en&modules=oojs-ui-core%2Coojs-ui-widgets&skin=timeless&version=7khli HTTP/1.1" 304 433 "https://[domain]/Main_Page?veaction=edit" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.228 (Edition avira-2)"
162.158.156.190 - - [08/Oct/2020:15:18:09 -0400] "GET /load.php?lang=en&modules=ext.visualEditor.core&skin=timeless&version=1sypu HTTP/1.1" 304 433 "https://[domain]/Main_Page?veaction=edit" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.228 (Edition avira-2)"
162.158.73.250 - - [08/Oct/2020:15:18:11 -0400] "GET /rest.php/[domain]/v3/page/html/Main_Page/95?redirect=false&stash=true HTTP/1.1" 403 455 "-" "VisualEditor-MediaWiki/1.35.0"
162.158.156.190 - - [08/Oct/2020:15:18:09 -0400] "GET /api.php?action=visualeditor&format=json&paction=parse&page=Main_Page&uselang=en&formatversion=2 HTTP/1.1" 200 931 "https://[domain]/Main_Page?veaction=edit" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.228 (Edition avira-2)"
162.158.156.190 - - [08/Oct/2020:15:18:11 -0400] "POST /api.php HTTP/1.1" 200 885 "https://[domain]/Main_Page?veaction=edit" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.228 (Edition avira-2)"
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
252
Points
63
Here's what I see:

Code:
GET /Main_Page
200 - OK
GET /load.php?lang=en&modules=oojs-ui-core%2Coojs-ui-widgets&skin=timeless&version=7khli
304 - NOT MODIFIED
GET /load.php?lang=en&modules=ext.visualEditor.core&skin=timeless&version=1sypu
304 - NOT MODIFIED
GET /rest.php/[domain]/v3/page/html/Main_Page/95?redirect=false&stash=true
403 - FORBIDDEN
GET /api.php?action=visualeditor&format=json&paction=parse&page=Main_Page&uselang=en&formatversion=2
200 - OK
POST /api.php
200 - OK

so your RewriteRule isn't fixing the issue. /rest.php/ looks strange to me. Usually, rest.php is a file (a PHP script), but it looks like a directory, with several more subdirectories (/rest.php/[domain]/v3/page/html/Main_Page/) and the final file name is 95.

Something is blocking the rewrite rule for /rest.php/ is my best guess. I don't even think the RewriteCond matter because such a file wouldn't even exist. What should be done with the additional data though? I would think that /[domain]/v3/page/html/Main_Page/95 is important information, but we're discarding it currently. So something like

Code:
RewriteRule ^/rest.php/(.*)$ /rest.php?SOMETHING=$1&%{QUERY_STRING}

If you can do a little digging, we just need to know what SOMETHING is. Or, we can discard that data, but I don't see how it isn't crucial to this working. If we assume that SOMETHING actually doesn't need to be there (which is totally valid) it could possibly be

Code:
RewriteRule ^/rest.php/(.*)$ /rest.php?$1&%{QUERY_STRING}

Can you find the documentation for rest.php ? Presumably, the API is described in the developer documentation.
 

costas1

Member
Messages
146
Reaction score
4
Points
18
rest.php is an actual file. The rest of the URL does not correspond to real directories.

The 404 error is solved. Both versions of the .htaccess (mine with the rest.php rewrite rule at the end and also this one: https://www.mediawiki.org/w/index.p...d=vv4jnai847r5f6u2#flow-post-vv4jnai847r5f6u2) solve the 404 and a 403 arises.

If you read the 2 links of Phabricator I shared you'll see what's going on with it, but it is related with MediaWiki, not with the server.

You can consider this problem (the 404 solved) and close this thread.
 
Top