Nginx duplicating query string on redirect - linux

I am trying to redirect all traffic from urls that contain 'www.' to the similar url that does not contain it, but when I do so Nginx appends a duplicate query string at the end of the url.
For example, the user enters:
www.website.com/test/?_id=12345
And they get redirected to:
website.com/test/?_id=12345?_id=12345
Here is my config:
server {
server_name www.website.com;
return 301 $scheme://website$request_uri permanent;
}
server {
server_name website.com
# actual server stuff
}
I have tried appending a '?' to the end of the redirect after the 'request_uri' portion because form what I have read that should work, but it didn't.

Your config is invalid. You seem to have mixed up the syntax of return and rewrite. See this document for details.
The $request_uri variable already includes the query string, so this should work with return:
return 301 $scheme://example.com$request_uri;
The rewrite directive will append the query string unless a ? is appended. It is possible to use a rewrite directive to accomplish the same function, but in this case it would be overkill.
As your config is invalid, nginx is still running with an earlier configuration, possibly an earlier experiment that placed $request_uri in a rewrite directive, hence the double query string.

Related

redirect to http depending on condition

I use nginx
Assume that url hello.test.example.com, hello example of dynamically subdomain, how can i redirect to hello.test2.example.com when hello file existing in specific directory path
server_name ~^(?<subdomain>[^.]+)\.test\.example\.com$;
set $m_file /home/files/$subdomain;
if (-f $m_file) {
rewrite ^/(.*)$ http://$subdomain.test2.example.com/;
}
this condition working when the url (hello.test.example.com) but when the url (hello.test.example.com\en) working but not every time ! why ?

.htaccess permanent redirect - browser strangely adds GET variable to URL

I am trying to implement a very basic redirect for specific pages with htaccess, however the browser adds a GET variable to the new URL after the redirect:
Redirect 301 /branding/ABCDE http://example.com/branding/NEW
New URL in browser:
http://example.com/branding/NEW?slug=ABCDE
How can I correct this so that the ?slug=ABCDE does not show up?
Without seeing your .htaccess file, the best guess is that you have a conflict with other (specifically mod_rewrite) directives in your .htaccess file. There is nothing actually wrong with the Redirect directive itself that you posted.
The Redirect directive is a mod_alias directive and executes after mod_rewrite, despite the apparent order of these directives in your .htaccess file. A URL of the form /branding/ABCDE most probably requires rewriting (using mod_rewrite) for it to be useful by your application. This rewrite occurs before the Redirect, but any query string will be maintained.
It is not recommended to mix both Redirect and RewriteRule directives in the same config file, because of these unexpected conflicts.
Change your redirect to use mod_rewrite instead and make sure this is near the top of your config file. For example:
RewriteEngine On
RewriteRule ^branding/ABCDE$ /branding/NEW [QSD,R=301,L]
You will need to ensure your browser cache is cleared before testing, since the erroneous 301 (permanent) redirect will likely have been cached by your browser. To avoid caching issues, it is advisable to first test with 302 (temporary) redirects.
The QSD flag (Apache 2.4+) will discard any query string that might be present on the initial request.

Nginx parse and redirect to other domain

I wanna make some redirect but for this i need to parse domain to send other domain.
My old domain url like this
http://olddomain.com/bg/some-name-part-421.html
http://olddomain.com/bg/some-name-1231.html
http://olddomain.com/bg/some-name-product-name-221.html
I want to redirect this to like this
https://www.newdomain.com/magazin/some-name-part.html
https://www.newdomain.com/magazin/some-name.html
https://www.newdomain.com/magazin/some-name-product-name.html
I try to redirect them like this on server block
rewrite ^(/bg/)([a-z-]+-[0-9]+)\.html$ http://www.newdomain.com/magazin/$2 permanent;
Not working well making redirect like this
http://www.olddomain.com/bg/chervena-borovinka-bioherba-3694.html
https://www.newdomain.com/magazin/chervena-borovinka-bioherba-3694
I want to delete also as last part of number and - but i dont know why not working well
Thats my .htaccess:
RewriteEngine On
RewriteRule "^bg\/([^0-9]+(?<!-))-([0-9]+)(\.html)" "http://newdomain.com/magazin/$1$3" [R]
Dont forget to set the RewriteBase.
Heres the code: https://regex101.com/r/UdMqaQ/3/
Nginx
rewrite "^/bg\/([^0-9]+(?<!-))-([0-9]+)(\.html)" "newdomain.com/magazin/$1";

