My htaccess show all GET parameters when redirecting non-www to www url - apache

I have an issue :
I made that my site redirect all non-www url to a www one for duplicate content issues, but on some pages with URL parameters, it breaks all the url and return a 404 error :
For example, this url :
https://www.mon-site.com/store/produits/sport/18/ballon-de-plage
When I delete the www., it redirects me to:
https://www.mon-site.com/?p=produits&category-slug=sport&product=18&product-slug=ballon-de-plage
And, obviously, it return a 404 error. It only happen on some pages I defined on my htaccess, but I don't understand why and how to fix it. This is my htaccess :
www/.htaccess :
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
#FORCE WWW
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#Enleve toujours le dernier slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
www/store/.htaccess
RewriteOptions inherit
RewriteRule ^produits/([a-zA-Z0-9-]*)$ index.php?p=produits&category-slug=$1 [L,QSA]
RewriteRule ^produits/([a-zA-Z0-9-]*)/([0-9]+)/([a-zA-Z0-9-]*)$ index.php?p=produits&category-slug=$1&product=$2&product-slug=$3 [L,QSA]
Thanks in advance for your help :)

Related

htaccess rewrite from a php file to subdomain

I would like to redirect from example.com/profile.php?UserName=xxxx to xxxx.example.com
xxxx contains a-z or 0-9.
I have tried bunch of codes to do it (some from this site) but none of them worked as I wanted it to be. Here is my current code:
RewriteEngine on
#this should redirect example.com/profile.php?UserName=x to x.site.com
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC]
RewriteRule (.*) %1/$1 [QSA,L]
#if link does not contain subdomain add www
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
You need to put all of your redirect rules before your internal routing rules. The rules with the R flag redirect.
RewriteEngine on
# This redirects the browser to include the "www"
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# This redirects the browser when profile.php is directly accessed
RewriteCond %{THE_REQUEST} \ /+profile\.php\?UserName=([a-z0-9-_]+)
RewriteRule ^ http://%1.example.com/? [L,R]
# this internally routes the XXX.example.com to the profile.php script
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC]
RewriteRule ^$ /profile.php?UserName=%1 [QSA,L]
Since there was not a good answer here.. In order to rename a php file and use the subdomain extention.. I had to edit my DNS settings.. This tutorial helped :http://www.mediacollege.com/internet/server/apache/mod-rewrite/subdomains.html

.htaccess redirect if domain = x.example.com

