Redirect second domain name? - apache

I have the following .htaccess file which works fine for say www.companyone.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /index.php?/$1 [L]
I also have the domain www.companytwo.com which points to the same site, i.e. www.companyone.com. The problem is that when someone uses www.companytwo.com which correctly goes to the original website, the url still contains www.companytwo.com, when it should become www.companyone.com.
So in effect, I am ending up with 2 duplicate sites which is bad for seo. Using the above .htaccess file, I am already ensuring that if a user types companyone.com, they automatically get redirected to www.companyone.com. I need the same thing for www.companytwo.com, i.e. companytwo.com and www.companytwo.com should become www.companyone.com.
Is this something I need to fix via the .htaccess file or some virtual server file?

You can redirect the Browser to your www.companyone.com site by using .htacces:
# Redirect to www.companyone.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^companytwo.com$ [OR]
RewriteCond %{HTTP_HOST} ^companyone.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.companytwo.com$
RewriteRule (.*)$ http://www.companyone.com/$1 [R=301,L]
# If your example works, this rewrite to index.php/$1
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /index.php?/$1 [L]

Related

htaccess rewrite rule all existing files and directories in one domain go to same filepath in another domain

I'm trying to write an htaccess rewrite rule to send all existing files and directories at the domain http:// or https:// and www or not lun.re to lunre.com.
So lun.re/admin, which does exist would go to lunre.com, but lun.re/new, which doesn't exist would stay at lun.re, but would redirect to https://lun.re/new.
Here's what I have so far:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{HTTP_HOST} ^lun\.re$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lun\.re$
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://lunre.com/ [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
Lines 5-8 were already there from my hosting provider.
I've also tried this:
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /loader.php [L,R]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{HTTP_HOST} ^(www\.)?lun.re$
RewriteRule ^ http://lunre.com/ [L,R]
But then I get a too many redirects error.
What exactly I am trying to do is if it does exist and the domain is lun.re then go to https://lunre.com/file-or-dir (the same filepath at the long url) otherwise if it doesn't exist then stay at the current domain and go to https://currentdomain/loader.php
You can use this:
RewriteEngine on
# http to https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
#redirect all existing files/directories to http://lunre.com
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{HTTP_HOST} ^(www\.)?lun.re$
RewriteRule ^ http://lunre.com/ [L,R]
You don't need a RewriteRule to Redirect non-existent requests back to your main domain as the Rule only redirects existing files/folders.

Apache htccess rewrite rules

My site is built in Angularjs, I am trying to re direct my site to html static snapshots pages in a snapshot folder, I can mange to get google to re-direct to the snapshot folder and to the correct links inside the folder, but cant seem to get Facebook to work, any thoughts?
Here is my htaccess file below
RewriteEngine on
# RewriteRule for GOOGLE ROOT URL
RewriteCond %{HTTP_USER_AGENT} Googlebot
RewriteCond %{HTTP_HOST} upfrontbeats\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /snapshots/index.html [L]
# RewriteRule for GOOGLE ROOT ALL OTHER URLS
RewriteCond %{HTTP_USER_AGENT} Googlebot
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ /snapshots/%1? [NC,L]
# RewriteRule for FACEBOOK
RewriteCond %{HTTP_USER_AGENT} facebookexternalhit/1.1 [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_=(.*)$
RewriteRule ^(.*)$ /snapshots/$1? [NC,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.php [NC,L]

redirect with .htaccess rewrite rule to different folder on my server

I have been trying to redirect to a different folder with .htaccess when I hit a domain.
Here's my .htaccess redirect rule, can you tell me where am I wrong?
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
So, what I want is, if someone visits racereadymotorsports.in, they redirect to /raceready/ directory
and on the other hand if they visit to annelies-slabbynck.com, they should redirect to /annelies/ directory
At present, this redirects twice for annelies-slabbynck.com which means that, I end up with:
http://www.annelies-slabbynck.com/annelies/annelies/ as my final url
I am running a shared hosting and do not have access to add a new configuration.
Please help.
You need to remove the [OR] flag and reproduce the same conditions with the other rule. Maybe something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
Or better yet, you can use these conditions instead: RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteCond %{DOCUMENT_ROOT}/raceready/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/raceready/$1 -d
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteCond %{DOCUMENT_ROOT}/annelies/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/annelies/$1 -d
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
Note that the R=301 flag in your second rule redirects the browser, unlike the first rule which only internally rewrites the request.

Apache Redirect - same domain different REQUEST_URI

I need multiple RewriteCond for the same domain, but different REQUEST_URIs
I need to redirect to a different domain depending on the REQUEST_URI.
I am trying this with no luck:
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteCond %{REQUEST_URI} !^/myuir/ [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.*)$ / [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteCond %{REQUEST_URI} !^/myuniqueuri/ [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.*)$ http://example2.com/$! [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteCond %{REQUEST_URI} !^/myotheruniqueuri/ [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.*)$ http://example3.com/$! [R=301,L]
I thought the [OR] on the request uri condition would work, but it did not. I have also tried without the [OR] and it did not work!
For example:
if the url is:
http://example.com/myuniqueuri
needs to redirect to:
http://example2.com
if the url is:
http://example.com/myotheruniqueuri
needs to redirect to:
http://example3.com
where example2 and example3 can be anything but are not the same.
Any help is greatly appreciated
Thanks
I think the problem might be in your syntax. The reference is $1 not $!. Also the ! is being used incorrectly. This basically means not. Like if not this directory then etc. Well it appears you want to match the directory and then redirect based on that. Try this code and see if it works for you.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/myuir
RewriteRule ^(.*)$ / [R=301,L]
RewriteCond %{REQUEST_URI} ^/myuniqueuri
RewriteRule ^(.*)$ http://example2.com/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/myotheruniqueuri
RewriteRule ^(.*)$ http://example3.com/ [R=301,L]

Redirect request and hide .php extension from the request

We have started to change our website, and we use the old and the new site together. If something not exist on the new site we just redirect them back to the old one. (This is a transparent process for the user.)
This is how .htaccess rules looks like now:
RewriteEngine On
RewriteBase /
RewriteCond %{http_host} ^domain.hu [NC]
RewriteCond %{http_host} !^domain.hu.data18.websupport.sk$ [NC]
RewriteRule ^(.*)$ http://www.domain.hu/$1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/static-old%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static-old%{REQUEST_URI} -d
RewriteRule ^(.*)$ /static-old/$1 [QSA]
RewriteRule ^(.*)\.html$ $1.php [L,R=301,QSA]
So if we don't find the the file on the main directory, but we find it in the old one we serve the old one instead of a 404. And if the requested file has html extension then we rewrite it internally to php, because we changed this a while ago.
I want to hide the php extension (and so html will be hidden too) so if a requested uri is /something.php or /something.html then only show /something to the user. But I if I edit .htacces like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/static-old-from-seocms%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static-old-from-seocms%{REQUEST_URI} -d
RewriteCond %{REQUEST_URI} ^(.*?)\.php[\s?] [NC]
RewriteRule ^/%1/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/static-old-from-seocms%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static-old-from-seocms%{REQUEST_URI} -d
%{REQUEST_URI} !\.(.*)$
RewriteRule ^(.*)$ $1.php [L,R=301,QSA]
RewriteRule ^(.*)$ /static-old-from-seocms/$1 [QSA,L]
I will get an 500 internal server error, the .htaccess parsing fail.
I think this script will check if the requested file has php extension and does not exist in the main directory then redirect it to the same address without the php extension, after that the script will check if the requested uri is not exist in the main directory and does not have an extension then write it a php extension to it internally.
Try this .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{http_host} ^domain\.hu [NC]
RewriteCond %{http_host} !^domain\.hu\.data18\.websupport\.sk$ [NC]
RewriteRule ^(.*)$ http://www.domain.hu/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.(php|html)[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)/?$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/static-old%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static-old%{REQUEST_URI} -d
RewriteRule ^(.+)$ /static-old/$1 [L]