4

I run a university club website which has two three (update: I have recently discovered the third) domains, let's say www.meow.co.uk, foo.uni.ac.uk/bar/meow/ and uni.ac.uk/bar/meow. I'm using the following .htaccess in the folder foo.uni.ac.uk/bar/meow/ in order to remove ".html" from the pages.

#meow.co.uk/page will display the contents of meow.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

#302 from meow.co.uk/page.html to meow.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=302,L]

This works fine on www.meow.co.uk/page.html --> www.meow.co.uk/page but for the other domain, it treats foo.uni.ac.uk/ as the root directory, so foo.uni.ac.uk/bar/meow/page.html --> foo.uni.ac.uk/page. I want it to go to --> foo.uni.ac.uk/bar/meow/page.

How can I modify the htaccess file so that it works for both domains? Or failing that how can I redirect from foo.uni.ac.uk/bar/meow to www.meow.co.uk?

2

RewriteRule ^(.*)\.html$ /$1 [R=302,L]

The problem is that for www.meow.co.uk you need to redirect to /$1 (as above) and for foo.uni.ac.uk you need to redirect to /bar/meow/$1. I don't think there is an immediate way around that.

However, you could conditionally set an environment variable (eg. BASE_URL) to the value of the required base URL for the redirect depending on the value of the Host. And use this environment variable in the RewriteRule directive.

For example, at the top of your script, set the environment variable:

SetEnvIf Host "foo\.uni\.ac\.uk" BASE_URL=/bar/meow

When the site is accessed via www.meow.co.uk then BASE_URL will not be set (ie. it's empty).

Then, change your RewriteRule substitution to use this:

RewriteRule ^(.*)\.html$ %{ENV:BASE_URL}/$1 [R=302,L]

You should change the 302 (temporary) redirect to a 301 (permanent) redirect when you have confirmed it's working OK.


UPDATE: However, you are better off canonicalizing the URL and redirecting to the canonical domain (ie. www.meow.co.uk). With three different domains you have a lot of potential duplicate content. So, instead of the above, simply redirect to the canonical domain at the top of your script:

RewriteCond %{HTTP_HOST} !=www.meow.co.uk [NC]
RewriteRule ^bar/meow/(.*) http://www.meow.co.uk/$1 [R=301,L]

Just to clarify... you have already changed the URLs on your site to use the extension-less version and this redirect is only required for old URLs that might have been indexed, shared, etc. Yes?

  • Actually, I haven't changed the URLs in my anchors to remove the .html extension - what is the reason for doing so? When I'm testing the website on my local drive, those anchors wouldn't work if I did that – binaryfunt Sep 30 '15 at 14:40
  • 1
    If you don't change the URLs on your site then every time a user clicks a link they will be redirected - 2 requests to your server. You would need to install a webserver locally to test. – MrWhite Sep 30 '15 at 16:08
  • I've updated my answer... in order to avoid "duplicate content" (particularly with 3 domains) you are better off redirecting to the canonical URL. – MrWhite Sep 30 '15 at 16:20
  • The rule in your update doesn't work for me. When I navigate to https://foo.uni.ac.uk/bar/meow/page/ it redirects to https://foo.uni.ac.uk/http://www.meow.co.uk/page/ – binaryfunt Oct 1 '15 at 12:42
  • 1
    (I'm using 302s until I can get htaccess to behave the way I want. Using 301s is risky as they will be aggressively cached) – binaryfunt Oct 1 '15 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.