Getting error while enable www. from Drupal 8 .htaccess file - .htaccess

Error:
This page isn’t working.
www.example.com redirected you too many times.
.htaccess
# If your site can be accessed both with and without the 'www.' prefix, you
# can use one of the following settings to redirect users to your preferred
# URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
#
# To redirect all users to access the site WITH the 'www.' prefix,
# (http://example.com/foo will be redirected to http://www.example.com/foo)
# uncomment the following:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The site is using https:// and it's a single site.
Any suggestions?

this could perhaps help you
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

Related

redirect different url with the same subfolder

I am on a wordpress multisite.
I have old urls with the same subfolder name now i have to redirect old url to a new url with new subfolder name.
Example (will be simpler)
Old url 1 to new url 1:
http://site1.fr/fr-chauffage.html to
https://www.site1.fr/installation_chauffage
Old url 2 to new url 2:
http://site2.fr/fr-chauffage.html to https://www.site2.fr/installateur_chauffage_dans_ma_region
So I wrote the following htaccess file
RewriteCond %{HTTP_HOST} ^site1\.fr
RewriteRule ^/?(.*)$ https://www.site1.fr/$1 [R=301,L]
RewriteRule ^fr\.html$ / [R=301,L]
RewriteRule ^fr-chauffage.html$ /installation_chauffage/ [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^site2\.fr
RewriteRule ^/?(.*)$ https://www.site2.fr/$1 [R=301,L]
RewriteRule ^fr\.html$ / [R=301,L]
RewriteRule ^fr-chauffage.html$ /installateur_chauffage_dans_ma_region/ [R=301,NC,L]
The trouble is whatever the website the user is always redirect to site1 + good_subfolder
If I have 3 websites with the same subfolder name the user will be always redirected to site1 + good_subfolder of the new url ...
The trouble is whatever the website the user is always redirect to site1 + good_subfolder
Because the RewriteCond directive only applies to the first RewriteRule that follows. The two subsequent RewriteRule directives are executing unconditionally.
You need to repeat the condition for all rules where it needs to apply.
You're directives are also in the wrong order - you need to have the more specific rules first, otherwise you will get multiple redirects. Currently, you are redirecting everything in your first rule. And then you have a more specific redirect later.
So, are all incoming requests less the www prefix (as suggested in your examples and directives)?
Try the following instead:
# Common rules for both sites
# (Although this will result in 2 redirects if requesting the non-canonical hostname)
RewriteRule ^fr\.html$ / [R=301,L]
# Site 1 rdirects
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.fr [NC]
RewriteRule ^fr-chauffage.html$ https://www.site1.fr/installation_chauffage/ [R=301,NC,L]
# Site 2 rdirects
RewriteCond %{HTTP_HOST} ^(www\.)?site2\.fr [NC]
RewriteRule ^fr-chauffage.html$ https://www.site2.fr/installateur_chauffage_dans_ma_region/ [R=301,NC,L]
# non-www to www redirect - both sites
RewriteCond %{HTTP_HOST} ^(site1\.fr|site2\.fr) [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]

Redirect http to https and www to non-www in .htaccess

First of all, I know there are lots of answers on this, but I don't actually find one that works. This is what I have in the .htaccess file right now, and I want to mention that it worked previously, but it does not anymore.
Redirect 301 /unt-de-cacao-de-plaja/filtre/producator/crisnatur/ /ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g
Options +FollowSymlinks
# Prevent Directoy listing
Options -Indexes
# Prevent Direct Access to files
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
Require all denied
## For apache 2.2 and older, replace "Require all denied" with these two lines :
# Order deny,allow
# Deny from all
</FilesMatch>
# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L]
RewriteRule ^system/download/(.*) index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
# FORCE HTTPS AND NON WWW
RewriteEngine on
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
As a mention, I will have a lot of Redirect 301 from old pages to the new ones since the entire structure has been changed.
And the links that I am redirecting inside my website come with "www" like:
https://www.example.com/unt-de-cacao-de-plaja/filtre/producator/crisnatur/
and needs to be redirected to:
https://example.com/ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g
Redirect to https and non-www
To instead redirect all requests to https and non-www, use the following code instead of the previous:
Canonical HTTPS/non-WWW
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) https://example.com/$1 [L,R=301]
</IfModule>
As before, place this code in the root .htaccess of your site. Here is what it's doing:
Checks if mod_rewrite is available
Checks if HTTPS is off, or if the request includes www
If either condition matches, the request qualifies and is redirected
to the https/non-www address
OR
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
A few issues, in order of importance:
You have your canonical HTTP to HTTPS and www to non-www redirects at the end of the file. By placing it at the end of the file, after your front-controller, it's simply never going to be processed for most requests. This needs to be near the start of the .htaccess file, before your front-controller.
You should avoid mixing redirects from both mod_alias (Redirect) and mod_rewrite (RewriteRule) in the same scope. Different modules execute at different times throughout the request, despite their apparent order in the config file. Since mod_rewrite is required for other redirects, you should convert the mod_alias Redirect directives to use RewriteRule instead.
For example:
RewriteRule ^unt-de-cacao-de-plaja/filtre/producator/crisnatur/$ /ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g [R=301,L]
You should include the canonical scheme and hostname in your URL redirects in order to avoid multiple redirects when requesting an "old" URL at a non-canonical scheme ot hostname.
For example:
RewriteRule ^unt-de-cacao-de-plaja/filtre/producator/crisnatur/$ https://example.com/ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g [R=301,L]
Depending on what you mean exactly by "a lot of Redirect 301" - you should not be doing this at all in .htaccess and instead redirecting in your server-side script, once you have determined that the request will 404. This is to prioritise normal site visiters and not your redirects (that get executed on every single request).
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Since you stated that these directives worked previously then I assume the use of the HTTPS environment variable is OK on your system. But note that, whilst this is relatively common, it's non-standard. (It implies the server is using some kind of SSL front-end/proxy.)
Note that the order of these rules will result in a double redirect when requesting http://www.example.com/<anything> (HTTP + www). Which is necessary if you are implementing HSTS, but otherwise, you should reverse these two rules to avoid this unnecessary double redirect.