Wordpress MU Cloudfront. Trailing slash redirect goes to wrong domain

I'm having some trouble with redirects within wordpress redirection causing the domain to change.
Example:
Site - noncdn.somedomain.com
CDN URL - www.domain.com
When I open links w/o a trailing slash there is a 301 redirect:
Going here: www.domain.com/page
Takes you here: noncdn.somedomain.com/page/
Since Cloudfront is hitting the server using Origin Domain, the server doesn't even know that requests are coming in from a different domain.
How do I force this 301 to use FQDN w/ correct CDN domain instead of doing a relative redirect?
I've already added this so that links on the site and images all load from Cloudfront domain, but it seems to have no effect on the redirect behavior:
add_filter('home_url','home_url_cdn',10,2);
function home_url_cdn( $path = '', $scheme = null ) {
return get_home_url_cdn( null, $path, $scheme );
}
function get_home_url_cdn( $blog_id = null, $path = '', $scheme = null ) {
$cdn_url = get_option('home');
if(get_option('bapi_site_cdn_domain')){
$cdn_url = get_option('bapi_site_cdn_domain');
}
$home_url = str_replace(get_option('home'),$cdn_url,$path);
//echo $home_url;
return $home_url;
}
Any Help is much appreciated!
Thanks!
I was tracking down a very similar issue for a while with a Cloudfront distribution of a standard static website running on Nginx. The symptoms were the same, links with a trailing slash (e.g. www.acme.com/products/) worked correctly, but omitting the trailing slash caused the user to be redirected to the origin.
The issue is that the webserver software itself is not properly attempting to resolve URIs and is instead responding with a redirect to a URL it can serve. You can test this by using curl against your site:
$ curl http://myhost.com/noslashurl
HTTP/1.1 301 Moved Permanently [...]
CloudFront is returning exactly what your server returns, in this case a 301 redirect to your origin URL. Instead of following the redirect and caching that, CloudFront caches the redirect itself. The only way to correct this is to ensure that your origin properly handles the requests and does not respond with a 301.
In my particular case, that meant updating the try_files directive for my location in the nginx configuration. As I mentioned this is a static site, and so my try_files became:
location / {
[...]
try_files $uri $uri/index.shtml /index.shtml =404;
}
You want to be sure that the try_files has an endgame, to avoid redirection cycling which will cause your server to return 500 Server Errors when a non-existent URL is requested. In this case, /index.shtml is the last-ditch attempt and failing that, it will return a 404.
I know this doesn't precisely answer your question, but yours was one of a very few I found when searching for "cloudfront without trailing slash redirects to origin", and you've not had an answer for a year, so I figured it was worth sending a response.
I had the same problem.
I fixed the issue changing some wordpress parameters.
In the elasticbeanstalk I set the parameter CUSTOM_URL for my custom domain and in the file /var/www/html/wp-includes/load.php
I set the parameters HTTP_HOST and SERVER_NAME to same value of CUSTOM_URL, and it resolved the redirect to elasticbeanstalk url.
$_SERVER['HTTP_HOST'] = $_SERVER['CUSTOM_URL'];
$_SERVER['SERVER_NAME'] = $_SERVER['CUSTOM_URL'];

How to redirect a site to a new site

I have a site which has pages like this:
blabla.com/page/whatever
blabla.com/category/whatever
blabla.com/about
...
How can I redirect each of these to a new domain, like:
blabla.net/page/whatever
blabla.net/category/whatever
blabla.net/about
...
?
Using .htaccess
Use the Redirect directive:
Redirect / http://blabla.net/
This directive automatically preserves anything specified after the /.
It might take a bit of fiddling, but the basic idea should work here:
RewriteEngine on
RewriteRule ^(.+)$ http://blabla.net/$1 [R,NC]
You need to have mod_rewrite installed in Apache.
This says "match all URLs on this site, and redirect them to http://blabla.net/the same URL. The [R] means to actually send a redirect request to the client (so the client will make the request to the new server), rather than just serving up the page but keeping the browser URL the same. You can take the R out if you just want to serve the page but keep the old URL.
Or if you use nginx (like we at http://applehub.us, http://crazyfootball.net etc)
location ~ ^/.*_sitemap([\d]+)?.(xml|xml.gz)$ {
rewrite /(.*) /$1 break;
proxy_pass http://yourupstrem;
}

Resources