write nginx rewrite rule to 301 redirect all page to another domain in php

To redirect all pages of a website hosted on nginx to another domain using PHP, you can use the following rewrite rule in your nginx configuration file:

server {
    listen 80;
    server_name your-domain.com;
    
    return 301 https://new-domain.com$request_uri;
}
115 chars
7 lines

This will redirect all requests to your-domain.com to https://new-domain.com with a 301 status code, indicating a permanent redirection.

Make sure to replace your-domain.com and new-domain.com with your own domain names.

You'll also need to make sure that PHP is properly installed and configured on your server. Once it is, you can create a index.php file with a 301 redirect like this:

main.php
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://new-domain.com".$_SERVER['REQUEST_URI']);
exit();
?>
127 chars
6 lines

This PHP code will send a 301 header with the new domain name and the requested URI, effectively redirecting all pages of the website to the new domain.

Note that this approach will only work if your server is properly configured to use PHP files.

gistlibby LogSnag