Redirect all sub-pages with .htaccess not working

Hello again experts.

I am really struggling to get my head around what should apparently be a simple rewrite.

All mobile/tablet users clicking on www.thealexandertechnique.co.uk get successfully redirected to https://mobile.thealexandertechnique.co.uk

The same users clicking on i.e. www.thealexandertechnique.co.uk/costs.html don’t get redirected. This is driving me nuts and I’ve tried what seems like a zillion suggestions from different websites.

This is the relevant bit of my current .htaccess file. I can post the whole thing if it helps.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.thealexandertechnique\.co\.uk\.*$
RewriteRule ^ https://www.mobile.thealexandertechnique.co.uk

Any and all suggestions and explanations gratefully received.

And, yes I know I should be making the main site responsive :slight_smile: however, I have also being trying and failing with that too. The sidebar really screws things up.

Once I can get the above working if indeed it’s actually possible, I’ll be seeking help with the responsiveness.

That snippet should work as expected. Please post the whole file :slightly_smiling_face:

I think that ^ only Rewrites the domain name and not any trailing file names.

…because the .htaccess I use has the following:

	RewriteRule ^(.*)$ index.php/$1 [L]

Try this:

RewriteRule ^(.*)$ https://www.mobile.thealexandertechnique.co.uk/$1 [L]

Well, it’s an odd one for sure. In regex the ^ matches the beginning of a string. So the regular expression ^ matches any string that has a beginning, so every string :slight_smile:

The effect is exactly the same as the regular expression ^(.*)$, but faster (because it knows faster that it has a match and can stop evaluating the rest of the string).

Hi @John_Betong . Thanks for the speedy response. :slight_smile: I’ve tried that before and it doesn’t work.

RewriteRule ^(.*)$ https://www.mobile.thealexandertechnique.co.uk/$1 [L]

As I’ve said, this has been driving me nuts.

.htaccess is now:

RewriteCond %{HTTP_HOST} ^www\.thealexandertechnique\.co\.uk\.*$
RewriteRule ^(.*)$ https://www.mobile.thealexandertechnique.co.uk/$1 [L]

Here’s the whole .htaccess

# Use HTTP Strict Transport Security to force client to use secure connections only

<IfModule mod_headers.c>
Header set Strict-Transport-Security "max-age=72000; includeSubDomains; preload" env=HTTPS
<FilesMatch "\.(js|css|xml|gz)$">
Header append Vary: Accept-Encoding
</FilesMatch>
Header set X-XSS-Protection: "1; mode=block"
</IfModule>

Header always append X-Frame-Options SAMEORIGIN
Header set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy no-referrer

RewriteEngine on
# HTTP to HTTPS redirect
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]

# Canonical www redirect
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R,L]


<IfModule mod_rewrite.c>
## Redirect to mobile site

# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:www.thealexandertechnique.co.uk]

RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile}       !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} 
"acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg- 
d|lg-g|lge-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT}  "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec- 
|newt|noki|opwv" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} 
"palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph- 
|symb|t-mo" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap- 
|wapa|wapi" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" 
[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]

# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST}          !^mobile\.
# Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) 

# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE}        !^.*mredir=0.*$ [NC]

# Now redirect to the mobile site

RewriteCond %{HTTP_HOST} ^www\.thealexandertechnique\.co\.uk\.*$
RewriteRule ^(.*)$ https://www.mobile.thealexandertechnique.co.uk/$1 [L]]
</IfModule>

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>

Thanks for the help.

I reckon that this:

RewriteCond %{HTTP_HOST} ^www\.thealexandertechnique\.co\.uk\.*$

should be this:

RewriteCond %{HTTP_HOST} ^www\.thealexandertechnique\.co\.uk$ [NC]

since .co.uk at the end of your URL will never be followed by anything, and I’ve added [NC] there to make it case insensive (so it also works on www.TheAlexanderTechnique.co.uk).

Also, the ]] at the end of

RewriteRule ^(.*)$ https://www.mobile.thealexandertechnique.co.uk/$1 [L]]

That should be just one ].

Lastly I’d update the https/www block to the following:

# http://thealexandertechnique.co.uk => https://www.thealexandertechnique.co.uk
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]

# http://www.thealexandertechnique.co.uk => https://www.thealexandertechnique.co.uk
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

# https://thealexandertechnique.co.uk => https://www.thealexandertechnique.co.uk
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]
  1. Redirect HTTP non-www to HTTPS www in one go, instead of in two steps (first https, then www)
  2. Use 301 status code for permanent redirects, instead of the default 302 for temporary redirect, so link juice is attributed correctly.

(Save the R=301 for last, keep it out at first when you test, so it stays [R,L]. Once you’ve changed the code and verified all works, then change R to R=301. The reason is that browsers cache 301 responses, so if the redirect is wrong, all users hitting your site will have wrong information in their cache. It’s quite hard to correct for that.)

1 Like

@rpkamp, I’m puzzled. Are the first 2 RewriteRules missing a www ?

1 Like

Correct, the first one is. I’ve added it. Thanks for the heads up! :slight_smile:

The second one doesn’t need it, as the URL already contains www.

1 Like

Hi @rpkamp Thanks for the input.

Still not working though. :banghead:

Same issue. site root redirects, sub-pages don’t.

current .htaccess

RewriteCond %{HTTP_HOST} ^www\.thealexandertechnique\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.mobile.thealexandertechnique.co.uk/$1 [L]

If I comment out the above, the mobile redirection from site root clicks stops, so I know the rewrite engine is working.

Could this be a config issue with the v-host for the site?

And you don’t have a cookie that indicates you don’t want mobile?

If you don’t, I have no idea. It really should work :man_shrugging:

Yes. I think it’s time to determine if it’s your use case, and if so what, why, and most importantly how to fix it.

My completely wild guess is that you developed on your local machine. Maybe you did something like edit the HOSTS file that you’ve since forgotten doing? When you use any other machine than one you developed on, is this still a problem ?

1 Like

Driving me totally nuts.

Tried everything; removing web-browsers from my phone and re-installing. Doing the same on my wife’s phone and on the cat’s too. :upside_down_face:

Thought I’d missed something really obvious or fundamental yet I’d manged to get the mobile redirect working ( with the help of google ) except for the sub-pages bit hence my shout for help.

Might see what my web-host says. Maybe they can see something in the logs that explains it.

Thanks for everyone’s time and efforts. Much appreciated.

Will obviously update if I find out the cause.

3 Likes

Update after some sunshine in Thailand… :slight_smile:

Response from my hosting provider:

The problem is not caused by the server configuration, but rather by the fact that the mobile site and the main site have different URL rewrite rules.

The mobile site rewrites the URL’s to https://www.mobile.thealexandertechnique.co.uk/index.php/URI, where the main site only uses https://thealexandertechnique.co.uk/URI, thus the rules that you have added to the .htaccess are not working for the sub-pages (when subpage is directly accessed via URL) and only the redirect for the main page is working.

I tried to configure the redirect for the mobile site using a regex, but I was not able to find the exact pattern to full fill all of the requirements for the URL rewrite.

Does that make sense to anyone? I suspect that they don’t understand. :frowning: as I’m not trying to rewrite main site URL’s to equivalent mobile URL’s.

Frustrating.

OK. Problem solved!

Apparently my host, Siteground, moved from Static Cache to NGINX Direct Delivery which means standard apache rewrites don’t work! Wish someone had advised me sooner though. :roll_eyes:

Thanks for all your suggestions and help. :slightly_smiling_face:

3 Likes

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