Fli
02-24-2014, 01:23 PM
Hi, i have been searching for good PHP redirect which will redirect urls matching certain phrasse.
Example:
URL contains "?en"
and it will be redirected to english_page.php
OR url contain some badwords like "drug" and it will be redirected to 404.php ...
So i completed this out of what people put on internet:
Example: https://instantcpanelhosting.com/info.php
the code behind it:
<?php
if (strpos($_SERVER["REQUEST_URI"],'?fr') !== false) {
header( 'Location: https://instantcpanelhosting.com/french.php' ) ;
die();
}
if (strpos($_SERVER["REQUEST_URI"],'?sp') !== false) {
header( 'Location: https://instantcpanelhosting.com/spanish.php' ) ;
die();
}
?>
the code must be pasted on the second line right after starting <?php
else it can output "headers already been sent" error
--------------------
If you want to redirect AND also use friendly URLS, you can use below code.
Example:
/english.php - has contents
/en - no file exist, but visitors will see english.php as /en
Options -Multiviews
RewriteEngine On
RewriteBase /
# Force search engines to use yourdomain.com
RewriteCond %{HTTP_HOST} !^yourdomain.com$
RewriteRule ^(.*) http://yourdomain.com/$1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
# Redirect old pages
RewriteRule ^english.php$ /en [R=301,L]
RewriteRule ^russian.php$ /ru [R=301,L]
# Specify search friendly URLs
RewriteRule ^en$ /english.php [L]
RewriteRule ^ru$ /russian.php [L]
Example:
URL contains "?en"
and it will be redirected to english_page.php
OR url contain some badwords like "drug" and it will be redirected to 404.php ...
So i completed this out of what people put on internet:
Example: https://instantcpanelhosting.com/info.php
the code behind it:
<?php
if (strpos($_SERVER["REQUEST_URI"],'?fr') !== false) {
header( 'Location: https://instantcpanelhosting.com/french.php' ) ;
die();
}
if (strpos($_SERVER["REQUEST_URI"],'?sp') !== false) {
header( 'Location: https://instantcpanelhosting.com/spanish.php' ) ;
die();
}
?>
the code must be pasted on the second line right after starting <?php
else it can output "headers already been sent" error
--------------------
If you want to redirect AND also use friendly URLS, you can use below code.
Example:
/english.php - has contents
/en - no file exist, but visitors will see english.php as /en
Options -Multiviews
RewriteEngine On
RewriteBase /
# Force search engines to use yourdomain.com
RewriteCond %{HTTP_HOST} !^yourdomain.com$
RewriteRule ^(.*) http://yourdomain.com/$1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
# Redirect old pages
RewriteRule ^english.php$ /en [R=301,L]
RewriteRule ^russian.php$ /ru [R=301,L]
# Specify search friendly URLs
RewriteRule ^en$ /english.php [L]
RewriteRule ^ru$ /russian.php [L]