I have some unused subdomains and I would like to redirect them to my homepage.
I have found this .htaccess code
# redirect all subdomains
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com/ [L,R]
It redirects when you call the exact subdomain, for example: test.example.com
But it does not work if you call anything else, for example: test.example.com/meh
It's possibile to redirect any URL in these subdmoains to the homepage?
You may use this rule:
# redirect all subdomains
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301,NE]
%{REQUEST_URI}at the end of target will add previous URI to new target.
Related
I saw similar topics but couldn't find a practical answer to my problem.
I'm moving my old website to a new one, and some URLs are changing.
I would like to make a generic 301 redirection to the new domain (because most paths are the same), while individually redirecting some URLs.
Here is what I have on my old website .htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com$
RewriteRule (.*)$ https://new.com/$1 [R=301,L]
Redirect 301 "/custom/url/" "https://new.com/my-custom-url"
</IfModule>
But the 301 redirects to : https://new.com/custom/url instead of https://new.com/my-custom-url
Some of my URLs also have URL parameters I would like to redirect, such as :
Redirect 301 "/brand.php?name=Example" "https://new.com/Example"
Redirect 301 "/brand.php?name=Example2" "https://new.com/another/url"
which do not seem to work as well.
Thank you very much for your help.
But the 301 redirects to : https://new.com/custom/url instead of https://new.com/my-custom-url
It is because your specific redirect rule appears after generic one. Moreover you are mixing mod_rewrite rules with mod_alias rules and these are invoked at different times.
Have it like this:
RewriteEngine On
# redirect /brand.php?name=Example2 to new.com/another/Example2
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=(Example2) [NC]
RewriteRule ^brand\.php$ https://new.com/another/%1? [R=301,L,NE]
# redirect /brand.php?name=Example3 to new.com/category/Example3
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=(Example3) [NC]
RewriteRule ^brand\.php$ https://new.com/category/%1? [R=301,L,NE]
# generic redirect /brand.php?name=Example to new.com/Example2
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=([^&]+) [NC]
RewriteRule ^brand\.php$ https://new.com/%1? [R=301,L,NE]
# redirect custom URL
RewriteRule ^custom/url/ https://new.com/my-custom-url [R=301,L,NE,NC]
# redirect everything else
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteRule ^ https://new.com%{REQUEST_URI} [R=301,L]
I need to make a request to https://subdomain.www.domain.com rewrite the URL to https://subdomain.domain.com
Which part of the below rewrite do I change to achieve this? I'm guessing I have to put the subdomain in front of the www, but what do I put there, also how do I capture all subdomains - what's the general rule for all subdomains?
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
You may use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.(domain\.com)$ [NC]
RewriteRule ^ https://%1.%2%{REQUEST_URI} [R=301,L,NE]
I would like to modify my htaccess so:
http:// domain.com/REQUEST is redirected to https:// domain.com/REQUEST
but
http:// blog.domain.com/REQUEST still goes to http:// blog.domain.com/REQUEST
and
https:// blog.domain.com/REQUEST is redirected to http:// blog.domain.com/REQUEST
how the code should look like?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,NE,L]
RewriteCond %{HTTP_HOST} !^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=302,NE,L]
This is assuming main domain and sub domains are all pointed to same DocumentRoot.
I have been able to find solutions to each of these redirects separately but I can't find something that does both correctly.
I need the website to redirect from:
http://somesite.com.au to https://somesite.com.au
http://www.somesite.com.au to https://somesite.com.au
https://www.somesite.com.au to https://somesite.com.au
The main combination that I'm having trouble with is:
https://www.somesite.com.au to https://somesite.com.au
Currently I am using the following in my htaccess file:
# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Ensure we are using HTTPS version of the site.
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect www to non-ww
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
What should I be using in my htaccess to make this work?
You're causing a loop because your www rule redirects to http://. Just change it to redirect to https:// instead:
# Redirect www to non-ww
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Using these directives, I can redirect any non-www subdomain to the www subdomain:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
What I need to achieve is to redirect the www subdomain only.
For example I want to redirect only blog.myname.com to www.blog.myname.com. The rule in the config above will redirect blog.myname.com to www but it also redirect myname.com to www.myname.com, which is not what I am looking for. I also need to redirect all queried subdomain not just blog.myname.com
How can I do this?
You can use this code to achieve that:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www)[^\.]*)\.myname\.com$ [NC]
RewriteRule ^ http://www.%1.myname.com%{REQUEST_URI} [L,R=301]
Above code withh redirect all subdomains but will not effect myname.com or www.myname.com.
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.myname\.com$ [NC]
RewriteRule ^ http://www.blog.myname.com{REQUEST_URI} [L,R=301]