Karma Kaos
Karma Kaos

Reputation: 69

htaccess alias domain to main domain AND keep the filename

I have 2 additional domains which I want to use as aliases to my main domain. I have this setup in my htaccess but it is not 100% right. Whenever I access a page from either one of the 2 domains it always redirects to the main domain index page.

e.g. going to https://bumpstudios.com/downloads redirects to https://www.bumpstudios.co.uk/ when it needs to redirect to https://www.bumpstudios.co.uk/downloads

here is my htacess file

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews -Indexes
</IfModule>

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule .* https://www.bumpstudios.co.uk%{REQUEST_URI} [R,L]

RewriteBase /

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

RewriteCond %{HTTP_HOST} ^bumpstudios.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.bumpstudios.com$ [OR]
RewriteCond %{HTTP_HOST} ^bumpstudios.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.bumpstudios.uk$ [OR]
RewriteCond %{HTTP_HOST} ^bumpstudios.co.uk$
RewriteRule (.*)$ https://www.bumpstudios.co.uk/{REQUEST_URI} [R=301,L]
</IfModule>

Upvotes: 1

Views: 282

Answers (2)

MrWhite
MrWhite

Reputation: 45958

RewriteCond %{HTTP_HOST} ^bumpstudios.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.bumpstudios.com$ [OR]
RewriteCond %{HTTP_HOST} ^bumpstudios.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.bumpstudios.uk$ [OR]
RewriteCond %{HTTP_HOST} ^bumpstudios.co.uk$
RewriteRule (.*)$ https://www.bumpstudios.co.uk/$1 [R=301,L]

This could be simplified... instead of matching all the domains/hostnames it shouldn't be, you could simply use a negated condition (! prefix) and match the single canonical hostname.

For example:

 RewriteCond %{HTTP_HOST} !=www.bumpstudios.co.uk
 RewriteRule (.*) https://www.bumpstudios.co.uk/$1 [R=301,L]

If it's not www.example.co.uk then redirect to www.example.co.uk. This uses a lexicographical string comparison (= prefix), not a regex. So the literal dots do not need to be escaped (something that was missing from your regex above).

RewriteRule (.*)$ https://www.bumpstudios.co.uk/{REQUEST_URI} [R=301,L]

You were missing a % from %{REQUEST_URI}.

But as you found, the order of these directives is important.

Upvotes: 0

Karma Kaos
Karma Kaos

Reputation: 69

After a bit more fiddling the issue now seems to be fixed. I removed the RewriteBase / and put the domain rewriteCond rules near the top. Seems to have done the trick. Here is the modified version

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews -Indexes
</IfModule>

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule .* https://www.bumpstudios.co.uk%{REQUEST_URI} [R,L]

RewriteCond %{HTTP_HOST} ^bumpstudios.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.bumpstudios.com$ [OR]
RewriteCond %{HTTP_HOST} ^bumpstudios.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.bumpstudios.uk$ [OR]
RewriteCond %{HTTP_HOST} ^bumpstudios.co.uk$
RewriteRule (.*)$ https://www.bumpstudios.co.uk/$1 [R=301,L]

# RewriteBase /

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Upvotes: 1

Related Questions