TLD specific 301 rewrite for relaunch on new server - apache

the company I work for is relaunching their newly built website on a new server under the same domain as before.
Example:
www.website.com/company/info/news.html on server A is going to change to www.website.com/news on server B.
On server A there is a second website (mostly a clone, so the same links are used) under a different TLD (website.ca). So there exists also a www.website.ca/company/info/news which should not be redirected.
We are relaunching only website.com for now so I need TLD-specific redirects. Everything combined there are about 250 pages that need redirecting to the new equivalent.
Those redirects are mainly aimed at crawlers and bots so we do not lose our pagerank and also for visitors with bookmarks, so I will add the rewrite-rules to the .htaccess-file on both servers.
I'm having trouble finding examples for this very specific scenario, so this is what I got so far from various how-tos and tutorials:
RewriteCond %{HTTP_HOST} ^http://www.website.com/company/info/news.html$ [NC]
RewriteRule ^(.*)$ http://www.website.com/news [L,R=301]
From what I understand it should redirect http://www.website.com/company/info/news.html to http://www.website.com/news with the information that it is a 301 and case-insensitive matching for the URL I want to redirect.
If this is correct I will add 250 RewriteCond (1 for each "old" page) and 1 RewriteRule per "new" page (about 10 in total) in this style:
RewriteCond 1
RewriteCond 2
RewriteRule 1
Thanks for any help in advance!

When checking for domain name, it is correct to look into %{HTTP_HOST}, but since it contains only the domain name (without the REQUEST_SCHEME or REQUEST_URI) you should compare it to www.website.com
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
For your exact example, the .htaccess RewriteRule would be:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^company/info/news.html$ /news [L,R=301]
But such exact rules would require you to write new Cond/Rule pait for every single .html file. If your structure follows some pattern, maybe something like this would work:
RewriteRule ^company/info/([a-zA-Z0-9_-]+).html$ /$1 [NC,L,R=301]
That should rewrite
http://www.website.com/company/info/anything.html to
http://www.website.com/anything

Related

301 Issues with 2 URLs on same webspace

