Htaccess www to non-www but keep the url intact - .htaccess

I think that this is rather simple, but I just can't wrap my head around it.
I have a domain like https://www.example.com
I want to rewrite it to https://example.com
That works. But I am also rewriting other urls like https://www.example.com/privacy
If I try to rewrite these too the user is always redirected to https://example.com/privacy.php
How do I prevent my server to show the actual filename instead of its rewritten url?
My code looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^checkout checkout.php [L]
RewriteRule ^imprint imprint.php [L]
RewriteRule ^privacy privacy.php [L]
RewriteRule ^terms terms.php [L]
RewriteRule ^allergy allergy.php [L]
RewriteRule ^nutrition nutrition.php [L]
RewriteRule ^order/([A-Z]*)?$ confirmation.php?lang=&id=$1 [L,QSA]
Any help is appreciated.
Regards

Have it this way:
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(checkout|imprint|privacy|terms|allergy|nutrition)/?$ $1.php [L]
RewriteRule ^order/([a-z]+)/?$ confirmation.php?lang=&id=$1 [L,QSA,NC]
Make sure to test after clearing your browser cache. Problem appears to be due to your patterns not using end anchor.
I have combined your multiple similar rules into one rule.

Related

301 htaccess redirects with 3 parameters to static urls

Im trying to redirect several urls with 3 parameters to different static urls with .htaccess but nothing working.
1.
http://olddomain.com/index.php?id_category=28&controller=category&id_lang=2
to
https://newdomain.com/page1/
http://olddomain.com/index.php?id_category=30&controller=category&id_lang=2
to
https://newdomain.com/page2/
http://olddomain.com/index.php
to
https://newdomain.com
I tried the below code but http://olddomain.com/index.php not going to https://newdomain.com :
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301,NC]
RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page2/? [R=301,L]
You need to have specific longer matches first and then have rules to remove index.php or domain redirect:
RewriteEngine On
# specific redirects with index.php as optional match
RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page1/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page2/? [R=301,L,NC]
# remove index.php and redirect to newdmain
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteRule ^(?:index\.php/?)?(.*)$ https://newdomain.com/$1 [L,R=301,NC,NE]
Make sure to clear your browser cache before testing this change.
In case you are taking page's id from id_lang= variable then please try following rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules to redirect to link: https://newdomain.com/page1/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\?id_category=28&controller=category&id_lang=(\d+)\s [NC]
RewriteRule ^ https://newdomain.com/page%1/? [NE,R=301,L]
##Rules to redirect https://newdomain.com/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\s [NC]
RewriteRule ^ https://newdomain.com [NE,R=301,L]

RewriteRule for subdomain to subfolder .htaccess

