HTTPS Redirect Breaks Rewrite Rule - .htaccess

I am having a little trouble trying to understand what exactly is going on with my redirects.
This works good:
RewriteEngine on
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
RewriteRule .* - [F]
# redirect to http subdomain
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^www\.m\.pets4you\.com$ [NC]
RewriteRule ^(.*)$ http://m.pets4you.com/$1 [L,R=301]
##################################
RewriteRule ^dogs/(.*).htm?$ breeds.php?page=$1 [L]
RewriteRule ^cats/(.*).htm?$ breeds.php?page=$1 [L]
RewriteRule ^(.*).htm?$ pets4you.php?page=$1 [QSA,L]
But this....
RewriteEngine on
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
RewriteRule .* - [F]
# redirect to http subdomain
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^www\.m\.pets4you\.com$ [NC]
RewriteRule ^(.*)$ https://m.pets4you.com/$1 [L,R=301]
##################################
RewriteRule ^dogs/(.*).htm?$ breeds.php?page=$1 [L]
RewriteRule ^cats/(.*).htm?$ breeds.php?page=$1 [L]
RewriteRule ^(.*).htm?$ pets4you.php?page=$1 [QSA,L]
Redirects to:
https://m.pets4you.com/pets4you.php?page=https://m.pets4you.com/dogs/breedname.htm
Any ideas why?
It is ignoring the first two rules and going directly to the last rule.
I do know of a workaround, but entails rewriting the php code to do it worpdress style, and change .htaccess.
The site is old and outdated and will be replaced with an actual wordpress site. But for now I am being asked to do this to this site. And a first time experiencing this kind of issue.
I personally despise .htaccess as it is not my best area. Any help would be greatly appreciated.
Thanks!

As always, the moment I ask a question I figure it out. A matter of some hardcoded URL's without https causing issues. Forgot to check the main index page. As I usually have the URL as a PHP variable.

Related

htaccess 301 redirect root to subdirectory

Trying to get
www.example.com
to go directly to
www.example.com/forum
How can I do this with this configuration? Thanks in advance.
Edit:
Right now I have added below content in .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
RewriteRule ^(.*)$ /forum/$1 [L,R=301]
I want result like
https://www.example.com/topic/topic-url/
to
https://www.example.com/forum/topic/topic-url/
With your shown samples, could you please try following. Please clear your browser cache before testing your URLs. Your tried rules will be creating an infinite loop hence it may not be working, I have added a condition if uri doesn't start from forum then only proceed with redirection.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{REQUEST_URI} !^/forum [NC]
RewriteRule ^(.*)$ /forum/$1 [L,R=301]

HT Access https and .co.uk to .uk

I'm going around in circles with this and could do with someone guiding me through it .
I am trying to redirect any call to .co.uk or uk, as well as http to https:// , with the added complication of codeigniter and removing /index.php.
I have this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.uk/$1 [R=301,L]
But access to http://example.uk does not redirect a) to https.. and b) seems to break any CSS links
http://www.example.co.uk seems to redirect to http://www.example.uk/www.example.co.uk
Really can't get my head around this.. would appreciate some help!
Assuming you want to always redirect to https://www.planandshop.uk/ I think you don't need to bother with this:
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
since you are explicitly redirecting to https://www.example.uk at the end anyway. Just have to make sure that the final rule will match.
To handle both example.co.uk and example.uk you could write the rule like this:
RewriteCond %{HTTP_HOST} ^(www\.)?example(\.co)?\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.uk/$1 [R=301,L]
Just like you made the www. optional.

Multiple redirects in .htacces (http to https; .html to .php; index to /)

For re-directing all requests without “www.” or with “http:” I was using the following .htaccess settings since activating SSL:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{SERVER_PORT} !^443
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.php/(.+)$ /$1 [R=301,L]
RewriteRule ^index\.php/?$ / [R=301,L]
I now noticed that my former redirections of index.php to / don’t work anymore, if the requested URL is http.
Also, I now need to change current .html files (half of my website) to .php and add this:
RewriteRule ^(.*)\.html$ $1.php [L]
I would like to use as little rewrites as possible to maximize SEO effects, but I got stuck trying around. Any help would be greatly appreciated.
After trying around for some more hours, I think I came to an elegant solution:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)index\.php/?$ [OR]
RewriteCond %{REQUEST_URI} ^(.*)index\.html/?$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/ [R=301,L]
# index.php or index.html are sent to https://www.domain.tld/
RewriteCond %{REQUEST_URI} ^(.*)\.html$
RewriteRule ^(.*)\.html$ https://www.%{HTTP_HOST}/$1.php [R=301,L]
# .html is changed to .php and additionally redirected to https://www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{SERVER_PORT} !^443
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Everthing not redirected before, gets an additional www. and/or is redirected to https.
I'm happy about comments and recommendations to improve further, if neccessary. Hope this is helpful to anyone.

How can I make a specific page not https

So, I use a chat script called AJAX-Chat
it works fine, but only when I take away the part of my .htaccess file that makes my site always go to https://
I need a way for a SPECIFIC directory to not be https://
My .htaccess code:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Add this above your other rules:
RewriteRule ^(Directory_to_exclude)($|/) - [L]
it will exclude that directory from forced to HTTPS.
Alternate(ther are lots)
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !exclude_me [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

.htaccess Wildcard Subdomains

I'm trying to implement a solution using .htaccess and wildcard subdomains so that
http://subdomain.example.com is mapped to http://example.com/index.php/accounts/subdomain/. My rules look something like:
RewriteCond %{HTTP_HOST} !www.example.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).example.com [NC]
RewriteRule ^(.*/) /index.php [PT,L]
Which works, but disregards everything else. When I try appending anything to the rule e.g:
RewriteRule ^(.*/) /index.php/hello [PT,L]
I get a 500 internal server error. How do I get this working?
You probably need to exclude the index.php from your rule:
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com$ [NC]
RewriteRule !^index\.php($|/) index.php/accounts/%2%{REQUEST_URI} [PT,L]
This is an adaptation of the code I use to redirect subdomains on my own site. I make no claims to it being best practice but it works;
RewriteCond %{HTTP_HOST} ^(.*)\.com$ [NC]
RewriteCond %1 !^(www)\.example$ [NC]
RewriteRule ^.*$ http://www.example.com/index.php/accounts/%1/ [R=301,L]
Try changing your RewriteRule to
RewriteRule ^/(.*)$ /index.php/accounts/%1/$1 [PT]
That will rewrite the URL to one that includes the subdomain and the original request URI.
EDIT: maybe it needs to be
RewriteRule ^(.*)$ /index.php/accounts/%1/$1 [PT]
as mentioned in the comments.

Resources