I have 2 domains, the old domain is mjvandco.co.uk and he wants this redirecting to mjvlaw.co.uk. I have both pointing to the same webspace but when I test the URLs using https://httpstatus.io/ I get different results.
I have the following in my htaccess along with other stuff, but this is the redirect content:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mjvlaw\.co\.uk
RewriteRule (.*) https://www.mjvlaw.co.uk/$1 [R=301,L]
# Remove .html (excluding blog)
RewriteCond %{REQUEST_URI} !^/blog(.*)$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
The only URL that now is not right is this one: http://www.mjvlaw.co.uk/. I used this site
https://mjvlaw.co.uk - goes to https://www.mjvlaw.co.uk
http://mjvlaw.co.uk - goes to https://www.mjvlaw.co.uk
http://www.mjvlaw.co.uk - not work as does not go to https
https://www.mjvlaw.co.uk - fine
However, when I do the same for the old domain it all works as it should and every one below goes too https://www.mjvlaw.co.uk.
https://www.mjvandco.co.uk
https://mjvandco.co.uk
http://mjvandco.co.uk
http://www.mjvandco.co.uk
Am I doing something stupid here? Should I create another webspace and have one folder for the old domain and what for the current one and each having it's own htaccess file?
Thanks. I have done another ticket a month or so back but I am not sure how to change the questions, so I apologise for the similar ticket.
You rule only redirects non-www http URLs to SSL version of your site. To redirect both non-www and www http versions , replace your first rewrite block with the following
RewriteCond ℅{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.mjvlaw\.co\.uk
RewriteRule (.*) https://www.mjvlaw.co.uk/$1 [R=301,L]
Make sure to clear your browser cache before you test this.

htaccess redirect to a specific url on same domain (without looping)

I really hope you can help me out (it is driving me crazy).
I've tried dozens of setups and nothing seems to work, Googled myself dizzy and tried numerous different setups, but it all seems to result in a loop or a server error.
This is what needs to happen:
I have a site with multiple domains attached to it. What I need is that when someone visits the website via the "domain.co.uk"-domain, a redirect to the correct language parameters (among others) takes place.
To be very specific: when visiting via "www.domain.co.uk" the visitor must be redirected to "www.domain.co.uk?lang=en&noredir=1&currency=3"
I've made sure that the www is present with this:
RewriteCond %{HTTP_HOST} !^www.domain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [L,R=301]
The trouble is (I think) the redirect within the same domain without causing a loop.
I've tried stuff like this, but with no result:
RewriteCond %{HTTP_HOST} ^www.domain.co.uk$
RewriteRule ^$ http://www.domain.co.uk/?lang=en&noredir=1&currency=3 [L,R=301]
Hope you can help,
Cheers!
This will cause a loop:
RewriteCond %{HTTP_HOST} ^www.domain.co.uk$
RewriteRule ^$ http://www.domain.co.uk/?lang=en&noredir=1&currency=3 [L,R=301]
Because you're only checking the host header. Every time the redirect fires it will arrive back at the server with a host header of www.domain.co.uk and redirect again. You need to also check the query string and only redirect if it doesn't already match what you sent:
RewriteCond %{HTTP_HOST} ^www.domain.co.uk$
RewrteCond %{QUERY_STRING} !lang=en&noredir=1&currency=3
RewriteRule ^$ http://www.domain.co.uk/?lang=en&noredir=1&currency=3 [L,R=301]

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.

Apache 2.2 - "Split" domain

I'm very new to Apache and already run into an problem which already takes a lot of time and I'm not even sure if it's possible.
I have two servers and one Domain called szop.in which is having an A record to my first server. On the first server I'm running an URL shortener called YOURLS, it's under szop.in/admin. I want the second server serve my homepage, therefor I want to redirect all requests like szop.in or http://subdomain.szop.in to the second server but not http://szop.in/admin.
Is this possible?
This doesn't seem to be the right solution and the mod_rewrite is causing me some headache:
RewriteEngine On
RewriteCond %{HTTP_HOST} szop.in [NC]
RewriteRule !^/admin$ hxxp://other-domain.in [R=301,L]
My idea was, since I need just one URL to work on the first server http://szop.in/admin, to redirect everything that is not starting with /admin to the other domain.
You almost got it:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^szop\.in$ [NC]
RewriteCond %{REQUEST_URI} !^/admin [NC]
RewriteRule ^ http://subdomain.szop.in%{REQUEST_URI} [R=301,L]
You cannot use the negation on the RewriteRule like that, you use it on a conditions.
This should do what you want, it verify if domain is szop.in and if folder is not /admin and redirect to subdomain.szop.in.

htaccess / mod_rewrite for Search Engine Friendly URLs

I have a bunch of files in the web root: -
/abc-674.php
/def-643.php
etc.etc.
I want to show these when the following URL's are requested, without changing the URL in the browser: -
/products/abc-674/this_can_be_anything.php
/products/abc-674/or_this.php
both redirect to /abc-674.php, and
/products/def-643/this_can_be_anything.php
/products/def-643/or_this.php
both redirect to /def-643.php.
So, basically, the bit between products/ and the next / is the target, while anything after that can effectively be ignored.
If it matters, I've already got a little code in my .htaccess to direct all traffic to my preffered domain (with www): -
# Direct all traffic to domain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} !^domain$ [NC]
RewriteRule ^(.*) http://www.domain.com/$1 [R=301,L]
EDIT: To clarify - the 2nd last line in the above is because I access the site on my dev server via http://domain/ only, and so don't want that rewrite to apply for those requests. I do, however, want this new rewrite to apply to all requests.
Any help greatly appreciated!
I think, you can use the next rule in .htaccess:
RewriteRule ^products/([\d\w]+)/(.*)$ /$1.php