need help with php redirection

by 11 replies
13
i want to redirect the link to some external link and i created the php file and uploaded it on the server .
php file looks like this
Code:
<?php
header('Location: http.........example.com' );
?>
i linked the url of file in the post so that when someone clicks on the link he will be redirected to example.com.
this code is not working as when i click on the link it is displaying post url/link url
and shows 404 error however when i check for link url only in browser it works fine.
i don't know why post url is coming infront of link url. please help ..
#programming #php #redirection
  • Do you have a .htaccess file that contains any mod_rewrite rules? I don't think your problem relates to your php code so it's likely to be a server side request conflict.
  • yes i have an .htaccess file and code for that is below :
    Code:
     
    
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
    please check if it is OK.
  • PHP is better. For debbugin's sake, remove the header etc and use a html meta redirect.
    • [2] replies
    • I'm assuming that the php script you have created with the header redirect code is located in the same directory as your .htaccess? If so then there is definitely a URI parsing conflict. Your base directory for re-writing is "/". Based on your mod_rewrite rules every request to your base directory (relative from your domain) is being re-written through index.php. I would suggest either moving your redirect script out of the base directory or creating a new rule in your .htaccess file to exclude it from being re-written.

      Example exclusion rule:

      Code:
      RewriteRule ^/redirect.php$ - [L]
      Where "redirect.php" represents the file name of your redirect script.
      • [1] reply
    • With regards to the OPs problem this will not help. His php script isn't even being parsed by the interpreter and is effectively being re-written on the uri request.
  • i tried both the methods ,
    firstly i put the redirect script in the "links" Directory i.e,
    domain.com/links/script.php when this url is alone entered it gets redirected to external url
    and when clicked from the post link the url becomes,
    /domain.com/links/script.php
    and ends with 404 error .
    after that i updated the .htaccess file and still the same thing happens.
    i cant understand what is happening ...
    • [2] replies
    • If this is a Wordpress installation, why not use one of the many WP redirect plugins?

      Page Links To
      WP Redirection
      Pretty Links

      and many others.
      • [ 1 ] Thanks
    • This sounds like WP itself modifying the link you are publishing. Probably because it's not being classed as an external link and is on the same domain as your WP installation. This will require modification of WP code and/or settings. I would take the advice of Jeff above and look into using a WP plugin as his advice would be much simpler than modifying the behaviour of WP.
      • [ 1 ] Thanks
  • thanks all for helping , i think now plugin is the only option for me.

Next Topics on Trending Feed

  • 13

    i want to redirect the link to some external link and i created the php file and uploaded it on the server . php file looks like this Code: <?php header('Location: http.........example.com' ); ?> i linked the url of file in the post so that when someone clicks on the link he will be redirected to example.com.