Mobile redirect using htaccess works on http and not on https

Hi,

I am using the following script for Mobile Redirect.

RewriteEngine On
RewriteCond %{QUERY_STRING} !^desktop
RewriteCond %{HTTP_USER_AGENT} “android|blackberry|googlebot-mobile|iemobile|iphone|ipod|#opera mobile|palmos|webos” [NC]
RewriteRule ^$ https://www.mysite.com/mobile-site [L,R=302]

This script works when a user type “http://mysite.com” in mobile browser.

This script does not work when a user type "https://mysite.com in mobile browser.

Please help me to redirect on both condition with http and with https.

Waiting for response

A2A,

Off the top, I see nothing glaringly wrong with your mod_rewrite code. Technically, however, I’d use

[code]RewriteEngine on

RewriteRule ^$ https:www/mysite.com/mobile-site/ [L,R=302]
// Note the trailing slash on the URL[/code]

Just why are you using a temporary redirection rather than permanent (301)?

Back to your problem:

  • Hosting companies used to use different directories to hold http and https code but that’s seems to NOT be the case these days.
  • I thought it possible that mod_rewrite would not be enabled for https requests but a check of Apache’s mod_rewrite documentation shows no such limitation (I’ll leave it to you to check with your host).

As a suggestion (which should NOT impact your problem but give a better solurion), why not replace your RewriteRule with

RewriteCond %{REQUEST_URI} !mobile-site RewriteRule .? https://www.mysite.com/mobile-site%{REQUEST_URI} [R=301,L]

Obviously, this would retain any request URI made as well as avoiding redirections if already made to mobile-site (but will not ensure https - yet - with this code).

In my htaccess i hhave found this.

# 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

Can this create such a problem?

A2A,

In short, yes. I’ve responded to so many over the years about the WP code that it’s become second nature (and I wish other would search here before asking a repeat question). Oh, well …

It is a pet peeve of mine that “webmasters” would waste server resources repeating a test that they should know to comment out once and, if it works, DELETE the wrapper. I even built a “rant” for that:

[rant #4][indent]The definition of an idiot is someone who repeatedly does the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an … wrapper is the same thing in the webmaster world. DON’T BE AN IDIOT! If you don’t know whether a module is enabled, run the test ONCE then REMOVE the wrapper as it is EXTREMELY wasteful of Apache’s resources (and should NEVER be allowed on a shared server).[/indent][/rant 4]

I have a problem with the RewriteBase directive as they’re generally in the DocumentRoot so RewriteBase / is redundant. If it’s not in the DocumentRoot, the / must be reset to the actual location (or simply DELETED). Oh, yeah, I haven’t used that standard rant in ages!

[standard rant #5][indent]I find myself repeating that the RewriteBase directive was created to UNDO a mod_alias redirection (e.g., Redirect) so, if you’ve not got any of those, there is no need to use RewriteBase. More importantly, using it inappropriately can cause mod_rewrite directives to be based on something you’re not aware of so, if you don’t NEED it, DON’T use it/indent][/standard rant #5]

It’s just too bad that this forum software won’t indent for me. :cry:

The first RewriteRule is redundant, too, as index.php is a file (first condition in the next rule set) so it, too, should be DELETED.

Finally, the second RewriteRule does not accommodate the directory request (which should already be handled by a DirectoryIndex directive) but that can be handled by adding a ? after the dot character. Then the absolute redirect to /index.php will first look at the SERVER’s root before looking at your DocumentRoot.

If those do not “create such a problem” for you, don’t fix it. On the other hand, if you’re as much a stickler for details (and optimizing code), then fix them.

RewriteEnding on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .? index.php [L]

Short & sweet, eh?

Just be glad that you didn’t employ the :fire: EVERYTHING :fire: atom (inappropriately) as my rant for that is a doozy!

OMG! The forum’s icons display during adding/editing a post but not in the board! What’s wrong with that picture?

Regards,

DK

4 posts were split to a new topic: Google showing Duplicate Title and description Tags Errors in GWT

A post was merged into an existing topic: Google showing Duplicate Title and description Tags Errors in GWT

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.