Search code examples
apache.htaccesssubdomain

Moving site to a subdomain, .htaccess redirections Apache


I have a site at the address http://example.com, it has been configured to remove .html (.php, etc.) at the end of url and prevent from hot-linking events (except Search Engines).

I want to set up a multi-regional site with subdomains like http://us-en.example.com and move everything from my current site http://example.com to http://us-en.example.com completely.

I have several questions regarding .htaccess file:

Question 1

Example: User have a "clear" link like this http://example.com/images/all/image1, but since I move everything this path contains no images and didn't exist.

How to set up a config file to redirect this "clean" url to a "default" subdomain (us-en), from http://example.com/images/all/image1 to http://us-en.example.com/images/all/image1, but do nothing if link already contains a subdomain?

Question 2

How to set up .htaccess file properly for all subdomains to have the same features as the original one (removing .html/.php and prevent hot-linking)?

Question 3

How to do all of this from the template bellow?

DirectoryIndex home.html

Options +MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)\.html$ /$1 [R=301,L]


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_REFERER}     !^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g?|png)$ [NC]
RewriteCond %{HTTP_REFERER}     !^https?://([^.]+\.)?example\. [NC]
RewriteCond %{HTTP_REFERER}     !search\?q=cache                 [NC]
RewriteCond %{HTTP_REFERER}     !google\.                        [NC]
RewriteCond %{HTTP_REFERER}     !bing\.                          [NC]
RewriteCond %{HTTP_REFERER}     !yahoo\.                         [NC]
RewriteRule \.(gif|jpe?g?|png)$ - [F,NC,L]

Solution

  • First, you should not use .htaccess at all if you have access to apache configuration files (for detailed explanation look here)

    Q1:

    in .conf:

    <VirtualHost *:80>
      ServerName example.com
      Redirect / https://us-en.example.com/
    </VirtualHost>
    

    or (if you don't want to use .conf files), put this on top of .htaccess:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} =example.com
    RewriteRule ^ https://us-en.example.com%{REQUEST_URI}
    

    For other questions, there is nothing domain specific in your rules that would prevent them from being reused. You should only change (when moving rules from .htaccess to VirtualHost configuration)

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

    to

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

    since in VirtualHost context, the Pattern will initially be matched against the part of the URL after the hostname and port (and before the query string) so you need leading / there