Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
154

Any redirect to localhost doesn't make sense from a remote system (e.g. client's Web browser). So the rewrite flags permanent (301) or redirect (302) are not usable in your case. Please try following setup using a transparent rewrite rule: location /foo { rewrite /foo/(.*) /$1 break; proxy_pass http://localhost:3200; proxy_redirect off;...


111

Simple location prefix matching works for this without using a rewrite rule as long as you specify a URI in the proxy_pass directive: location /foo { proxy_pass http://localhost:3200/; } Notice the additional / at the end of the proxy_pass directive. NGINX will strip the matched prefix /foo and pass the remainder to the backend server at the URI /. ...


49

The absolute most correct way and best practice is usually as follows: location /foo/ { proxy_pass http://localhost:3200/; # note the trailing slash! } Note the dire importance of the trailing slash in proxy_pass, which automatically alters the $uri variable to have the /foo/ on the front-end correspond with / on the backend. No need for an explicit ...


34

Use openssl s_client piped to openssl x509: $ openssl s_client -connect foo.example.com:443 < /dev/null | openssl x509 -text (Add -servername foo.example.com to the s_client command if the server uses SNI.) The redirection of stdin from /dev/null for the first invocation of openssl will prevent it from hanging waiting for input.


30

You can use Include directive in httpd.conf to be able to maintain redirects in another file. But it would not be very efficient, as every request would need to be checked against a lot of regular expressions. Also a server restart would be required after every change in the file. A better way for so many redirects would be to use RewriteMap directive of ...


29

The IIS URL Rewrite Module for IIS7+ may be your friend. The module can be downloaded here: https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-the-url-rewrite-module Once it's installed, you can create a host wide redirect. After the module is installed, you'll have a URL Rewrite App in IIS manager. Click that and then Add Rule(s)... ...


21

Like many admin/developers I've been fighting the intricacies of rewrite rules for years and am unhappy with the existing Apache documentation, so I decided as a personal project to get to the bottom of how mod_rewrite actually works and interacts with the rest of the Apache core, so over the last few months I've been instrumenting test cases with strace + ...


21

Most efficient and clean way to do this is to configure two separate server{} blocks - one to do redirects, and another one (with canonical name) to actually handle requests. Example configuration: server { listen 80; listen 443 ssl; server_name xxx yyy.example.com $hostname; ssl_certificate ... ssl_certificate_key ... return 302 ...


18

Since nginx 0.8.25 named captures can be used in server_name. You should use them. Here, the sub-domain will be stored in a variable called $sub. Then you will be able to reuse it in the rewrite directive : server { listen 80; server_name ~^(?<sub>\w+)\.olddomain\.com$; rewrite ^ $scheme://$sub.doma.in$request_uri? permanent; } Alternatively, ...


18

The Order, Deny, and Allow options have been replaced in Apache 2.4 with <Directory /var/www/mysite.com/htdocs/public> Require all granted </Directory> You can explicitly restrict addresses through the use of the following: <Directory /var/www/mysite.com/htdocs/public> Require all granted Require not ip 192.168.0.1 </...


16

A redirect from http://old.example.com to https://new.example.com does not require a certificate for old.example.com. But a redirect from https://old.example.com to https://new.example.com does. If people's bookmarks or search engine search results or other external links point to the https site, you better renew the cert. If you merely assume that people ...


15

Using rewritemap There are lots of things you can do with rewritemaps. Rewritemaps get declared using the Rewritemap directive, and can then be used both in RewritCond evaluations, and in RewriteRule Subsitutions. The general syntax for RewriteMap is: RewriteMap MapName MapType:MapSource For example: RewriteMap examplemap txt:/path/to/file/map.txt You ...


15

Yes, you will need a new certificate if the redirection is done in a HTTP response (a 301 or 302 return code). If you don't the redirect will not work, visitors of the old domain will get an error the certificate expired if they visit the old domain via HTTPS.


12

And voila, the fix: <VirtualHost mysite.com:80> ServerName mysite.com ServerAlias www.mysite.com DocumentRoot /home/rotate/public_html ServerAdmin me@mysite.com UseCanonicalName Off </VirtualHost> NameVirtualHost mysite.com:80 <VirtualHost 192.168.1.1:80> ServerName 192.168.1.1 Redirect 403 / ErrorDocument ...


10

Snarky Answer: DNS does not do URL forwarding. DNS has no knowledge of this "301 Redirect" of which you speak, and has no interest in your H-T-T-P verbosity over on port 80, nor these you-are-ell thingies - it only knows name, address, and maybe a few other important conversational words like "food", "bathroom" and "penicillin" . Non-Snarky Answer: You can'...


9

Think very carefully about what you're asking for, and strongly consider not doing it. RFC 2616 specifies that the entity bodies you want to remove should be present. 10.3.2 301 Moved Permanently The new permanent URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain ...


9

There is a special kind of processing for this scenario, as per the docs: If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, or memcached_pass, then the special processing is performed. In response to a request with URI equal to this string, ...


8

It was an empty request. %r is actually the first line of their request, which means they sent an empty request. In other words, no headers, no body, nothing. It was likely a socket connection to port 80. The 301 was likely not to the website--remember, they have nothing defined in their request, including the desired file on your web site. Exactly. - ...


8

nginx already can cache 301 redirects. You can change the amount of time they are cached with the proxy_cache_valid directive: proxy_cache_valid 301 365d; # Cache permanent redirects for a whole year


7

It looks like a 426 should only be thrown if you actually tried negotiating for TLS and it failed. Because the user only used HTTP, we don't yet know their SSL capabilities so what you would want to do is 301 the user to HTTPS, and then if they are unable to handle TLS, you throw a 426.


7

Disclaimer: I'm the author of http://www.yes-www.org/ and I'm glad you like it. You've correctly identified a serious issue here, that of client-side and possibly even proxy caching of the 301 response. (Fortunately, search engines will pick up any change pretty quickly, so there's little to worry about there.) On caching, RFC 2616 has this to say: A ...


7

This configuration will do what you want: server { listen 80: server_name example.com; return 301 https://$server_name/subdirectory; } server { listen 443; server_name example.com; location = / { return 301 https://$server_name/subdirectory; } } The = / specifiers means a full match, so it matches only the exact root ...


7

As it stands, you're redirecting all traffic to https, which is good for http traffic, but for https traffic is quite pointless, and results in a redirection loop. What is happening right now is the following: http -> https -> https -> https -> https -> https ... and so on for quite a bit, up until the point where your browser tells you, "it's enough, we won'...


6

This is the best rule I found and it is working to me: rewrite ^/content /? permanent; Meaning, rewrite all requests: starting in the root of the domain (^/) than followed by "content" to the root (index) of the site (/) removing all query strings it might have (?) and show a 301 redirect in the headers. I guess that's it.


6

Second way is better... server { listen 80; server_name www.domain.com; return 301 $scheme://domain.com$request_uri; } Why Let me quote directly from the official Nginx wiki at Pitfalls and Common Mistakes: By using the built-in variable $request_uri, we can effectively avoid doing any capturing or matching at all, and by using the return ...


6

The answer could be much much simpler. Just copy this into bottom of httpd.conf (usually located at /etc/httpd/conf) <VirtualHost *:80> ServerName localhost Redirect 403 / UseCanonicalName Off UserDir disabled </VirtualHost> <VirtualHost *:80> ServerName www.example.com DocumentRoot /var/www/html </VirtualHost> Then only when ...


6

Right now any and all requests are going to hit this server block: server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; server_name _; # This doesn't do anything rewrite ^ $scheme://www.example.com$request_uri permanent; # Rest of file irrelevant } Because: no server block has a valid server_name (...


5

Cleanest way is to use a map block. map $uri $new { /aa /en/aa; /bb /newstuff/bb; ^/cc/(?P<suffix>.*)$ /$suffix; /john /users/john/; } server { server_name www.example.com; rewrite ^ $new permanent; }


5

It's not necessarily Apache's configuration that's doing this - is Apache handing the request off to a dynamic content generator? Look for two things in your Apache config; Redirect, and RewriteRule directives that have an R flag. If those aren't in place, then Apache isn't doing the redirect (with the exception of /directoryname redirecting to /...


5

Yes, you can ABSOLUTELY do it with NGINX! Simply install an exception handler, a.k.a. error_page, to post-process required responses. Make sure to set it in such a way as to prevent the error page from modifying the HTTP Status Code, e.g., don't use the = parameter (or use it to hardcode whichever code you desire). Make sure to return a response with a ...


Only top voted, non community-wiki answers of a minimum length are eligible