Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 17, 2021 11:53 am GMT

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 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 OnRewriteBase / # if it's lives under subdirectory, add the directory accordinglyRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME}/index.html !-fRewriteCond %{REQUEST_FILENAME}/index.php !-fRewriteRule . 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.


Original Link: https://dev.to/katzueno/very-simple-php-redirect-5cf6

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To