Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created a PHP file which should detect if mobile or tablet. I gleaned the following code from a web search as I don't know any coding although years ago COBOL was my breadwinner!

The code in my PHP file is;

$browser = cge_utils::get_browser();
if( $browser->isMobile() )

I don't know if it will work but all things are possible - eventually!

I have been searching all day without luck to find out how to get this file read by the htaccess file

Can someone show me how to write the correct code and or say if this PHP script would work?

I currently use Java script in my home page but people say this slows down the upload speed for mobiles.

share|improve this question

The .htaccess file is for extended server configuration and as such you can't run php code from within it. It looks like all you need to do is put the code in your question in the pages that you want to redirect from.

share|improve this answer
I was wondering if there is a way to get htaccess to read the PHP file as I would like to put the php file server side. I have a set up for my html index page which works well but I have been advised to put the detection and redirection server side or wherever so that the redirection is done before reading the index page. – user126707 Jul 1 '12 at 17:05

To do this, simply add a 302 found redirect from the index to this PHP, and make sure it's being handled as a CGI script. I am way too lazy to write it for you, but that is the general method.

You can't really include them in .htaccess files, because they are for server configuration.

share|improve this answer
I'll have a go at this. I've set up a 301 redirect. Hopefully I just need to change a digit. Thanks – user126707 Jul 1 '12 at 17:14

If the objective is to use this PHP code at the beginning of every response served from the same location that the .htaccess file is responsible for, then auto_prepend_file might be exactly what you need. It is a php.ini core directive which can be set within the .htaccess file like so:

php_value auto_prepend_file "/path/to/redirects.php"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.