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

To redirect all pages to another domain's homepage using nginx rewrite rules in PHP, you can add the following code to your nginx configuration file:

main.php
server {
    listen 80;
    server_name old_domain.com;
    return 301 $scheme://new_domain.com;
}
99 chars
6 lines

Replace the old_domain.com with your current domain name and new_domain.com with the new domain name to which you want to redirect all your website's pages.

You can save the changes and reload your nginx configuration to apply the new settings:

main.php
sudo service nginx reload
26 chars
2 lines

After making this change, all pages from the old domain will be redirected to the homepage of the new domain with a 301 permanent redirect.

related categories

gistlibby LogSnag