is it possible with .htaccess to mask a redirect of host to subdomain.
So that example.co.uk is masked to subdomain.test.co.uk, but still appears in browser as example.co.uk
I have tried using
RewriteCond %{HTTP_HOST} example\.co\.uk [NC]
RewriteRule ^(.*)$ http://subdomain.test.co.uk/$1 [NC,L]
But that just the redirect host to subdomain.test.co.uk, which isn't what I want.
Tried using [r=301] as one of the params too.
I have very limited access to hosting and it doesn't allow add-on domains (currently using parked domains to get host in .htaccess). If there is another solution I'm all ears :)
UPDATE
Tried changing the .htaccess to :
Options +FollowSymLinks -MultiViews
ProxyRequests On
ProxyPreserveHost On
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} simplysleeptime\.co\.uk
RewriteRule ^(.*)$ http://simplysleeptime.labithiotis.co.uk/$1 [NC,P]
RewriteCond %{HTTP_HOST} zurvivor\.co\.uk [NC]
RewriteRule ^(.*)$ http://zurvivor.labithiotis.co.uk/$1 [NC,P]
but i can't edit the httpd.conf to enable mod_proxy as I don't have full access to godaddy hosting.
Related
I am having trouble with an htaccess setup. My goal is to have a wildcard sub directory point to the appropriate folder but without changing the URL. I followed some examples here on stackoverflow and elsewhere. I already have my wildcard setup in DNS, Virtual Host is good to go and mod packages. All that is left is the htaccess pointing to the matching folder without changing the url in the browser.
This Works as in I see the domain.com/example/index.html file (But the url changes from example.domain.com to domain.com/example in browser)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1/$1 [L,NC,QSA]
</IfModule>
When I swap out the L flag for the P flag so that the url doesnt change. When I do and I try example.domain.com the url does not change as intended but it shows me what I would see if I had went to domain.com instead of what is in the /example folder.
Here is what I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1/$1 [P,NC,QSA]
</IfModule>
What am I doing wrong?
You are trying to provide the URL in your substitution, which causes the redirection. Try to simply set config as follows:
In VirtualHost
ServerName example.com
ServerAlias *.example.com
DocumentRoot /path/to/your/doc/root
In htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteCond %{REQUEST_URI} !^/%1 [NC]
# Please confirm whether you need the next line in your rules.
# I should think that it'd be required
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [L,QSA]
While looking for an answer for the question above I have encountered this script as a solution(I would like to have my website on http except of a few pages like login, register,manage etc.):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# force https for /login.php and /register.php
RewriteCond %{HTTPS} =off
RewriteRule ^(login|register)\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# don't do anything for images/css/js (leave protocol as is)
RewriteRule \.(gif|jpe?g|png|css|js)$ - [NC,L]
# force http for all other URLs
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(login|register)\.php$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This exact example should work on my server, but it works only partially, meaning, it redirects all pages to http, but I am unable to open login.php or register.php. The webbrowser states that these pages include redirect loop and they can't show up. By no means I am not an expert on mode_rewrite or htaccess so I would appreciate any help.
edit:
I have followed the suggestions and run mywebsite with chrome plugin. I discovered that in the page login.php has redirect loop http->https->http etc. (The only other redirection on that page is when the user is already signed, but it doesn't seem ike a couse for this loop). I have tried also different codes for setting SSL and this one works:
# Rewrite Rules for example.com
RewriteEngine On
RewriteBase /
# Turn SSL on for payments
RewriteCond %{HTTPS} off
RewriteCond %{SCRIPT_FILENAME} \/login\.php [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Turn SSL off everything but payments
RewriteCond %{HTTPS} on
RewriteCond %{SCRIPT_FILENAME} !\/login\.php [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
And this one is working correctly, but it has only one exception for https and I would like to have more of them(around 6). Has someone any idea why the first script is not working? or how to modify the second one in order to have more https websites?
Thanks
I had similar problem, this is what worked for me.
In your httpd.conf, under virtual hosts, make sure you have both:
ServerName domain.com
ServerAlias www.domain.com
BOTH in VirtualHost *:80 AND VirtualHost *:443
I have a site example.com. Any traffic to www.example.com is redirected to example.com in the .htaccess file using:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
This piece of code that I swiped of the interweb works fine.
I have now added a subdomain subdom.example.com. Similar, any traffic to www.subdom.example.com should be redirected to the non www canonical version.
The following code does not work:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://subdom.example.com/$1 [R=301,L]
Presumably redirects work a little differently where subdomains are involved. Could anyone share how I would edit the above snippet to redirect any www. traffic to the canonical non www. subdomain version?
I believe you don't have sufficient RewriteCond conditions for both redirects. What is happening is that first one is unconditional redirect and since it appears first it always fires and 2nd one never fires.
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Above code will work with both of your domains.
I have a domain staticbackup.mydomain.com which has a static backup of the main site incase of server failure I can quickly switch the www. cname to point to staticbackup.mydomain.com (A record)
On the staticbackup I don't want the site accessible if you visit via statisbackup.mydomain.com, only if it's www.mydomain.com
Is there a way I can do this with htaccess?
This is what I have created (the site is live and I cannot test)
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^staticbackup.mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/ [r=301,nc]
I would recommend you deny access to that domain:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^staticbackup.mydomain.com [NC]
RewriteRule .* - [F]
I have a situation where we've redesigned a site and have moved it to a new domain. The old site has a few client subdomains that we need to redirect to the new domain, but to folders that we will explicitly specify. Note that the new domain is actually a subdomain of the old domain, so not sure how that complicates anything, if at all.
For example, oldsite.com currently redirects all request to new.oldsite.com with the following rule:
RewriteCond %{HTTP_HOST} !^\.oldsite\.com
RewriteRule ^(.*)$ http://new.oldsite.com/$1 [R=302,L]
I also want to redirect some specific subdomains to folders of new.oldtsite.com, like subdomain.oldsite.com to new.oldsite.com/subdomain/. There also the likelihood that the old subdomain and the new directory will have different names (subdomain.oldsite.com to new.oldsite.com/subdomain-name/. :-/
I've attempted to duplicate this rule for the subdomain to folder redirect, but that just hijacks all redirects.
I will give you a virtual high-five if you can spot me a fix.
Have your rules from specific to generic. Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^subdomain\.oldsite\.com$ [NC]
RewriteRule ^(.*)$ http://new.oldsite.com/subdomain-name/$1 [R=302,L]
RewriteCond %{HTTP_HOST} ^oldsite\.com$ [NC]
RewriteRule ^(.*)$ http://new.oldsite.com/$1 [R=302,L]
EDIT: To redirect any.oldsite.com/foo to new.oldsite.com/any/foo
RewriteCond %{HTTP_HOST} ^([^.]+)\.oldsite\.com$ [NC]
RewriteRule ^(.*)$ http://new.oldsite.com/%1/$1 [R=302,L]
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.