mod_rewrite remove www for all domains - apache

I have multiple domains all pointing to the same website and depending on which domain you enter the website, will determine which categories will be selected.
For this to work I have to place any Mod_Rewrite code inside my
apache2/sites-available/primarydomain
file.
The website has the potential to host hundreds of domains so I need a system where I dont have to write a rule out for each domain eventuality.
I have seen this suggested online
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
but it doesnt work for me.
Does any regex guru out there know if this is even achievable?
Heres hoping :-)

Related

how do i redirect a domain name to an alternate on the same site? i.e example1.com to example2.com; both the same site via htaccess?

All the examples I've found have something along the lines of
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ https://www.newdomain.com/$1 [R=301,L]
But it doesn't seem to catch in my .htaccess despite online syntax checkers telling me everything is fine and it'll fire.
I have a bunch of non conflicting individual page RewriteRules that don't conflict with this and fire off fine so I know mod rewrite is working and i'm stumped.
olddomain is a domain that used to reside on a different server but its dns is being pointed at newdomain so all these rules are firing off on the same site if that matters

Apache Mod_Rewrite needs to keep the Path but change the Domain

Background
I have a website that has several other alias domain names, in an effort to streamline and simplify I want all the domains to revert to the one domain name I now advertise -
Main name: www.explorerWorld.co.uk
Other names that load the same content but retain their own address:
www.exploreElsewhere.co.uk
www.exploitedexplodedexplorers.co.uk
what I have is that across the internet there are links in peoples blogs and elsewhere that go to specific pages on my site, but these will be under these different domains.
I have changed the domain Alises under cPanel and this works, for the base address only, but a domain for example www.exploreElsewhere.co.uk/trees.php does not redirect.
Question
I would like these pages to still work but to redirect to the main site -- www.explorerWorld.co.uk -- * but preserving their file path*
so: www.exploreElsewhere.co.uk/plants/trees.php gets seemlessly changed to www.explorerWorld.co.uk/plants/trees.php
My htaccess so far:
RewriteCond %{HTTP_HOST} !^exploreWorlds\.co\.uk$1 [NC]
RewriteRule ^(.*)$ http://www.exploreWorlds.co.uk/$1 [R=301,L]
But this seems to work in an endlessly repeating loop, How can I improve this htaccess ?
I have also looked here but this was not very helpful:
.htaccess change domain but keep path
I have found a solution to this problem was to use %{REQUEST_URI} which is the path given to the server.
(with a pointer from Arco444)
RewriteCond %{HTTP_HOST} !^(www\.)?exploreworlds\.co\.uk [NC]
RewriteRule ^(.*) http://www.exploreworlds.co.uk/%{REQUEST_URI} [R=301,L]
This successfully rewrites the domain part of the URL to force alias URL links to be redirected to my base website address + path
You have a mistake in your RewriteCond. Try
RewriteCond %{HTTP_HOST} !^www.exploreWorlds\.co\.uk$1 [NC]
RewriteRule ^(.*)$ http://www.exploreWorlds.co.uk/$1 [R=301,L]
instead, or remove the tilda from your original snippet

301 Redirect Whole Site Except One Folder, leave Add-on domains alone

I have a brainteaser and need help from people smarter than me. I have a shared hosting account. I'd like to 301 forward the root URL (say, domain.org) to a new URL. I also want one folder (/blog/) to be left alone (not forwarded). I was able to find an example of this here, and I put together this potential scenario for doing that:
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ http://newdomain.org/$1 [L,R=301]
I believe that this should be OK, but here's the trick: I have add-on domains in this hosting, and if I use the above, I'm pretty sure that I will forward every one of them to newdomain.org, not just domain.org. I did some testing using more specific text strings in the first spot following RewriteRule, but I can't seem to get the syntax without blowing up my site and getting a 500.
Any ideas would be greatly appreciated!
Thanks, Dave
Try adding another condition:
RewriteCond %{HTTP_HOST} ^domain\.org$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ http://newdomain.org/$1 [L,R=301]
Where domain.org is the domain that you want everything to be redirected to newdomain.org, except /blog/.

Is my rewrite code correct?