I have a site on a subdomain:
x.example.com
I put it on a subdomain because it's running on a separate server from the main site.
Using proxy for apache, I have now moved the site to example.com/x/ I want to make a 301 redirect from the subdomain like this:
Redirect 301 / http://example.com/x/
but I don't know how to make sure that it does not result in a redirect loop, as the page i'm redirecting to contains the same htaccess-file (as it's a proxy of the old file)
this is what I tried:
RewriteCond %{HTTP_HOST} ^x.example.com$ [NC]
RewriteRule ^(.*)$ http://staging.example.se/x/ [R=301,L,NE]
this is my full .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
RewriteCond %{HTTP_HOST} ^zenqa.herokuapp.com$ [NC]
RewriteRule ^(.*)$ http://staging.zenconomy.se/faq/$1 [R=301,L,NE]
</IfModule>
You're missing a backreference, you need a $1 to get the original request and pass it along to the redirect:
RewriteCond %{HTTP_HOST} ^x.example.com$ [NC]
RewriteRule ^(.*)$ http://staging.example.se/x/$1 [R=301,L,NE]

htaccess rewrite rule fails on https redirection

my htaccess look like this
RewriteEngine On
RewriteCond %{HTTP_HOST} !^localhost$
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^login/?(.*)$ login.php$1 [L]
RewriteRule ^register/?(.*)$ register.php$1 [L]
i want to redirect my pages login and redirect to https and so i added the code below
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(login|register)/?$
RewriteRule .*$ http://%{HTTP_HOST}%{REQUEST_URI} [L]
so when i type http://www.xyz.com/login/ it redirects to https://www.xyz.com/login/ everything was fine when i decided to redirect the pages to http which are not login or register and i wrote
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(login|register)/?$
RewriteRule .*$ http://%{HTTP_HOST}%{REQUEST_URI} [L]
after that i testted the url http://www.xyz.com/login/ again and it redirects me to https://www.xyz.com/login.php
any idea to redirect http://www.xyz.com/login/ to https://www.xyz.com/login/ and on that page when i click the link https://www.xyz.com/news/1/ redirect to http://www.xyz.com/news/1/
Try adding this right below the RewriteEngine On line:
RewriteCond %{HTTPS} off
RewriteRule ^(login|register)(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteCond %{HTTPS} on
RewriteRule !^(login|register) http://%{HTTP_HOST}%{REQUEST_URI} [L,R]
It also looks like you've pasted your rules wrong, the two blocks of rules are identical.

Redirecting to https with wildcard urls (htaccess)

I have the following problem. I have a site where some pages must be redirected to the https-secured equivalent of the link.
I tried several things but in the end it seems that the server ends up in a loop redirecting :(
Now I redirect every page to the https page but that is not what I want to.
This is the code I use right now:
RewriteCond %{http_host} ^www.domain.nl
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.nl/$1 [L,R=301]
I would like to have al urls starting with bo- have https.
examples:
http://www.domain.nl/bo-users -> redirects to https://www.domain.nl/bo-users
http://www.domain.nl/bo-groups -> redirects to https://www.domain.nl/bo-groups
but
http://www.domain.nl/about-us -> stays at http://www.domain.nl/about-us
It is clear to me I need some wildcards in the rewrite condition for all urls startin with bo-.
We are using apache on the server.
Hope someone can send me in the right direction.
Thanks,
Frank
Update after the tips from Anubhava.
I current;y have this htaccess but I can't get it working (even when clearing the cache in my browser)
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain\.nl$ [NC]
RewriteCond %{HTTPS} on
RewriteRule !^bo- http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]
RewriteCond %{HTTP_HOST} ^www.domain\.nl$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^bo- https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]
#rewrites een url to www.
RewriteCond %{HTTP_HOST} ^[a-z0-9-]+\.[a-z]{2,6}$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$0 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Hope for some extra help!
Thanks,
Frank
I just opened another call. The initial answer worked fine!
Use this rule:
# force HTTPS for bo- URIs
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.nl$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^bo- https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]
# force HTTP for non bo- URIs
RewriteCond %{HTTP_HOST} ^www.domain\.nl$ [NC]
RewriteCond %{HTTPS} on
RewriteRule !^bo- http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC,NE]

redirecting from www to non-www urls with .htaccess

I am redirecting an application with following code on my .htaccess file, the page is supposed to do the followings:
replace .php extension with .html
redirect from http to https
redirect from www to non-www urls
The extension .html is working fine and it is redirecting from http to https but the issue is to redirect from www to non-www, it is working properly on main url but when there is reference to a file then it is not working.
Say when i write www.ntestechnologies.com i get my desire url that is https://ntestechnologies.com but when i write www.ntestechnologies.com/index.html i get this https://www.ntestechnologies.com/index.html i don't need the www in this url as well please guide me, here is the code on htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.ntestechnologies\.com$
RewriteRule ^/?$ "https\:\/\/ntestechnologies\.com\/$1" [R=301,L]
RewriteRule ^(.*)\.html$ $1.php [nc]
You need only one RewriteEngine On.
You cannot use HTTP_HOST or REQUEST_URI in a RewriteRule. If you need to capture these values, you must do so in a RewriteCond
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)(.+)
RewriteRule .* https://%1/$0 [R,L]
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule .* https://%1/$0 [R,L]
This removes the leading www, if present. At the same time, it redirects to HTTPS.
RewriteEngine On
# Redirects from HTTP to HTTPS. We use %{SERVER_PORT} as it's more reliable than %{HTTPS}
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# Redirects www.example.com/... to example.com/...
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule .* https://%1%{REQUEST_URI} [R,L]
RewriteRule ^(.*)\.html$ $1.php [nc]