htaccess redirect woes - .htaccess

I site that I've inherited for some reason was set up with www- as well as www.
To clarify, both of these URLs work:
http://www-mcr.trinhall.cam.ac.uk
http://www.mcr.trinhall.cam.ac.uk
I want to get all traffic using the www- to redirect to the www.
I thought that I'd nailed it with the following htaccess rules, but they don't seem to be working. Any help would be much appreciated!
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mcr.trinhall.cam.ac.uk$ [NC]
RewriteRule ^(.*)$ http://www.mcr.trinhall.cam.ac.uk/$1 [L,R=301]
The full .htaccess file is below:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mcr.trinhall.cam.ac.uk$ [NC]
RewriteRule ^(.*)$ http://www.mcr.trinhall.cam.ac.uk/$1 [L,R=301]
RedirectMatch 301 ^/wiki/International_Students http://www.mcr.trinhall.cam.ac.uk/prospective-students/international/
RedirectMatch 301 ^/wiki/McMenemy_Seminar http://www.mcr.trinhall.cam.ac.uk/academics/mcmenemy-seminars/
RedirectMatch 301 ^/wiki/Welfare http://www.mcr.trinhall.cam.ac.uk/welfare/counselling/
RedirectMatch 301 ^/wiki/Transportation http://www.mcr.trinhall.cam.ac.uk/prospective-students/getting-around/
RedirectMatch 301 ^/wiki/Grants http://www.mcr.trinhall.cam.ac.uk/college-support/grants-and-funding/
RedirectMatch 301 ^/wiki/Grants_and_Funding http://www.mcr.trinhall.cam.ac.uk/college-support/grants-and-funding/
RedirectMatch 301 ^/wiki/Cake http://www.mcr.trinhall.cam.ac.uk/welfare/peer-support/
RedirectMatch 301 ^/wiki/Disability_Access http://www.mcr.trinhall.cam.ac.uk/welfare/students-with-disabilities/
RedirectMatch 301 ^/wiki/Recycling_Instructions http://www.mcr.trinhall.cam.ac.uk/current-college-residents/green/
RedirectMatch 301 ^/wiki/Bateman_Street http://www.mcr.trinhall.cam.ac.uk/prospective-students/accommodation/
RedirectMatch 301 ^/wiki/Life_in_Cambridge http://www.mcr.trinhall.cam.ac.uk/prospective-students/eating-in-cambridge/
RedirectMatch 301 ^/wiki/Prospective_Students http://www.mcr.trinhall.cam.ac.uk/prospective-students/accommodation/
RedirectMatch 301 ^/wiki/Leslie_Stephen_Room http://www.mcr.trinhall.cam.ac.uk/college-facilities/function-rooms/
RedirectMatch 301 ^/wiki/Dining http://www.mcr.trinhall.cam.ac.uk/dining-in-college/formal-dinners/
RedirectMatch 301 ^/wiki/Mailing_list http://www.mcr.trinhall.cam.ac.uk/your-mcr/mailing-lists/
RedirectMatch 301 ^/wiki/MCR_Committee http://www.mcr.trinhall.cam.ac.uk/your-mcr/committee/
#RedirectMatch 301 ^/wiki/ http://www.mcr.trinhall.cam.ac.uk/
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

The problem is in this line:
RewriteCond %{HTTP_HOST} !^www.mcr.trinhall.cam.ac.uk$ [NC]
You NEED to escape dot . character if you want to achieve what you want. In regular expressions the dot character means ANY character. You current regex will match www.mcr.trinhall.cam.ac.uk as well as www-mcr-trinhall-cam-ac-uk as well as www8mcr8trinhall8cam8ac8uk etc -- you have got the idea.
To solve it escape the dots:
RewriteCond %{HTTP_HOST} !^www\.mcr\.trinhall\.cam\.ac\.uk$ [NC]
or use non-regex match (plain string comparison):
RewriteCond %{HTTP_HOST} !=www.mcr.trinhall.cam.ac.uk [NC]

Related

htaccess redirect not working for inner pages / folders

