Mobile app version of vmapp.org
Login or Join
Tiffany637

: .htaccess rewrite when site has multiple domains 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/

@Tiffany637

Posted in: #Domains #Htaccess #MultipleDomains

I run a university club website which has two three (update: I have recently discovered the third) domains, let's say 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 ^(.+)$ .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$ / [R=302,L]


This works fine on meow.co.uk/page.html --> 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 meow.co.uk?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Tiffany637

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

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


The problem is that for meow.co.uk you need to redirect to / (as above) and for foo.uni.ac.uk you need to redirect to /bar/meow/. 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 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}/ [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. 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/(.*) www.meow.co.uk/ [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?

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme