My task is to set up a redirect from pages with a trailing slash to a page without a trailing slash through 301 redirects for SEO.
By default, nextjs has a 308 redirect configured.
Now:
/catalog/123/ -> /catalog/123 308 redirect
What I expect:
/catalog/123/ -> /catalog/123 301 redirect
/catalog/99/ -> /catalog/99 301 redirect
I'm trying to do it this way. What is my mistake?
module.exports = {
async redirects() {
return [
{
source: '/catalog/:slug/',
destination: '/catalog/:slug',
statusCode: 301,
},
]
},
}
I have seen that it is possible to configure redirect in getServerSideProps, but in my case it is getStaticProps.
upd:
Code project example https://stackblitz.com/edit/nextjs-g2zvta?file=pages/posts/[id].js
Link example. posts/1/ redirect to posts/1 with 308
https://nextjs-g2zvta--3000.local.webcontainer.io/posts/1/
GetStaticProps() only runs at build time. You have to organize middlewares for redirects on static pages. Unlike the config you have (almost) full control over the response stats in there.
Related
I've been trying to get this to work, but cannot.
I know I can redirect http -> https using something like this:
http-request redirect scheme https code 301 if !{ ssl_fc }
I know I can redirect www -> non-www using something like this:
http-request redirect prefix https://%[hdr(host)] code 301 if { hdr_beg(host) -i www }
// although note this doesn't work for me, it does nothing
I need the following:
http://example.com/any-uri -> https://example.com/any-uri
http://www.example.com/any-uri -> https://example.com/any-uri
https://www.example.com/any-uri -> https://example.com/any-uri
How can I do both http -> https and www -> non-www using a single 301 redirect?
Thanks
I have a solution, although it requires two rules.
http-request redirect prefix https://%[hdr(host),regsub(^www\.,,i)] code 301 if { hdr_beg(host) -i www. }
http-request redirect scheme https code 301 if !{ ssl_fc }
Based on the above, the following will happen, all with a single redirect:
http://example.com/any-uri -> https://example.com/any-uri
http://www.example.com/any-uri -> https://example.com/any-uri
https://www.example.com/any-uri -> https://example.com/any-uri
This is because the first rule catches http://www... and https://www... URLs and redirects them to https://example.com. The remaining case is http://example.com which gets handled by the second rule via a simple redirect to https://example.com.
I am moving my old HTTP site to Netlify and need some guidance on 301 redirects and SEO.
I will be using netlify.toml for redirects.
Netlify will automatically redirect http://www.example.com to https://www.example.com so I'm assuming that a redirect rule is not needed for this case.
Question 1: For http://example.com to https://www.example.com - should I do this in DNS or 301 redirect rule in netlify.toml?
For the following URL
http://www.example.com/directory/page1.html
Question 2: Should I use relative or absolute URLs to redirect? Which from below would be a best-case scenario?
[[redirects]]
from = "/directory/page1.html"
to = "https://www.example.com/directory/:splat"
status = 301
force = true
[[redirects]]
from = "http://www.example.com/directory/page1.html"
to = "https://www.example.com/directory/:splat"
status = 301
force = true
Hi i need redirect only www.example.com/foo in foo.example.com, if try in this way:
redirect location http://foo.example.com code 301 if { path_beg /foo }
all /foo (include foo.example.com/foo) redirect to wrong URL, how can redirect only www.example.com/foo?
Thanks
I have an haproxy that I want to reroute only one url from test.ai to www.test.ai
redirect prefix https://www.test.ai code 301 if { hdr(host) -i test.ai}
But for some reason I get the test.ai redirected you too many times.
You appear to have a redirect set somewhere else. If you're using Wordpress as a backend. You need to set your site URL and home.
define( 'WP_HOME' , 'https://test.ai');
define( 'WP_SITEURL', WP_HOME . '/');
Then you'll need to add:
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
$_SERVER['HTTPS']='on';
Finally be sure this is at the bottom...
require_once(ABSPATH . 'wp-settings.php');
Otherwise, you may have a redirect or rewrite in your apache/nginx setup
Using node-red on Bluemix, I was able to get my application running on both http://example.com and https://example.com. However, I want my application to be secured and can be accessed only with https. I can not find the way to redirect http to https on node-red running on Bluemix. I found only the way to do it on node.js/express and other web servers.
I also tried to do the redirect on my application by detecting msg.req.headers.$wssc and use http request node to redirect to https url but it did not work (still got http response).
Configure redirect on DNS is also not an option because AFAIK, you can not redirect http://example.com to https://example.com but you can redirect http://example.com to https://www.example.com.
Please suggest.
You should be able to do this by using the msg.req.headers.x-forwarded-proto value then sending a response with a 301 status e.g.
if (msg.req.headers['x-forwarded-proto'] == 'http') {
msg.statusCode = 301;
msg.headers = {
location: 'https://' + msg.req.get('host') + msg.req.originalUrl
}
msg.payload = "";
return [null, msg]
} else {
return [msg, null]
}
If you put that in a function node with 2 outputs it will send the redirect out the second output else send it out the first.
You could also do this by adding your own httpNodeMiddleware function in settings.js like this:
....
httpNodeMiddleware: function(req, res, next){
if (req.headers['x-forwarded-proto'] == 'http') {
res.redirect(301, 'https://' + req.get('host') + req.originalUrl);
} else {
next();
}
},
...
This method means you don't need any of the redirect logic in your flow
Side Note:
You probably need to test for msg.req.headers['$wssc'] rather than msg.req.headers.$wssc.