Code Snippet: Redirect without editing .htaccess file

You need to do a 301 or 302 redirect, but have no access to the server configuration files or the .htaccess file? Unfortunately not all hosters allow you to edit the .htaccess file, where you could easily set a redirect via the line:

Redirect /oldpage.html [R=301,L]

I recently had exactly that problem, but PHP has some nice function, that helped me out: header(). The function allows you to send a raw HTTP header to the client browser. A 301 or 302 redirect is stored in the header section and so we can set our redirect without ever touching the .htaccess:

< ?php 
    header("HTTP/1.1 301 Moved Permanently"); 
    header("Location: http://YOURDOMAIN.COM"); 
    header("Connection: close"); 

All you have to do is paste that code into the index file on the domain you want to make the redirect from and edit the line that specifies the location with your URL and maybe the redirect itself (e.g. for a 302, etc) – that’s it, now you have your redirect!

Similar Posts: