I'm having issues with a client site.
The issue is that there seems to be a redirect on a URL which doesn't exist/has no content:
www.henparty-houses.com/liverpool
Yet if we changed the end URL to another with no content / does not exist we get the correct page load (i.e. page doesn't exist).
www.henparty-houses.com/leeds
===
There seems to be some sort of redirect on the /liverpool URL.
The issue is when a page is create, ending with the URL /liverpool - it is not possible to use this URL as wordpress think's it's already in use / taken.
I might be missing something obvious or some sort of error, but I need to somehow clear the /liverpool URL so content can be used, and ensure when a page with content is created using /liverpool there is no redirect!
Can someone please help?
---- when checking this redirect, here is the output. I've checked clourflare and can't see any redirects setup:
Results:
www.henparty-houses.com/liverpool
HTTP/1.1 301 Moved Permanently
Date: Fri, 25 Sep 2020 10:08:36 GMT
Content-Type: text/html; charset=iso-8859-1
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=d440b3cca7954a48bdc3f84b50e73ea7a1601028515; expires=Sun, 25-Oct-20 10:08:35 GMT; path=/; domain=.henparty-houses.com; HttpOnly; SameSite=Lax
Location: https://www.henparty-houses.com/liverpool
Cache-Control: max-age=0
Expires: Fri, 25 Sep 2020 10:08:36 GMT
Host-Header: b7440e60b07ee7b8044761568fab26e8
X-Proxy-Cache: MISS
CF-Cache-Status: DYNAMIC
cf-request-id: 05665580120000ed6fcaa52200000001
Server: cloudflare
CF-RAY: 5d83f1e01a84ed6f-SJC
https://www.henparty-houses.com/liverpool
HTTP/2 302
date: Fri, 25 Sep 2020 10:08:39 GMT
content-type: text/html; charset=UTF-8
set-cookie: __cfduid=d974040f170c5090b9a4e61af9590d1b61601028518; expires=Sun, 25-Oct-20 10:08:38 GMT; path=/; domain=.henparty-houses.com; HttpOnly; SameSite=Lax
x-cache-enabled: True
p3p: CP="ALL DSP NID CURa ADMa DEVa HISa OTPa OUR NOR NAV DEM"
x-redirect-by: WordPress
location: https://www.henparty-houses.com
cache-control: max-age=0
expires: Fri, 25 Sep 2020 10:08:38 GMT
host-header: b7440e60b07ee7b8044761568fab26e8
x-proxy-cache: MISS
cf-cache-status: DYNAMIC
cf-request-id: 05665588c800009623ca33e200000001
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
cf-ray: 5d83f1ee09ee9623-SJC
https://www.henparty-houses.com
HTTP/2 200
date: Fri, 25 Sep 2020 10:08:41 GMT
content-type: text/html; charset=UTF-8
set-cookie: __cfduid=d63a3cce54fa1ecc1e3d8bbf7c7415b8d1601028519; expires=Sun, 25-Oct-20 10:08:39 GMT; path=/; domain=.henparty-houses.com; HttpOnly; SameSite=Lax
x-cache-enabled: True
p3p: CP="ALL DSP NID CURa ADMa DEVa HISa OTPa OUR NOR NAV DEM"
link: ; rel="https://api.w.org/", ; rel="alternate"; type="application/json", ; rel=shortlink
vary: Accept-Encoding
cache-control: max-age=0
expires: Fri, 25 Sep 2020 10:08:40 GMT
host-header: b7440e60b07ee7b8044761568fab26e8
x-proxy-cache: MISS
cf-cache-status: DYNAMIC
cf-request-id: 0566558f4e0000930a5d2f8200000001
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
cf-ray: 5d83f1f87ede930a-SJC
I don't think you're going to find the answer looking in your .htaccess files. As you can see from the headers, the redirect is being issued by WordPress itself.
x-redirect-by: WordPress
If you go to your WordPress installation directory, you can use the following command to find the function that sets this header.
grep --line-number --with-filename --fixed-strings -i -R 'x-redirect-by' .
The result is pretty short:
./wp-includes/pluggable.php:1284: * Filters the X-Redirect-By header.
./wp-includes/pluggable.php:1296: header( "X-Redirect-By: $x_redirect_by" );
Unsurprisingly, the function that sets this header is called wp_redirect.
In short, the problem is probably not the server configuration. What I think is happening is that one of your pages' slug is set to liverpool.
Normally, when WordPress receives a request for a page that doesn't exist (triggering a 404 error), it first attempts to redirect the request to a "canonical" URL (see this post). These canonical URLs are called "slugs".
If one of your existing pages has a slug set to liverpool, this would trigger the URL canonicalization process. It's therefore possible that maybe that page is set to hidden, or is manually redirecting the user because it requires the user to be logged in, or a myriad of other things. Either way, a manual redirect is the reason for the 302 redirect. The best way to find out what exactly is going on is to find the page with that particular slug.
The reason I'm inclined to believe the page exists right now is that a 302 means Found. If a page had previously used a slug of liverpool and then changed it, the proper response would have been a 301 Moved Permanently with a redirect to the current version of the page. My thinking is that a page exists with a slug of liverpool that is purposefully redirecting the client to the main page. The solution, therefore, is most likely tracking that one page down.
You can speed this process up significantly by accessing the database directly, rather than via the user interface. The wp_posts table contains most of the page content (see the WordPress documentation for a more in-depth explanation), and the wp_postmeta table contains post metadata.
I would start by running a query like this (I'm assuming you're using MariaDB or MySQL, but a Postgres or whatever query would be analogous, if not identical).
SELECT `wp_posts`.`post_name` FROM `wp_posts` WHERE `wp_posts`.`post_name` LIKE '%liverpool%';
This query should get you close. If you can't find anything there, you might want to check the wp_postmeta table. Depending on how large the site's database is, it might actually be easier to just go page by page in the UI and check by hand, since a lot of entries are either serialized objects, HTML, or just generally hard to visually parse.
SELECT `wp_postmeta`.* FROM `wp_postmeta`;
You can use another LIKE clause here, obviously, which might be helpful to try first, before running this one. Keep in mind it might take a while to run though since it has to filter the results based on a wildcard match, so it might be helpful to run this basic query first so you have all the results, then the specific query on another console, so you can try to go through the output while the other query executes. I'm not sure how fast the hardware you're on is, but I speak from personal experience lol.
I would start by running the following query on any tables you're working with so you can get a handle of the table structure.
DESCRIBE `wp_postmeta`;
DESCRIBE `wp_posts`;
You can also do SHOW TABLES to actually see all of the tables in the database you're working with.
You should also take a look at this post. In particular, note where it says you can disable redirects by adding the remove_action( 'template_redirect', 'wp_old_slug_redirect');. I'm not personally familiar with that functionality, but if you look at the wp_redirect function definition ($WORDPRESS_INSTALL_DIR/wp-includes/pluggable.php, line 1246), you can see the function applies filters elsewhere defined.
Note also that the function specifically checks to make sure the status code is between 300 and 399, meaning something is changing the status code from 404 to 302.
Here is the function definition, for your convenience and edification.
/**
* Redirects to another page.
*
* Note: wp_redirect() does not exit automatically, and should almost always be
* followed by a call to `exit;`:
*
* wp_redirect( $url );
* exit;
*
* Exiting can also be selectively manipulated by using wp_redirect() as a conditional
* in conjunction with the {#see 'wp_redirect'} and {#see 'wp_redirect_location'} filters:
*
* if ( wp_redirect( $url ) ) {
* exit;
* }
*
* #since 1.5.1
* #since 5.1.0 The `$x_redirect_by` parameter was added.
* #since 5.4.0 On invalid status codes, wp_die() is called.
*
* #global bool $is_IIS
*
* #param string $location The path or URL to redirect to.
* #param int $status Optional. HTTP response status code to use. Default '302' (Moved Temporarily).
* #param string $x_redirect_by Optional. The application doing the redirect. Default 'WordPress'.
* #return bool False if the redirect was cancelled, true otherwise.
*/
function wp_redirect( $location, $status = 302, $x_redirect_by = 'WordPress' ) {
global $is_IIS;
/**
* Filters the redirect location.
*
* #since 2.1.0
*
* #param string $location The path or URL to redirect to.
* #param int $status The HTTP response status code to use.
*/
$location = apply_filters( 'wp_redirect', $location, $status );
/**
* Filters the redirect HTTP response status code to use.
*
* #since 2.3.0
*
* #param int $status The HTTP response status code to use.
* #param string $location The path or URL to redirect to.
*/
$status = apply_filters( 'wp_redirect_status', $status, $location );
if ( ! $location ) {
return false;
}
if ( $status < 300 || 399 < $status ) {
wp_die( __( 'HTTP redirect status code must be a redirection code, 3xx.' ) );
}
$location = wp_sanitize_redirect( $location );
if ( ! $is_IIS && 'cgi-fcgi' !== PHP_SAPI ) {
status_header( $status ); // This causes problems on IIS and some FastCGI setups.
}
/**
* Filters the X-Redirect-By header.
*
* Allows applications to identify themselves when they're doing a redirect.
*
* #since 5.1.0
*
* #param string $x_redirect_by The application doing the redirect.
* #param int $status Status code to use.
* #param string $location The path to redirect to.
*/
$x_redirect_by = apply_filters( 'x_redirect_by', $x_redirect_by, $status, $location );
if ( is_string( $x_redirect_by ) ) {
header( "X-Redirect-By: $x_redirect_by" );
}
header( "Location: $location", true, $status );
return true;
}
I am trying to access private S3 from an EC2 machine but using curl only.
So I am trying to create a proxy server using nginx and lua which will call metadata apis and get the authorization token and set the headers in proxy_pass.
location /download/ {
set $date '';
set $token '';
set $authorization '';
content_by_lua_block {
% some code %
ngx.var.date = date;
ngx.var.token = awsToken;
ngx.var.authorization = authorization;
}
proxy_set_header Date $date;
proxy_set_header X-AMZ-Security-Token $token;
proxy_set_header Authorization $authorization;
proxy_pass "https://nisingla-ethos.s3.amazonaws.com/";
}
However, when i check date varible outside of content_by_lua_block, its value is not set.
Can someone help me with the issue.
PS: I have tried other method of using bucket policy and vpc endpoint but due to some constraint they will not work for me.
Both content_by_lua_block and proxy_pass are processed in Content phase.
Nginx does call only one such directive.
You may use access_by_lua_block or set_by_lua_block.
Recently we plan to change tengine to nginx, we used $unxi_time variable in tengine, and I didn't find similiar variable in nginx document, any advice on this? thanks
Already solved.
used lua module to return local timestamp
location = /management/timestamp {
default_type application/json;
content_by_lua '
time=os.time();
ngx.say("{\"timestamp\""..":"..time.."}");
';
}
I'm working on an API and I can't get Sf2 to catch the "Date" parameter from the request header... Demo is below. I'm testing my API via Postman.
$date = $this->request->headers->get('Date');
$auth = $this->request->headers->get('Authorization');
echo $date; // NULL
echo $auth; // whatever i pased.
A very strange behaviour indeed ! Could anyone know why ?
The Date header is a restricted one and it's not possible to overwrite it unless you use the Interceptor Chrome Extension (see this official link for details).
This is a list of restricted headers:
Accept-Charset
Accept-Encoding
Access-Control-Request-Headers
Access-Control-Request-Method
Connection
Content-Length
Cookie
Content-Transfer-Encoding
Date
Expect
Host
Keep-Alive
Origin
Referer
TE
Trailer
Transfer-Encoding
Upgrade
User-Agent
Via
That's why you're getting an empty header.
change
$date = $this->request->headers->get('Date');
to
$date = $this->request->headers->get('X-Date');
I have a client program I cannot modify. It makes large POST (x-www-form-urlencoded) requests containing hundreds of variables across WAN links, but I only need 5 of them. I'm inserting nginx as a reverse proxy on the local client system. What's the easiest to get nginx to strip out the extra data?
Two ways I see so far:
1. Use Lua (If I did, should I do content_by_lua, rewrite the body, and then make a subrequest? Or is there a simpler way?)
2. Use form-input-nginx-module and proxy_set_body to parse and grab a few variables out.
I'm already using OpenResty, so Lua means no extra modules. But, it probably means writing more locations and so on to do subrequests.
In my opinion the easiest way will be using lua. The choice between content_by_lua, rewrite_by_lua, access_by_lua or any combination of them; will depend on how you use the response body of your subrequest. That decision will also determine if you would need additional locations or not.
Here are a couple of examples:
1. with content_by_lua targeting a local location.
(This approach requires the definition of the sub request location)
location /original/url {
lua_need_request_body on;
content_by_lua '
--Lots of params but I only need 5 for the subrequest
local limited_post_args, err = ngx.req.get_post_args(5)
if not limited_post_args then
ngx.say("failed to get post args: ", err)
return
end
local subreq_uri = "/test/local"
local subreq_response = ngx.location.capture(subreq_uri, {method=ngx.HTTP_POST,
body = ngx.encode_args(limited_post_args)})
ngx.print(subreq_response.body)
';
}
location ~/test/local {
lua_need_request_body on;
proxy_set_header Accept-Encoding "";
proxy_pass http://echo.200please.com;
}
2. with rewrite_by_lua to remote target
(No additional location is needed)
location /original/url/to/remote {
lua_need_request_body on;
rewrite_by_lua '
--Lost of params but I only need 5 for the subrequest
local limited_post_args, err = ngx.req.get_post_args(5)
if not limited_post_args then
ngx.say("failed to get post args: ", err)
return
end
--setting limited number of params
ngx.req.set_body_data(ngx.encode_args(limited_post_args))
--rewriting url
local subreq_path = "/test"
ngx.req.set_uri(subreq_path)
';
proxy_pass http://echo.200please.com;
}
Sample post request with 7 args limited to 5:
curl 'http://localhost/original/url/to/remote' --data 'param1=test¶m2=2¶m3=3¶m4=4¶m5=5¶m6=6¶m7=7' --compressed
response:
POST /test HTTP/1.0
Host: echo.200please.com
Connection: close
Content-Length: 47
User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
Accept: */*
Accept-Encoding: deflate, gzip
Content-Type: application/x-www-form-urlencoded
param3=3¶m4=4¶m1=test¶m2=2¶m5=5