Redirect using .htaccess so that users appear as a subdomain

I have been using .htaccess for few redirects, I am having a hard time finding any solution for a new redirect I want.
I have users on my site and urls look like:
http://www.domain.com/users.php?user=username
Is it possible to rewrite it using htaccess so that it appears:
http://username.domain.com
Any help is much appreciated.
Something along these lines:
RewriteRule ^users.php?user=(.*)$ http://$1.domain.com/ [R,L]
As long as you've set it up so that all subdomains of domain.com point to the same document root as domain.com then you can do this with a single htaccess file in your document root:
RewriteEngine On
# externally redirect to user's subdomain when a specific request is made to users.php
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /users\.php\?user=([^&\ ]+)
RewriteRule ^ http://%2.domain.com/ [L,R=301]
# internally rewrite the user's subdomain to users.php
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^/?$ /users\.php?user=%1 [L,QSA]

allow domain only for certain extensions with hatccess

I have a domain abc.com
with subdomains
abc.com
www.abc.com
img.abc.com
All pointing to the same folder on my server.
Since I intend to use img.abc.com as a cdn, need to prevent img.abc.com/pqr.html from being a duplicate of www.abc.com/pqr.html.
ie
abc.com/* should redirect to www.abc.com/*
img.abc.com/*.jpg/gif/png should be allowed
[img.abc.com/*.other extension] should be redirected to [www.abc.com/*.other extension]
What could be the possible htaccess rules to be included?
Add these rules to the htaccess file in your document root:
RewriteEngine On
# abc.com/* should redirect to www.abc.com/*
RewriteCond %{HTTP_HOST} ^abc\.com$ [NC]
RewriteRule ^(.*)$ http://www.abc.com/$1 [L,R]
# img.abc.com/*.jpg/gif/png should be allowed
RewriteCond %{HTTP_HOST} ^img\.abc\.com$ [NC]
RewriteRule \.(jpe?g|png|gif)$ - [L,NC]
# [img.abc.com/*.other extension] should be redirected to [www.abc.com/*.other extension]
RewriteCond %{HTTP_HOST} ^img\.abc\.com$ [NC]
RewriteRule ^(.*)$ http://www.abc.com/$1 [L,R]

.htaccess removing www. AND redirect

I have been trying to redirect old pages for my old site to the corresponding new ones with permanent redirect.
as well as redirecting www.example.com to example.com
I dont seem to able to do both on the same time
at the moment redirects works for correct links from ex www.example.com/correctlink to example.com/correctlink
but only example.com/Info.aspx is redirected to example.com/about-magento-demo-store and NOT www.example.com/Info.aspx
*Update
I want to remove www. AND redirect 40-50 specific adress to new specific adresses. My problem is that the redirect only works if google has saved the old link without Www. IF google has stored a link including www. then my redirect Redirect permanent example.com/tabid/61/CategoryID/13/ProductID/64/Default.aspx /index.php/solpaneler/re does not function –
*Update
my htaccess looks a bit like this
(with a few lines in the end that where present before I started editing)
(i also tried to ad a line Redirect permanent http://www.example.com/Info.aspx http://example.com/about-magento-demo-store but it does not function)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://example.se/$1 [R=301,QSA,L]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301]
Redirect permanent /Info.aspx /about-magento-demo-store
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule .* index.php [L]
Just use rewrite rules for everything. Rewrite rules and Redirect don't always mix well.
RewriteRule ^Info\.aspx$ /about-magento-demo-store [L,R=301]
I've had the same problem a looooooooong time ago. Here's a sample of my rewriterules:
# If domain name doesn't end with .com redirect to .com:
RewriteCond %{HTTP_HOST} (.*)\.(fr|net|org|eu) [NC]
RewriteRule (.*) http://%1.com$1 [R=301,L]
# If domain name without "www", add them:
RewriteCond %{HTTP_HOST} ^mydomainname\.(fr|com|net|org|eu) [NC]
# Ca signifie forcément que c'est sans www => forcer redirection :
RewriteRule (.*) http://www.mydomainname.%1$1 [QSA,R=301,L]
NB: Put this on the top of your rewrite rules, because it should be processed before anything else so that you are sure your domain name always begin with "www"
Note that it redirects mydomainname only if it's "empty" i.e. nothing behind. It won't touch URLs like http://abc.mydomainname.com and it won't touch URLs like http://abc.def.mydomainname.com
Tell me if it works.

Resources