I am trying to write rewrite code for my customer's site. I have no way of verifying if it's correct because I don't have access to the server yet. I know that sounds strange but it's what I have to accept and work around.
I plan to put this in the root htaccess file on the server. Bottom line is this URL does not work:
http://www.regions.noaa.gov/gulf-mexico/index.php/highlights/restore-act-passed/
So when the above fires, I want it to permanently redirect to:
http://www.regions.noaa.gov/gulf-mexico/highlights/restore-act-passed/
Here is what I have
RewriteEngine on
RewriteCond %{HTTP_HOST} ^regions\.noaa\.gov$ [OR]
RewriteCond %{HTTP_HOST} ^www\.regions\.noaa\.gov$
RewriteRule ^gulf\-mexico\/index\.php\/highlights\/restore\-act\-passed\/$ "http\:\/\/www\.regions\.noaa\.gov\/gulf\-mexico\/highlights\/restore\-act\-passed\/" [R=301,L]
I'd appreciate any feedback on this. Thanks.
UPDATE - thanks to all who replied. Here's what I don't understand. I found this code on my web hosting company's code generator. It seems to work:
RewriteCond %{HTTP_HOST} ^designerandpublisher.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.designerandpublisher.com$
RewriteRule ^services.html$ "http\://www.regions.noaa.gov/gulf-mexico/highlights/restore-act-passed/" [R=301,L]
I usually do like this and works fine.
IF user enter in the URL with highlights/restore-act-passed/ THEN will display contents from index.php/highlights/restore-act-passed/ in the browser.
# [NC] Means “No Case”, so it doesn’t matter whether the domain name was written in upper case, lower case or a mixture of the two.
RewriteEngine on
RewriteRule ^highlights/restore-act-passed/?$ index.php/highlights/restore-act-passed/ [NC]
IF the user enter in the URL with index.php/highlights/restore-act-passed/ THEN will display contents from _http://%{HTTP_HOST}/gulf-mexico/highlights/restore-act-passed/
RewriteRule ^index.php/highlights/restore-act-passed/?$ _http://%{HTTP_HOST}/gulf-mexico/highlights/restore-act-passed/ [NC]
You don't need to specify the HTTP_HOST, unless you will have multiple domains coming through here (add-ons, subdomains, parked domains, etc.). If you do want to specify it, it can be simplified to one line:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?regions\.noaa\.gov$
RewriteRule ^gulf\-mexico\/index\.php\/highlights\/restore\-act\-passed\/$ "http\:\/\/www\.regions\.noaa\.gov\/gulf\-mexico\/highlights\/restore\-act\-passed\/" [R=301,L]
Actually, a subdomain doesn't even need the www, but it doesn't hurt. Then, in the rewrite rule, you only need to escape specific metacharacters in the pattern, and none in the replacement string:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?regions\.noaa\.gov$
RewriteRule ^gulf-mexico/index\.php/highlights/restore-act-passed(/)?$ http://www.regions.noaa.gov/gulf-mexico/highlights/restore-act-passed/ [R=301,L]
I also made the last (trailing) / optional. Since you're going to the same domain, there is no need to repeat it:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?regions\.noaa\.gov$
RewriteRule ^gulf-mexico/index\.php/highlights/restore-act-passed(/)?$ /gulf-mexico/highlights/restore-act-passed/ [R=301,L]
The 301 code says to alert search engines that this URL or URI has permanently moved (it will show up changed in a browser address bar, too, so human visitors can choose to rebookmark it).
As this appears to be an SEO URI, presumably it will be translated into a dynamic format (/gulf-mexico/index.php?area=highlights&item=restore-act-passed). That means that the above rewrite has to be done before any SEO-to-dynamic translation. An alternative would be to directly translate it to dynamic format right here, but since you're giving a 301, presumably you want the SEO format to show in a browser or search engine result.

.htaccess 301 redirects based on TLD

We're in the process of switching our current site from a multiple domain configuration into a single domain, multiple folder format. i.e.
.co.uk/<uri> is becoming .com/en-gb/<uri>
.com/<uri> is becoming .com/en-us/<uri>
I'm hoping that I'll be able to handle this via a couple of well-crafted .htaccess rules, but I'm not sure of code I'm going to need to achieve this. Can you help?
(PS, I've left the actual domain blank, as we only need to test for the TDL, not the entire domain - although whatever the original domain was need to stay the same, with only the TLD changing - i.e. whatever.co.uk would redirect to whatever.com/en-gb/, whatever2.co.uk would redirect to whatever2.com/en-gb/, and whatever.com would redirect to whatever.com/en-us/)
add the following directives to your .htaccess:
RewriteCond %{HTTP_HOST} (.+)\.co\.uk$
RewriteRule (.*) http://%1.com/en-gb/$1 [R=301,L,QSA]
RewriteCond %{HTTP_HOST} \.com$
RewriteCond %{REQUEST_URI} !^/(en-us|en-gb)/
RewriteRule (.*) /en-us/$1 [R=301,L,QSA]
if you have so many domain TLDs, you maybe want to use RewriteMap to avoid duplicating the first rule for every TLD, RewriteMap will map TLD to Uri string (ex: .co.uk to en-gb),