.htaccess add tablet redirect - .htaccess

Using the .htaccess file, I wish to redirect tablet traffic to a tablet optimised site.
Regular URL: waxxxed.com.au
Tablet Optimised URL: waxxxed.com.au/tablet/
I've tried the following code but it doesn't work.
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ipad [NC]
RewriteCond %{HTTP_HOST} !^ipad\. [NC]
RewriteRule ^ http://waxxxed.com.au/tablet/ [L,R=301]
x3 questions...
This code doesn't work. Appears to create a loop. Not sure, but that may be related to my existing .htaccess code provided below.
I want to redirect ALL tablets (not mobiles) not just iPads. How do I capture other tablet brands?
Can you recommend any further optimisation of my .htaccess coding.
Full current .htaccess file (it works!)...
<IfModule mod_expires.c>
# Enable cache expirations
ExpiresActive On
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year”
</IfModule>
# force redirect of html to no-extension
RewriteCond %{THE_REQUEST} ^GET\s.+\.html
RewriteRule ^(([^/]+/)*[^/.]+)\.html$ http://waxxxed.com.au/$1 [R=301,L]
# www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.waxxxed.com.au
RewriteRule (.*) http://waxxxed.com.au/$1 [R=301,L]
# Redirect from / to non-/
RewriteEngine On
RewriteRule ^(.*)/$ $1 [R=301,NC,L]
# parse file as file.html
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(([^/]+/)*[^/.]+)$ $1.html [L]
# refer not found content to 404 page
ErrorDocument 404 /404page.html

The reason why it creates a loop is because you're checking that the hostname doesn't start with ipad, and since you're redirecting to waxxxed.com.au, it's never going to start with ipad. Instead check for the request URI starting with /tablet/:
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ipad [NC]
RewriteCond %{REQUEST_URI} !^/tablet/. [NC]
RewriteRule ^ http://waxxxed.com.au/tablet/ [L,R=301]
As for #2, you need to lookup user-agents for other tablets. In general, you want to find "android" but not "mobile":
RewriteCond %{HTTP_USER_AGENT} android [NC]
RewriteCond %{HTTP_USER_AGENT} !mobile [NC]
RewriteCond %{REQUEST_URI} !^/tablet/. [NC]
RewriteRule ^ http://waxxxed.com.au/tablet/ [L,R=301]
The rest of your htaccess file looks fine as long as it's doing what you expect, anything would be really nit-picky.

Related

Need help correctly configuring .htaccess for HTTPS

I am trying to configure and write my .htaccess file correctly. There are some things I already tried, but somehow they dont all work out.
I am trying to accomplish the following redirects:
Redirect from HTTP to HTTPS for all folders, including subfolders
Remove ".html" endings so the url shows www.domain.com/photos instead of www.domain.com/photos.html
Remove "index.html" when on home page, so it only shows www.domain.com
Some things work, but when going in a subdirectory like www.domain.com/sub, the page changes from HTTPS to HTTP.
Also, when accessing the page via HTTPS, it doesnt correctly load the font, that is specified in the HTML file
()
See my code below:
<IfModule mod_rewrite.c>
AddType text/x-component .htc
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS. # i.e. http://www.example.com/foo/ to https://www.example.com/foo/ # The leading slash is made optional so that this will work either in httpd.conf or .htaccess context
# remove .html; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
RewriteRule (.*)\.html$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .html to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.html [L]
</IfModule>
I figured it out and it works so far:
<IfModule mod_rewrite.c>
AddType text/x-component .htc
RewriteEngine On
RewriteBase /
# Redirect to www
RewriteCond %{HTTP_HOST} !^www\.domain\.ch [NC,OR]
# Redirect to https
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^ https://www.domain.ch%{REQUEST_URI} [NE,R=301,L]
# remove .html; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
RewriteRule (.*)\.html$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .html to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.html [L]
</IfModule>

Force HTTP (no SSL) on a single page in a site where all pages are forced HTTPS

