Very Simple PHP Redirect

If you’ve moved (are planning to move) to new domain & you don’t want to simply redirect to new domain.
You want to tell your site visitors that your domain has changed.

This simple PHP script shows very simple message that domain has changed for …


This content originally appeared on DEV Community and was authored by Katz Ueno

If you've moved (are planning to move) to new domain & you don't want to simply redirect to new domain.
You want to tell your site visitors that your domain has changed.

This simple PHP script shows very simple message that domain has changed for set amount of seconds, then redirected to the new domain.

It is also render 301 status code. So Google will know your page was moved, too.

It will fetch the current request URL, and redirect to the new domain with exact same URI.

By the way, it has Japanese text, too :)

https://github.com/katzueno/simple-php-redirect

This post was copied from Readme. If there is a newer version and newer description at GitHub readme, refer to GitHub's readme.

Requirement

  • For Apache server, mod_rewrite module should be enabled, and allowing to use .htaccess
    • You could set it in Apache config.
  • For Nginx server
    • PHP-FPM
    • Ability to set nginx config

How to install

  1. Change the config values such as new domain to index.php to your desired state.
  2. Place (or upload) index.php script to your old domain's webroot and clear all other files
  3. Edit your apache config, .htaccess or Nginx config so that all request will go through this index.php
  4. Enjoy the rest

Where to set

$newDomain: Enter the new domain name. Starts with https://

$siteNameEn: Site name in English.

$siteNameJa: Site name in Japanese.

$sec: Seconds to wait and redirect

Apache's .htaccess example

Create .htaccess file (if your server is allowing it), and enter the following.
If you want to place it under some directory, make sure to change RewriteBase.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase / # if it's lives under subdirectory, add the directory accordingly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>

Nginx config example

I assume you know about Nginx server and its config.
Add the following config in your nginx config.
I assume you've set ~ \.php($|/) location to send it to php-fpm.
Change location to sub-directory if you want just to place underneath, and absolute_redirect must be turned off.

location / {
    index index.php index.html index.htm;
    if (!-e $request_filename) {
        rewrite ^ /index.php last;
    }
}

Enjoy.


This content originally appeared on DEV Community and was authored by Katz Ueno


Print Share Comment Cite Upload Translate Updates
APA

Katz Ueno | Sciencx (2021-10-17T11:53:21+00:00) Very Simple PHP Redirect. Retrieved from https://www.scien.cx/2021/10/17/very-simple-php-redirect/

MLA
" » Very Simple PHP Redirect." Katz Ueno | Sciencx - Sunday October 17, 2021, https://www.scien.cx/2021/10/17/very-simple-php-redirect/
HARVARD
Katz Ueno | Sciencx Sunday October 17, 2021 » Very Simple PHP Redirect., viewed ,<https://www.scien.cx/2021/10/17/very-simple-php-redirect/>
VANCOUVER
Katz Ueno | Sciencx - » Very Simple PHP Redirect. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/17/very-simple-php-redirect/
CHICAGO
" » Very Simple PHP Redirect." Katz Ueno | Sciencx - Accessed . https://www.scien.cx/2021/10/17/very-simple-php-redirect/
IEEE
" » Very Simple PHP Redirect." Katz Ueno | Sciencx [Online]. Available: https://www.scien.cx/2021/10/17/very-simple-php-redirect/. [Accessed: ]
rf:citation
» Very Simple PHP Redirect | Katz Ueno | Sciencx | https://www.scien.cx/2021/10/17/very-simple-php-redirect/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.