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

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.

Related

How to redirect and in the same time allow any files in directory .htaccess?

currently, I've used this code in my htaccess
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Allow any files or directories that exist to be displayed directly
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
Options -Indexes
This will hide my directory and let me view my file, but how to put together redirect (code below) into above code?
RewriteCond %{HTTP_HOST} !^www\.nextdomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.nextdomain.com/$1 [L,R=301]
Okay here the answers after get guide from #Nic3500
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} !^www\.nextdomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.nextdomain.com/$1 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]

htaccess skip all following rules for subdomain match

I'm having problems with this answer https://stackoverflow.com/a/16800319/1283556
My current HTACCESS 301 redirects example.com to https://www.example.com, adding www. and https:// in as few redirects as possible.
I don't want it to add www. or https:// if the subdomain is m.example.com so I thought matching this condition and putting no rule, and using the [L] flag to stop doing future conditions would work.
RewriteEngine On
# Skip forcing www or https if the 'm' subdomain is being used for mobile
RewriteCond %{HTTP_HOST} ^m\. [NC]
RewriteRule - [L]
# Force www. and HTTPS
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
# Force HTTPS (www. was already there)
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Don't remap resources to the index processor, just add SSL
RewriteCond %{REQUEST_FILENAME} ^.*^.ico$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.css$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.js$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.gif$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.png$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.jpg$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.jpeg$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.xml$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.txt$$
RewriteRule ^(.*)$ https://$1 [R=301,L]
# Remap /page/filter/?query to /index.php?page=page&filter=filter&query
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/*([a-zA-Z0-9-]*)/?$ /index.php?page=$1&filter=$2 [QSA]
The effect of the above is:
example.com »301» https://www.example.com
m.example.com »301» https://www.m.example.com
The desired effect is:
example.com »301» https://www.example.com
m.example.com (do nothing, just serve content from example.com/mobile)
Server Version: Apache/2.4.10 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4 Apache
It's worth noting that I got this working as a solution but I consider it a work-around and not a linear solution:
RewriteEngine On
# Force www. and HTTPS
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^m\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
# Force HTTPS (www. was already there)
RewriteCond %{HTTP_HOST} !^m\. [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Don't remap resources to the index processor, just add SSL
RewriteCond %{REQUEST_FILENAME} ^.*^.ico$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.css$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.js$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.gif$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.png$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.jpg$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.jpeg$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.xml$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.txt$$
RewriteRule ^(.*)$ https://$1 [R=301,L]
# Remap /page/filter/?query to /index.php?page=page&filter=filter&query
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/*([a-zA-Z0-9-]*)/?$ /index.php?page=$1&filter=$2 [QSA]
Your original and updated question has two 301 redirect rules that are not good for both SEO rankings or user experience.
You can combine both rules i.e. http -> https and force-www into one and also take care of excluding m. host in the same rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?!m\.)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Make sure to keep this rule as very first rule and test it after completely clearing your browser cache.

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.

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]

Redirect second domain name?

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]