I know that there exists several posts asking about the exact same thing. I asked the same question again, since I've read each and every one of them, and tried the solutions. Maybe they worked for the O.Ps'es codes, but unfortunately didn't work for mine.
I really need to disable HTTPS on a single PHP page called play.php, so that the page is accessible via direct HTTP, or redirect to HTTP if directly requested via HTTPS.
I need to change https://example.com/play/blabla to http://example.com/play/blabla, while the rest of the site is forced HTTPS.
Here is my full .htaccess code:-
Header set Access-Control-Allow-Origin "*********"
ErrorDocument 404 /pagenotfound.php
ErrorDocument 403 /pagenotfound.php
Options -MultiViews
RewriteEngine on
RewriteBase /
Options +FollowSymLinks
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteRule ^search/(.*)$ viewgames.php?search=$1 [L,QSA,NC]
RewriteRule ^category/(.*)$ viewgames.php?cat=$1 [L,QSA,NC]
RewriteRule ^users/(.*)$ users.php?action=$1 [L,QSA,NC]
RewriteRule ^play/(.*)/$ play.php?gn=$1 [L,QSA,NC]
RewriteRule ^play/(.*)$ play.php?gn=$1 [L,QSA,NC]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-z]+)/?$ $1.php [L,NC]
I have a script called play.php which is rewrited using .htaccess to /play/ i.e if someone requests example.com/play/foobar, the request will be sent to example.com/play.php?gn=foobar.
I'm very new to .htaccess. I will really appreciate your help.
[Edit] I have updated the above code to show my full .htaccess. I hope it helps. BTW, the stars (*) on the first line of code is to hide my actual website address.
Here is full .htaccess with my comments:
Header set Access-Control-Allow-Origin "*********"
ErrorDocument 404 /pagenotfound.php
ErrorDocument 403 /pagenotfound.php
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
# it is important to keep www removal rule as first rule to avoid multiple redirects
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
# http->https if URL is not starting with /play/ or /play.php
RewriteCond %{HTTPS} !on
RewriteRule !^play(?:\.php|/.*)?$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE,NC]
# https->http if URL is starting with /play/
RewriteCond %{HTTPS} on
RewriteRule ^play(?:/.*)?$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE,NC]
RewriteRule ^search/(.*)$ viewgames.php?search=$1 [L,QSA,NC]
RewriteRule ^category/(.*)$ viewgames.php?cat=$1 [L,QSA,NC]
RewriteRule ^users/(.*)$ users.php?action=$1 [L,QSA,NC]
RewriteRule ^play/(.+?)/?$ play.php?gn=$1 [L,QSA,NC]
# make sure to check for presence of .php file before rewrite
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-z]+)/?$ $1.php [L,NC]
It is important to completely clear browser cache or use a new browser of testing the changes.
You can disable https to your play.php URI using the following RewriteRule
RewriteEngine on
RewriteCond ℅{REQUEST_URI} play\.php$ [OR]
RewriteCond ℅{REQUEST_URI} /play
RewriteCond ℅{HTTPS} on
RewriteRule ^ http://℅{HTTP_HOST}℅{REQUEST_URI} [L,NE,R]
This will redirect https://example.com/play.php to its non SSL version.
Since your rule already checks http urls , you can also add a condition to your rule to exclude the play.php from HTTPS redirection something like the following :
RewriteCond %{HTTPS} off
#redirect all to https except /play and /play.php
RewriteCond %{REQUEST_URI} !play
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

mysterious exception in htaccess rewrites

My htaccess files contains only a few lines that firstly remove the www and then add ".php" to the slug to get the correct php file, so
www.kalicup.fr/seo
should rewrite to
kalicup.fr/seo
and then display the file seo.php (without the .php extension displaying in the url itself)
at the moment
kalicup.fr/seo
correctly displays seo.php without showing the file extension.
however, when I try
www.kalicup.fr/seo
it rewrites to
kalicup.fr/seo.php
adding the .php extension in the url
so there's abviously a problem in my htaccess but I can't see it !
here's my code
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
ErrorDocument 404 /404.php
# redirect the url with www to url without
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?kalicup\.fr)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?kalicup\.co\.uk)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
# add .php to urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
can anyone see the problem ?
Use that in your .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
ErrorDocument 404 /404.php
# redirect the url with www to url without
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?kalicup\.(?:fr|co\.uk))$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# add .php to urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Only one test for .fr and .co.uk.
And -MultiViews: http://httpd.apache.org/docs/2.2/en/mod/core.html#options
The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.
http://httpd.apache.org/docs/2.2/en/content-negotiation.html

