开发者

htaccess redirect woes

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/开发者_Go百科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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