I'm trying to accomplish the following:
non-http force to https - works
www force to non-www - works
website loaded from subfolder (/web) - works
test.example.com load different subfolder (/test) - does not work
4, Does not work, the condition is met to go to /web. Can't understand how to change this into /test
the .htaccess code I use:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^(.*)$ /test/$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteRule ^(.*)$ /example/web/$1 [L,NC,QSA]
With your shown samples please try following htaccess rules file here. Please make sure to clear your browser cache before testing your URLs. In case you have further more rules(apart from shown ones) then make sure these Rules are before those rules.
RewriteEngine On
##Apply https to uris here..
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
##Apply non-www to uris here..
RewriteCond %{HTTP_HOST} ^(?:www\.)(example\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
##Apply test to all uris here..
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test [NC]
RewriteRule ^(.*)/?$ example/web/$1 [L,NC,QSA]
##Apply test to all uris here..
RewriteCond %{HTTP_HOST} ^test\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test [NC]
RewriteRule ^(.*)/?$ www.example.com/test/$1 [L,NC,QSA]

.htaccess rewrites results in www.www

What I want to achieve should be quite simple:
Redirect all traffic to HTTPS and the www. subdomain. And to achieve this I used the following rule:
# Canonical https/www
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ([^.]+)\.domain.com [NC]
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^(.*) https://www.%1/$1 [R=301,L]
</IfModule>
However, if I follow a link like this:
http://www.example.com
I end up here:
https://www.www.example.com
So then I found this question: .htaccess: http://www redirects to www.www
And I completely replaced the rule I used above with the rule suggested in the accepted answer:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^(.*)$ https://www.%1%{REQUEST_URI} [R=301,L]
However now I get the ERR_TOO_MANY_REDIRECTS error, and the site completely refuses to load.
Can someone help me out here?
Check this rewrite in your .htaccess file, maybe it help
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

htaccess language rediretion

I have a website, www.thesite.com and an alias www.lesite.com
i need a htaccess redirection:
I would like that, when I go to www.thesite.com/fr, it redirects me to www.lesite.com/fr
Likewise, when I go to www.lesite.com/en, I would like it to redirect me to www.thesite.com/en
I have tried different methods but i only succeed to create infinite loops !----
Options +FollowSymlinks
RewriteRule ^fr$ http://dev.mariage.ch/fr/ [L]
RewriteRule ^de$ http://dev.hortzeit.ch/de/ [L]
OR
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch\/de\/
RewriteRule (.*) http://dev.hortzeit.ch/de$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch\/fr\/
RewriteRule (.*) http://dev.mariage.ch/fr$1 [R=301,L]
You can't use path as part of RewriteCond for HTTP_HOST
instead of dev.hortzeit.ch/de you must use just host part dev.hortzeit.ch
RewriteEngine On
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch [NC]
RewriteRule ^de(/?.*)$ http://dev.hortzeit.ch/de$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch [NC]
RewriteRule ^fr(/?.*)$ http://dev.mariage.ch/fr$1 [R=301,NC,L]

Forcing WWW And HTTPS (SSL) In .htaccess -- Requiring Assistance

I have found a thousand other topics asking for help with this, but none of their solutions seem to work for some reason.
I just purchased SSL for my domain a couple of days ago because I am accepting credit/check cards on my site and I want my customers to feel and be secure.
Anyways, this is what my .htaccess file looks like at the moment:
php_flag display_startup_errors off
php_flag display_errors off
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^buy-wow-accounts index.php [NC]
RewriteRule ^sell-wow-accounts sell.php [NC]
RewriteRule ^about-khaccounts about.php [NC]
RewriteRule ^buy-sell-wow-accounts-faq faq.php [NC]
RewriteRule ^khaccounts-feedback feedback.php [NC]
RewriteRule ^payment-plan payment-plan.php [NC]
RewriteRule ^customer-login customer-login.php [NC]
RewriteRule ^customer-center customer-center.php [NC]
RewriteRule ^privacy-policy privacy.php [NC]
RewriteRule ^buy-world-of-warcraft-wow-accounts/page-([0-9]+) listing.php?pageid=$1 [L,NC]
RewriteRule ^buy-world-of-warcraft-wow-accounts listing.php [L,NC]
RewriteRule ^world-of-warcraft-wow-acc/([^/]*)\.html$ account.php?acc=$1 [NC]
What I want to do is force WWW if it is not already in the URL and ensure that HTTPS (SSL) is forced as well. I have a ton of links out there to my site and most of them are just links like 'www.khaccounts.net', 'http://khaccounts.net', and 'khaccounts.net'.
I want to ensure that each of these different old links will be forced into having WWW and HTTPS. In order words, I want people to have the URL - 'https://www.khaccounts.net' no matter what URL they took to get to my site.
Thanks!
Right after your www rules add:
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Also, change your www rule to https:// so that there aren't 2 redirects.
Looks like you're rewriting all of your urls to the root. Why don't you try:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L]
This works in showing the proper URL when khaccounts.net is hit, but still throws a redirect loop.
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.khaccounts.net/$1 [R=301,L,QSA]
Try replacing these lines :
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
With this :
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.khaccounts\.net$ [NC]
RewriteRule ^(.*)$ https://www.khaccounts.net/$1 [R=301,L,QSA]

Resources