why does my ReWrite rule not rewrite?

I want to rewrite the urls like
site.com/eng?sentence=a-sentence
to be like:
site.com/eng/a-sentence
I only want the text shown in the brower url to change, I dont want a redirect.
RewriteCond %{THE_REQUEST} ^GET\s/+eng?sentence=([^/?&\s]+) [NC]
RewriteRule ^ /eng/%1 [NC,L,NE]
I have fiddled with it, comparing other rewrite rules i found for a long time, but just cant get it to work, And i really can't figure out why. I tried removing all other rewrite from my htaccess everytime i tested.
here is my htaccess file:
# Use PHP5.3 Single php.ini as default
AddHandler application/x-httpd-php54s .php
AddType application/x-httpd-php .htm .html
Options -Indexes -MultiViews
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{THE_REQUEST} ^GET\s/+eng?sentence=([^/?&\s]+) [NC]
RewriteRule ^ /eng/%1 [NC,L,NE]
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
preferably, if you could fix my rule rather than write your own one that I won't likely understand, that would be great.
I only want the text shown in the browser url to change, I dont want a
redirect.
To change URL in browser you need external redirect using R flag.
Also ? is special regex symbol and to match a literal ? you need to escape it like \?. Also you would need a ? in the target URI's end to strip off query string.
Your complete .htaccess:
RewriteEngine On
# actual oneto pretty URL - external redirect
RewriteCond %{THE_REQUEST} \s/+eng\.php\?sentence=([^&\s]+) [NC]
RewriteRule ^ /eng/%1? [L,NE,R=302]
# pretty URL to actual one - internal rewrite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^eng/([^/]+)/?$ /eng.php?sentence=$1 [L,QSA,NC]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
I highly recommend you to read: Apache mod_rewrite Introduction

Redirecting only a specific page request for mobile users using htaccess

I want to redirect requests for mobile users only to http://mydomain.com/video.html to http://mydomain.com/mobile/index.html using .htaccess
Currently it's a Wordpress site, so there is already some Wordpress stuff in my .htaccess file.
Here is what I have now:
AddHandler php5-script .php
# 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
# BEGIN Mobile redirect for the video
<IfModule mod_rewrite.c>
RewriteEngine on
# stuff to let through (ignore)
RewriteCond %{REQUEST_URI} "/mobile/"
RewriteRule (.*) $1 [L]
#
RewriteCond %{REQUEST_URI} !^/html/video.html.*$
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ /mobile/index.html [L,R=302]
</IFModule>
# END Mobile redirect
It is currently redirecting my iPad for all pages, and NOT for the page I want it to (video.html)
Am I missing something?
Try this one:
# BEGIN Mobile redirect for the video
<IfModule mod_rewrite.c>
RewriteEngine On
# stuff to let through (ignore)
RewriteRule ^mobile/ - [L]
# redirect /video.html to /mobile/index.html for mobile browsers
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^video\.html$ /mobile/index.html [L,R=302]
</IfModule>
# END Mobile redirect
1. Because you have WordPress it will be better to place this block BEFORE WordPress block.
2. There is no real need for # stuff to let through (ignore) rule if you have so little rewrite rules in this block (especially if you place whole block before WordPress one). But since I do not know for sure how your website works, I leave it for you to test.
3. I see you are using (.*) pattern in RewriteRule .. and then one extra pattern in RewriteCond to do actual URL matching. No need -- you can do all of that in RewriteRule itself.
For example, this:
RewriteCond %{REQUEST_URI} "/mobile/"
RewriteRule (.*) $1 [L]
can easily be replaced by
RewriteRule ^mobile/ - [L]

Resources