2

I have a site that stores an index.php for desktop computers and a mobile.php for mobile devices. Both files are in the same directory (say /var/www). As default the user accesses index.php and if his screen width is less than 900 px he gets redirected to mobile.php.

To load various content within index.php or mobile.php there are menu links passing one or two php variables which state the directory where the content is stored.

for example, if I want to display the location I load the page from ./Contact/Location/Location.php into index.php.

The problem is here that .htaccess does not know if I'm working with the default or with the mobile site, so the following rules do not work together:

RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ index.php?id=$1&id2=$2 [QSA]

RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ mobile.php?id=$1&id2=$2 [QSA,L]

Is there a way to make it working? The main advantage of having it like this is that I don't have to maintain two sites. The layout is made entirely within mobile.php and index.php.

2
  • Would you be up for different URLs for mobile and desktop like /1234/5678/ for desktop and /m/1234/5678/ for mobile? You say that users are getting redirect do mobile, so then it would just be a matter of changing the code that does the redirect to use the /m/ URLs. Nov 20, 2020 at 18:58
  • This would seem a very bad way of doing it - why not incorporate the switching logic straight into php (by way if conditional includes?), or better yet use CSS to render the site appropriately per device. Also see stackoverflow.com/questions/930431/… for how to use environment variables in Apache config so you can select the rewrite rule on the variable in .htaccess - but yuk.
    – davidgo
    Nov 20, 2020 at 19:13

1 Answer 1

1

You need to make the mobile and desktop URLs different to resolve this ambiguity. Perhaps with an /m/ (mobile) and/or /d/ (desktop) prefix?

For example, /m/Contact/Location/ for your mobile site and the default /Contact/Location/ for the desktop site. However, since there is a strong preference for "mobile-first" these days, maybe this should be /d/ for desktop and no prefix for mobile?

To load various content within index.php or mobile.php there are menu links passing one or two php variables which state the directory where the content is stored.

To be honest, when I read this I assumed you were already differentiating the mobile/desktop URLs in someway? I guess not.

You need to allow users to manually switch between desktop and mobile. You could perhaps store this user preference in a cookie/session variable (although that's not strictly necessary if the URLs are different).

You could rewrite the URL based solely on a mobile/desktop cookie (readable from .htaccess), however, search engines generally don't crawl with cookies (Google does not) so this will likely create problems with indexing.

As default the user accesses index.php and if his screen width is less than 900 px he gets redirected to mobile.php

How do you handle search engine crawling currently? If you don't change the URL then I imagine Google could be redirected on every page request when searching using a mobile user-agent?

(Aside: "less than 900 px" - I assume that's CSS px, not actual screen pixels?)

Have you considered an entirely responsive design instead? One URL for both mobile and desktop. index.php for both mobile and desktop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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