Virtual domains and subdomains using mod_rewrite - mod-rewrite

I would like to create a service where a user either can get a subdomain or a regular domain on my service.
The idea is that all subdomains except www. should go to /subdomains.php?user=[sub]&url=[requestet url]
And all domains except my own 2 domains should go to /domain.php?url=$1
My 2 domains are:
domain1.com and domain2.se
I've tried the following code:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
# Rewrite sub domains.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.domain1\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain1\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.domain2\.se$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain2\.se$ [NC]
RewriteRule ^(.*)$ /subdomain.php?user=%2&url=$1 [QSA,L]
#Rewrite
RewriteCond %{HTTP_HOST} !^www\.domain1\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.domain2\.se$ [NC]
RewriteRule (.*) http://www.domain1.com/domain.php?url=$1 [R=301,L]
</IfModule>
But it does not seen to work.

Sub domains: how do you expect an URL to be both a subdomain of domain1.com and domain2.se at the same time?
Try e.g. this simplification:
RewriteCond %{HTTP_HOST} !^www\.(domain1\.com|domain2\.se)$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.(domain1\.com|domain2\.se)$ [NC]

Related

How to point a subdomain to a subfolder

I have an Apache server running ISPConfig. On a domain example.com I am pointing its subdomains to subfolders, e.g. foo.example.com to /sub/foo. This works fine:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/example\.com/
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule ^/(.*)$ /sub/%1/$1 [L]
Now I need to point one specific subdomain into a subfolder's subfolder (bar.example.com into /sub/bar/public to use Laravel). But for some reason, this does not work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^bar\.example\.com/
RewriteRule ^/(.*)$ /sub/bar/public/$1 [L]
RewriteCond %{REQUEST_URI} !^/example\.com/
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule ^/(.*)$ /sub/%1/$1 [L]
The problem was the / at the end of the domain name. If I delete it or put there $, it works.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^bar\.example\.com$
RewriteRule ^/(.*)$ /sub/bar/public/$1 [L]
RewriteCond %{REQUEST_URI} !^/example\.com/
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule ^/(.*)$ /sub/%1/$1 [L]

Having difficulty OHS rewrite rule for multiple domains

I'm having a bit of difficulty with rewriting on Oracle HTTP Server for multiple domains that point to same IP address and port
Following is working
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub-doamin-1/psp/UACMP/SELF_SERVICE/SA/c/NUI_FRAMEWORK.PT_LANDINGPAGE.GBL [R,L]
However when I try https://sub-doamin-2/analytic it redirects to the https://sub-doamin-1/psp/UACMP/SELF_SERVICE/SA/c/NUI_FRAMEWORK.PT_LANDINGPAGE.GBL
Tried RewriteCond ${HTTP_HOST} method with no luck. It just redirect to / (root)
RewriteEngine On
RewriteCond ${HTTP_HOST} sub-doamin-1$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub-doamin-1/psp/UACMP/SELF_SERVICE/SA/c/NUI_FRAMEWORK.PT_LANDINGPAGE.GBL [R,L]
RewriteCond ${HTTP_HOST} sub-doamin-2$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub-doamin-2/analytics
Can you please assists resolving this issue?
It should be %{HTTP_HOST} instead of ${HTTP_HOST}
So the rules should be:
RewriteCond %{HTTP_HOST} sub1.test.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub1.test.com/psp/UACMP/SELF_SERVICE/SA/c/NUI_FRAMEWORK.PT_LANDINGPAGE.GBL [R,L]
RewriteCond %{HTTP_HOST} sub2.test.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub2.test.com/analytics [L]
You can check the rules here: https://htaccess.madewithlove.be?share=6632e45c-a7bb-5099-ab0b-468ba1066277
for the urls https://sub1.test.com and https://sub2.test.com
If you write your original rules in that website you will get This test string is not supported: ${HTTP_HOST} so this can also help you next time.

Rewrite example.txt to another based on the domain

Im using a multi-install for my webpage with two different domains and i need for each domain a unique robots.txt
like
https://www.domain1.tdl/robots.txt should use the https://www.domain1.tdl/robots_domain1.txt
and
https://www.domain2.tdl/robots.txt should use the https://www.domain2.tdl/robots_domain2.txt
Check this rewrites in .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.tdl [NC]
RewriteCond %{REQUEST_URI} ^\/robots\.txt$
RewriteRule (.*) https://www.domain1.tdl/robots_domain1.txt [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.tdl [NC]
RewriteCond %{REQUEST_URI} ^\/robots\.txt$
RewriteRule (.*) https://www.domain2.tdl/robots_domain2.txt [L]

redirect all pages including sub pages to new domain

I tried this:
RedirectMatch 301 (.*) http://olddomain.com$1
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^olddomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
But all subpages are not redirected.
Try this,
RewriteEngine On
# Take care of www.old.com.au
RewriteCond %{HTTP_HOST} ^www.old.com.au$ [NC]
RewriteRule ^(.*)$ http://www.new.com/$1 [L,R=301]
RewriteCond %{QUERY_STRING} ^attachment_id=([0-9]*)$ [NC]
RewriteRule ^$ http://www.new.com/? [R=301,NE,NC,L]
It's simple, I was just using this to do some special rewriting for my own, here is your code:
Put this inside your /www/.htaccess file:
RewriteEngine on
// Rules to redirect to another domain
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.net/$1 [L,R=301,NC]
Check http://www.inmotionhosting.com/support/website/redirects/setting-up-a-301-permanent-redirect-via-htaccess, for 3 more ways to make a redirection.
Are you setting directive AllowOverride All in your Apache config?
Is the mod_rewrite module working?

mod_rewrite to force SSL for a subfolder

I try to make a redirect from a special folder to a special host.
When somebody enter http://mydomain.com/administrator he should be redirected to https://a-otherdomain.com/mydomain.com/administrator
What I have is this in a .htaccess file in DOCUMENT_ROOT .. but it want work :(
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (^|\.)mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/administrator(/*) [NC]
RewriteRule ^/administrator/(.*)$ https://a-otherdomain.com/mydomain.com/administrator/$1 [QSA,R=301,L]
Any Ideas?
Update:
now I use this, which works in apache configuration files for the vhost:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (^|\.)mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/administrator/ [NC]
RewriteRule ^/(.*)$ https://a-otherdomain.com/mydomain.com/$1 [R=301,L]
Thanks,
Thomas
RewriteRule doesn't match leading slash in a URI.
Try this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (^|\.)mydomain\.com$ [NC]
RewriteRule ^administrator(/.*|)$ https://a-otherdomain.com/mydomain.com/$0 [R=302,L,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Resources