Justin01
Justin01

Reputation: 288

Htaccess redirection all while hiding directory

I want visitors who are landing on domain.com from their desktop to be transferred to domain.com/desktop, all while hiding the "desktop" part in the URL.

Lastly, if they visit domain.com from their mobile, I want them to be transferred to domain.com/m. In this case the "m" can stay I guess, I don't care.

My question is; is the redirection in the .htaccess or in the index.php of my public_html?

EDIT : My /desktop/ directory's htaccess contains this :

RewriteEngine On
RewriteRule ^inc/.*$ index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

The root's .htaccess contains this :

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule !^desktop/ /desktop%{REQUEST_URI}  [L]

And my public_html's index.php contains :

include 'mobile_detect.php';
$detect = new Mobile_Detect();
if ($detect->isMobile()) {
    header("Location: http://domain.com/m"); exit;
}else{
    header("Location: http://domain.com/desktop"); exit;
}

I feel like I'm doing something very wrong, I just can't figure out what it is. Help?

Thank you very much.

Upvotes: 0

Views: 55

Answers (1)

Peter Gordon
Peter Gordon

Reputation: 1077

Have a look at this duplicate answer - it will redirect mobile users to m.domain.com, which could be a better system as you could use another VirtualHost to entirely separate the mobile website so it wouldn't cause any redirect loops.

As for the redirect to /desktop, why is that necessary? You could simplify this to two VirtualHosts:

(VirtualHost config is normally stored in /etc/apache2/sites-enabled/000-default.conf)

<VirtualHost *:80>
    ServerName m.domain.com
    DocumentRoot /var/www/mobile/
</VirtualHost>
<VirtualHost *:80>
    ServerName domain.com
    ServerAlias www.domain.com
    DocumentRoot /var/www/desktop/
</VirtualHost>

Upvotes: 1

Related Questions