.htaccess redirect chain generated by multiple redirects - apache

Wondering if you can help me?
I'm working on a site at the moment, and the following is in the .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.co.uk [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
RewriteRule ^$ http://www.example/folder-name [R=301,L]
As I see it, this works in the following manner:
root domain rewrites using 301 to www. and then an additional 301 redirect to the /folder-name.
Ideally, I'd like to remove the middle redirect (i.e from root domain to www. and keep just the redirect from root domain to /folder-name.
NB. there is a subdomain, let's call that products.example.co.uk - and access would be needed here.
Is this possible? my .htaccess skills are not particularly tight so any help gratefully received!

This should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/folder-name/$1 [L,R=301]

Ideally, I'd like to remove the middle redirect (i.e from root domain to www. and keep just the redirect from root domain to /folder-name.
This should do it :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example.co.uk [NC]
RewriteRule ^((?!folder).*)$ http://www.example.co.uk/folder/$1 [L,R=301]

Related

htaccess Rewrite URL based on TLD

On my website, I am interested in moving my forums from a subdirectory to a subdomain. The forums are currently located at example.com/forums, and I want to move them to forums.example.com. After the move, I want to use htaccess to 301 redirect all forum traffic to the subdomain, but the problem is that I have two TLDs for my site, a .com domain and a .net domain.
I am currently trying to redirect traffic using this:
RewriteCond %{HTTP_HOST} !=forums.example.net
RewriteRule ^forums(/(.*))?$ https://forums.example.net/$2 [L,R=301]
RewriteCond %{HTTP_HOST} !=forums.example.com
RewriteRule ^forums(/(.*))?$ https://forums.example.com/$2 [L,R=301]
This only half-works. Regardless of which TLD I visit, it always redirects me to forums.example.net, even if I am visiting from example.com/forums, in which case I want it to go to forums.example.com. How could I achieve this?
You can use this singe rule as very first rule in forums/.htaccess instead of top level .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.(?:com|net))$ [NC]
RewriteRule ^.*$ http://forums.%1/$0 [L,R=301,NE]
For root .htaccess use this rule as your first rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.(?:com|net))$ [NC]
RewriteRule ^forums(/.*)?$ http://forums.%1$1 [NC,L,R=301,NE]
Please use below rules for the proper redirection to work as you want it.
Rewritengine On
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^forums$ http://forums.example.com/? [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.net$
RewriteRule ^forums$ http://forums.example.net/? [L,R=301]

RewriteCond for a folder only on a specific domain extension

I have a site that can be accessed via 3 domains (domain.com, domain.ch, domain.fr). The three use exactly the same files and folder, though.
Regarding the .fr domain (and only this domain), I need the following:
redirect domain.fr (root) to domain.com/fr/france.
This has been achieved with the following rule:
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com\/fr\/france" [R=301,L]
It works. (There's also a functioning rule to force www. in front of every URL.)
What I can't get to work is:
redirect domain.fr/fr also to domain.com/fr/france.
and finally, redirect any URL domain.fr/fr/* to domain.com/fr/*
(keeping whatever * stands for).
The trick (to me) is that the same .htaccess file will also be present on domain.com and domain.ch, but those rules must not activate for those domains.
You can put these rules in your htaccess
# Redirect [www.]domain.fr and [www.]domain.fr/fr to www.domain.com/fr/france
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^(|fr)$ http://www.domain.com/fr/france [R=301,L]
# Redirect domain.fr/fr/* to domain.com/fr/*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/$1 [R=301,L]
Try :
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteCond %{REQUEST_URI} !^/fr/france
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/france/$1 [R=301,L]

Redirect a.com/a.html to b.com/a.html

I need to redirect a.com/a.html to b.com/a.html htaccess Apache. I have several subdomains that are not being redirected to the b.com(which is fine). and the example below is only the first domain that needs to be redirected. Three other domains are needed to be redirected to b.com as well with all of their subpages as a.com/a.html to b.com/a.html. Any hints?
RewriteCond %{HTTP_HOST} ^(www\.)?nexus21\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^tvlift\.com$ [NC]
RewriteRule ^ http://www.tvlift.com%{REQUEST_URI} [L,NE,R=301]
I would do each subdomain redirect separately. That way it is cleaner and more maintainable. If you are redirecting few known files then name them directly.
RewriteEngine on
RewriteRule "^/foo\.html$" "bar.html" [R]
If there are lots of file or you will be adding in future then use a pattern
RewriteEngine on
RewriteRule "^/docs/(.+)" "http://new.example.com/docs/$1" [R,L]
See this page
http://httpd.apache.org

301 when www. infront of subdomain

Could anyone please explain the redirect conditions and rule to achieve this:
User visits www.example.domain.com
301 to example.domain.com
(Basically removing the www. from the URL, but ONLY when they access a subdomain.
It must allow access at www.domain.com
Thanks!
Adding this to the htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.([^\.]*)\.domain\.com$ [NC]
RewriteRule (.*) http://%1.domain.com/$1 [R=301,L]
will redirect www.example.domain.com to http://example.domain.com it will even redirect www.other.domain.com to http://oter.domain.com
Try this,
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.([a-zA-Z0-9]+)\.domain\.com [NC]
RewriteRule ^(.*)$ http://%1.domain.com/$1 [L,R=301]

.htaccess issue with using a -org secondary domain vs using a .com secondary domain

I have successfully forwarded www.atlantalawvideo.legalvideoconsulting.com to www.atlantalawvideo.com for SEO purposes.
I'm using this code in my .htaccess file in my /atlantalawvideo/ folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.atlantalawvideo.com
RewriteRule (.*) http://www.atlantalawvideo.com/$1 [R=301,L]
I'm having trouble forwarding www.legalvideographer-org.legalvideoconsulting.com to www.legalvideographer.org
I'm using this code in my .htaccess file in my /legalvideographer-org/ folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.legalvideographer.org
RewriteRule (.*) http://www.legalvideographer.org/$1 [R=301,L]
Not sure what the problem is http://www.legalvideographer-org.legalvideoconsulting.com forward goes nowhere.
The second rewrite is not working because you are not meeting the condition of the %{HTTP_HOST}. You're not using the full host name to check. legalvideographer.org appears to be a subdomain oflegalvideoconsulting.com. See if this works for you.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.legalvideographer-org\.legalvideoconsulting\.com [NC]
RewriteRule (.*) http://www.legalvideographer.org/$1 [R=301,L]
That should work if that is your domain name.
You should also be able to do a simple redirect instead of the rewrite rule
redirct 301 / http://www.legalvideographer.org