.htaccess rewrite does not trigger on main URL - .htaccess

I have two different domains who I want to point to separate folders on my server.
This .htaccess file is what I have currently
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?foo\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /foo/index.html [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?bar\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /bar/index.html [R,L]
(Note that the [R] option is only there so I can see what is (not) happening while testing)
It works fine if one goes to www.foo.com/asdf or www.bar.com/something
However, simply going to www.foo.com or www.bar.com (with or without the www. subdomain) won't trigger these rules for some reason.
What am I doing wrong?

It is happening due to RewriteCond %{REQUEST_FILENAME} !-f failing for your home page (index.php or index.html) since either of those files will be found. You set your rules like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?foo\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /foo/index.html [L]
RewriteCond %{HTTP_HOST} ^(www\.)?foo\.com [NC]
RewriteRule ^index\. /foo/index.html [L]
RewriteCond %{HTTP_HOST} ^(www\.)?bar\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /bar/index.html [L]
RewriteCond %{HTTP_HOST} ^(www\.)?bar\.com [NC]
RewriteRule ^index\. /bar/index.html [L]

Related

Redirect from sub.domain.co.id to https://sub.domain.co.id

Good day. I have a website called with sub.domain.co.id and now I want to redirect it to https://sub.domain.co.id.
I'm using codeigniter and this what i change in my .htaccess
Before .htaccess look like this
RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
then I change it to ref(redirecting https://domain.com to https://www.domain.com)
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^http://. [NC]
RewriteRule ^ https://sub.domain.co.id%{REQUEST_URI} [R=301,L,NE]
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
but no help. the URL still use http. How can i achieve that?
try this and see. (index.php will removed too)
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub.domain.co.id [NC]
RewriteRule ^(.*)$ https://www.sub.domain.co.id/$1 [L,R=301]
didn't test. 95% Sure. Not the 100%

Create .htaccess Wildcard with Subdomains

I'm trying to implement a solution using .htaccess and wildcard subdomains so that
http://example.com.domain.com is mapped to http://domain.com/www/example.com
. My rules look something like:
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-\.]+)\.domain\.com$ [NC]
RewriteRule !^www/ www/%2%{REQUEST_URI} [PT,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Try changing the second rule to:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^www/ index.php [L]

Using htaccess to pass directories to GET parameters Without rewriting url

I would like to pass (not redirect) something like this:
http://www.example.com/ (with / optional)
passed to the script
http://www.example.com/index.php
http://www.example.com/foo/ (with / optional)
passed to the script
http://www.example.com/index.php?nav=foo
http://www.example.com/foo/bar (with / optional)
passed to the script
http://www.example.com/index.php?nav=foo&subnav=bar
Furthermore I would like to pass the subdomain http://testumgebung.example.com to http://www.example.com/testumgebung.php with the same parameters as above.
I just managed to do the following, but it rewrites the url in the browser bar to the passed location and doesn't leave the url as before:
RewriteCond %{HTTP_HOST} ^testumgebung\.maskesuhren\.de
RewriteRule ^(.*)$ http://www.maskesuhren.de/$1/testumgebung.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]+/?$
RewriteRule ^([^/]*)/?$ index.html?nav=$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /.*/.*
RewriteRule ^([^/]*)/([^/]*)$ index.html?nav=$1&subnav=$2 [R,L]
You want to get rid of the R in the last 2 rules:
RewriteCond %{HTTP_HOST} ^testumgebung\.maskesuhren\.de
RewriteRule ^(.*)$ http://www.maskesuhren.de/$1/testumgebung.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]+/?$
RewriteRule ^([^/]*)/?$ index.html?nav=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /.*/.*
RewriteRule ^([^/]*)/([^/]*)$ index.html?nav=$1&subnav=$2 [L]
The R in the square brackets tell mod_rewrite to redirect the browser, which changes the URL in the browser's location bar.
.
I had to change the order to achive what I wanted
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]+/?$
RewriteRule ^([^/]*)/?$ ?nav=$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /.*/.*
RewriteRule ^([^/]*)/([^/]*)$ ?nav=$1&subnav=$2 [R,L]
RewriteCond %{HTTP_HOST} ^testumgebung\. [NC]
RewriteRule ^$ testumgebung.html [L]
and to rewrite the html so that the external resources start with a slash.

subdomains linked to different files using htaccess

I'm trying to create redirect to different files for different subdomains, maybe domains in the future. This is based on ModX CMS.
I want to keep url the same, just run different file for each subdomain, passing some data. For domain.com it's index.php, for sub1.domain.com - sub1.php. Also I want to pass some data, you can see it in the example below:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub1\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /sub1.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub2\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /sub2.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub3\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /sub3.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub4\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /sub4.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub5\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /sub5.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub6\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /sub6.php?q=$1 [L,QSA]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I tried to # last 3 lines, but it didnt make any difference. I've got more lines in htaccess, but they are #. Subdomain I create in control panel of my server, all subdomains are ponted to the same directory (public_html).
Right now I'm not sure passing data works, and when I go to sub1.domain.com apache redirect me to www.domain.com/sub1.php, thich is wrong.
How do I link subdomains to their php files and keep url the same (sub1.domain.com)??
Remove redundant stuff and keep your code like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub1\.domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /sub1.php?q=$1 [L,QSA]

htaccess subdomain ssl wildcard not opening file

Ok So i have come across a problem with my htaccess and how to get it to work.
I have just purchased a ssl wildcard for my primary and sub domains.
I am with bluehost and they suggest adding this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.example.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdomain/$1
RewriteCond %{HTTP_HOST} ^subdomain.example.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
At the moment all i have is this...
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ https://www.domain.com/$1 [L]
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sub_folder/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
and that works however, i cannot get it so that when i open the file, it just opens the file with no redirects...
So If i open sub.domain.com/file/fil1.js it will open the index.php which is what i do not want...
but if i do /browse or something like that it works...
Ok after much mucking around and playing around it was the file request which wasnt including the subdomain folder, which was causing it to not find the file location. So I managed to do a manual check for the file, and this is what i got. This seems to do exactly what i want. Im not sure if there is a more efficient way, but this is what it got.
#-------------------SUB.DOMAIN.COM---------------------
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteCond %{DOCUMENT_ROOT}/sub_folder%{REQUEST_URI} -f
RewriteRule . %{DOCUMENT_ROOT}/sub_folder%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sub_folder/index.php [L]
#--------------------------------------------------------------
instead of
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sub_folder/index.php [L]
You need to change your second rule so that it only gets applied when the request is ^(/)?$, then you need to duplicate what bluehost tells you to do. Essentially, replace:
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sub_folder/index.php [L]
With what they tell you to use:
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdomain/$1 [L]
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteRule ^/?$ /sub_folder/index.php [L]

Resources