Ok, so I've got a stack of htaccess redirects from one domain to another, which for the most part works fine. However, this particular link isn't working:
http://giccampers.com.au/general-trailers/tipper-trailer/
It should take people to this website:
http://blackseriescampertrailers.com.au/
If you go to the main domain of http://giccampers.com.au/ it redirects fine. But I can't seem to get any of the other pages to redirect from that domain.
Here's what I've got (I didn't set these up, btw):
RewriteCond %{HTTP_HOST} ^giccampers.com.au [NC]
RewriteRule ^(.*)$ http://blackseriescampertrailers.com.au/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.giccampers.com.au [NC]
RewriteRule ^(.*)$ http://blackseriescampertrailers.com.au/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^giccampers\.com\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.giccampers\.com\.au$
RewriteRule ^/?$ "http\:\/\/www\.blackseriescampertrailers\.com\.au\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^www.giccampers.com.au [NC]
RewriteRule ^(.*)$ http://blackseriescampertrailers.com.au/$1 [L,R=301]
redirect 301 /general-trailers/tipper-trailer/ http://blackseriescampertrailers.com.au/
Any help would be great!
use this
RedirectMatch 301 http://giccampers.com.au/general-trailers/tipper-trailer/ http://blackseriescampertrailers.com.au/
RedirectMatch 301 /general-trailers/tipper-trailer/ http://blackseriescampertrailers.com.au/
or
RedirectMatch 301 /tipper-trailer$ http://blackseriescampertrailers.com.au/
or
RedirectMatch 301 /general-trailers/tipper-trailer(.*)http://blackseriescampertrailers.com.au/

Conditional redirects in .htaccess

I'm not sure if what I want to achieve is even possible via .htaccess, I have trawled Google without any luck.
I have a pile of redirects to apply to a multi-store website which means that two domains will share the same .htaccess file. The problem that I have is that the redirects for each domain are conflicting with each other.
Is it possible set up the equivalent of a php 'if' statement within the .htaccess file without adding a condition before each redirect?
<If domain = http://www.example-1.com>
RedirectMatch 301 ^/en/1-2-drive-torque http://www.example-1.com/90-britool-expert-torque-wrenches
RedirectMatch 301 ^/en/10-Britool http://www.example-1.com/24-britool-expert-spanners
RedirectMatch 301 ^/en/11-Britool http://www.example-1.com/78-britool-expert-combination-spanners
RedirectMatch 301 ^/en/12-Britool http://www.example-1.com/77-britool-expert-ratchet-spanners
RedirectMatch 301 ^/en/13-Britool http://www.example-1.com/79-britool-expert-open-end-spanners
etc...
</If>
<If domain = http://www.example-2.com>
RedirectMatch 301 ^/en/1-2-drive-torque http://www.example-2.com/390-britool-expert-torque-wrenches
RedirectMatch 301 ^/en/10-Britool http://www.example-2.com/624-britool-expert-spanners
RedirectMatch 301 ^/en/11-Britool http://www.example-2.com/478-britool-expert-combination-spanners
RedirectMatch 301 ^/en/12-Britool http://www.example-2.com/677-britool-expert-ratchet-spanners
RedirectMatch 301 ^/en/13-Britool http://www.example-2.com/779-britool-expert-open-end-spanners
etc...
</If>
Any help or advice will be very much appreciated
You'll need to use mod_rewrite for this instead and use the %{HTTP_HOST}. Unfortunately, there's no way to create "if" containers like you have in your example. You'll need to duplicate the condition each time:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example-1\.com$ [NC]
RewriteRule ^en/1-2-drive-torque /90-britool-expert-torque-wrenches [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-1\.com$ [NC]
RewriteRule ^en/10-Britool /24-britool-expert-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-1\.com$ [NC]
RewriteRule ^en/11-Britool /78-britool-expert-combination-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-1\.com$ [NC]
RewriteRule ^en/12-Britool /77-britool-expert-ratchet-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-1\.com$ [NC]
RewriteRule ^en/13-Britool /79-britool-expert-open-end-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-2\.com$ [NC]
RewriteRule ^en/1-2-drive-torque /390-britool-expert-torque-wrenches [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-2\.com$ [NC]
RewriteRule ^en/10-Britool /624-britool-expert-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-2\.com$ [NC]
RewriteRule ^en/11-Britool /478-britool-expert-combination-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-2\.com$ [NC]
RewriteRule ^en/12-Britool /677-britool-expert-ratchet-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-2\.com$ [NC]
RewriteRule ^en/13-Britool /779-britool-expert-open-end-spanners [L,R=301]

.htaccess website.com/jp to website.com/lang=jp

I need to redirect people who type
domain.com/jp or www.domain.com/jp
to
www.domain.com/lang=jp
My .htaccess already includes this
RewriteEngine On
rewritecond %{http_host} ^domain.com$ [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
Thank you for any help
This should work
RewriteEngine On
RewriteRule ^([^/]+)$ http://www.domain.com/lang=$1 [R=302,nc]
Change R=302 to R-301 when you are sure the redirect works.
EDIT:
RedirectMatch 302 /jp http://www.domain.com/lang=jp
RedirectMatch 302 /cz http://www.domain.com/lang=cz
RedirectMatch 302 /eng http://www.domain.com/lang=eng
RedirectMatch 302 /en http://wwwdomain.com/lang=en

301 redirect, from index.php

How can I redirect via 301, from http://mydomain.com/index.php to http://mydomain.com or http://mydomain.com/variable to http://mydomain.com ? I tried this, but doesn't work:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^http://mydomain.com/variable [nc]
rewriterule ^(.*)$ http://mydomain.com/$1 [r=301,nc]
Try:
RedirectMatch 301 ^/index.php$ /
RedirectMatch 301 ^/variable$ /

301 Redirects with dynamic URLs

I am trying to rewrite my .htaccess file to redirect old dynamic URLS to new ones.
I have tried these so far but they are not working as expected. The second rule seems to redirect to the first one.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^(.*)$ "http\:\/\/www\.mydomain\.com\/$1" [R=301,L]
Redirect 301 /product.php?l=old-product-name1 http://www.mydomain.com/product.php?l=new-product-name1
Redirect 301 /product.php?l=old-product-name2 http://www.mydomain.com/product.php?l=new-product-name2
Can anyone help me redirect these properly?
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^(.*)$ "http\:\/\/www\.mydomain\.com\/$1" [R=301,L]
RewriteCond %{QUERY_STRING} =l=old-product-name1
RewriteRule ^product.php$ http://www.mydomain.com/product.php?l=new-product-name1 [R=301]

Resources