.htaccess redirect: www not working - .htaccess

I want to redirect a domain alias to a specific page on my site. I use this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^afdekfolie.be$ [NC]
RewriteRule ^(.*)$ http://www.technosales.be/wordpress/afdekfolie/ [R=301,L]
This works when entering afdekfolie.be as the alias, but not when entering www.afdekfolie.be
How do I make this work?
Thanks,
Stefaan

Because you are asking this rule to work for the host starting with afdekfolie...
Try this one
RewriteCond %{HTTP_HOST} ^(www\.)?afdekfolie\.be$ [NC]
Or you can break it into two rules
RewriteCond %{HTTP_HOST} ^afdekfolie\.be$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.afdekfolie\.be$ [NC]
ps: how is this related to the php tag?

Related

htaccess redirect subfolder to root and force https

I'm trying to redirect all URLs with a subfolder to the root, but also force https and www
So, for example, I want to redirect:
example.com/shop/category/product
www.example.com/shop/category/product
example.com/SHOP/category/product
to
https://www.example.com/category/product
I've tried the following code
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1
but this doesn't redirect the subfolder, and if I add the subfolder as a previous rule like
RewriteRule ^shop/(.*)$ /$1 [R=301,NC,L]
It just doesn't work.
One thing to point out - I must also make sure that any other requests to the site (with or without the subfolder) are being redirected to the https www version.
Can anybody point me in the right direction or help me solve this? TIA
You can use this single rule for this redirect at top of your .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{REQUEST_URI} ^/shop/ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(?:shop/)?.*$ https://www.%1/$0 [R=301,L,NC,NE]
# rest of your rules go below
Make sure to clear browser cache or use a new browser for testing.

301 redirecting while preserving path structure

I am trying to redirect every URL from my old site to my new site, so that, if someone goes to say oldsite.com/about it redirects to store.newsite.com/about rather than just store.newsite.com/index.php
I tried the following code but for some reason it always takes me to store.newsite.com/index.php when I go to oldsite.com/about instead of taking me to store.newsite.com/about, and the same goes for every other path on the site. Is there something wrong with what I am putting in my .htaccess file?
Thank you!
RewriteEngine on
RewriteCond %{HTTP_HOST} ^oldsite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.com [NC]
RewriteRule ^(.*)$ http://store.newsite.com/$1 [L,R=301,NC]
Just fixed it with this code
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.site$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.old\.site$ [NC]
RewriteRule ^(.*)$ http://store.newsite.com/$1 [R=301,L]

.htaccess redirects - map all URLs but give homepage specific redirect

I have the following set up for an old domain, which works really well to mapping the old pages to the ones on the new domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^oldurl.org.uk [NC,OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.org.uk [NC]
RewriteRule ^(.*)$ http://www.newurl.co.uk/$1 [L,R=301,NC]
The problem is I need to redirect the homepage to a specific page on the new URL, keeping the above in tact. Whats the best way to do this?
Thanks in advance!!
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?oldurl\.org\.uk$ [NC]
RewriteRule ^$ http://www.newurl.co.uk/specificPage [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?oldurl\.org\.uk$ [NC]
RewriteRule ^(.+)$ http://www.newurl.co.uk/$1 [L,R=301]

Rewrite www.domainnamenumberone.com/wordpress/path/ to domaintwo.com/wordpress/path

I have two domain names for the same website, and need to do a correct rewrite, so that whenever someone accesses the first domain name and all subdirectories, with, or without www. they get redirected to the second domain and subdirectories without www.
I managed to set the redirect for the domain name without subdirectories, but for whatever reason, subdirectories are not getting rewritten.
So when I go to domainnamenumberone.com, or www.domainnamenumberone.com, i get redirected to domaintwo.com – however, when I go to domainnamenumberone.com/wordpress/path or www.domainnamenumberone.com/wordpress/path I remain there, and nothing gets rewritten.
Here's what I placed in .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.domaintwo\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
Would be grateful for your help!
You need to place this rule as very first rule in DocumentRoot/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domaintwo\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainnumberone\.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(\S*)\s [NC]
RewriteRule ^ http://domaintwo.com/%1 [L,R=302,NE]
Then add this line in each child .htaccess like wordpress/ or wordpress/path/ (wherever .htaccess already exists) below RewriteEngine On line
RewriteOptions Inherit
You can use that:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP_HOST} ^domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
But it seems to me that it should also work with yours.....
try a different browser (cache problem)

Htaccess redirecting non-www to www

I have searched the web all over for a good generic .htaccess script for redirecting non-www to www, and currently i'm using this:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
This works fine, but if i go to a subdomain www. will be added. Does anyone has a good working redirect .htaccess script?
Try this :
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^([^\.]+)\.([^\.]+)\.([a-z]{2,4})$
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
#Vince What if the requested url is something like:
http://www.abc.example.com
IMHO, I think with your method it would never be redirected to:
http://www.example.com
How about this?
# Rewrite domain
RewriteCond %{HTTP_HOST} !^www\.([a-z1-9\-]+)\.([a-z]+)$ [NC] [and]
RewriteCond %{HTTP_HOST} ([a-z1-9\-]+)\.([a-z]+)$ [NC]
RewriteRule ^(.*)$ http://www.%1.%2/$1 [R=301,L]
Also, you guys may find these references useful:
https://www.drupal.org/node/93603
http://www.askapache.com/htaccess/modrewrite-tips-tricks.html

Resources