ISAPI Rewrite - allow a subdomain to only hit a specific folder - url-rewriting

Is there a way with ISAPI v3 to allow a subdomain to only hit a specific folder, and all other requests would redirect to www? I also have some urls under this folder that need to be rewritten. There will also be rewrite rules for valid www urls.
Allow:
http://myaccount.mysite.com/account/
http://myaccount.mysite.com/account/profile/
http://myaccount.mysite.com/account/profile/changeEmail.aspx
Allow, but needs to be rewritten:
http://myaccount.mysite.com/account/edit/123456789.aspx
These should be redirected to www:
http://myaccount.mysite.com/directory/
http://myaccount.mysite.com/folder1/
http://myaccount.mysite.com/folder1/folder2/
etc....

Try using the following:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP:Host} ^myaccount\.mysite\.com$ [NC]
RewriteRule ^account/edit/123456.aspx$ /otherpage [NC,L]
RewriteCond %{HTTP:Host} ^myaccount\.mysite\.com$ [NC]
RewriteRule ^account/.*$ - [NC,L]
RewriteCond %{HTTP:Host} ^myaccount\.mysite\.com$ [NC]
RewriteRule (.*) http://www.mysite.com/$1 [NC,R=301,L]

Related

.htaccess - check if part of URL is missing - 301 redirect for ajax URLS

I have made a copy of a wordpress site and set it up on localhost (WAMP), everything works well apart from all of the AJAX calls as the URLs have not been updated.
I need the urls to be in the following format: http://localhost/mysite/dashboard/ but instead they have the /mysite/ part missing ie. http://localhost/dashboard/
I assumed this could be resolved by doing a 301 redirect in the same way a none www domain is redirected to a www. I have tried the below but it doesn't do anything:
RewriteCond %{HTTP_HOST} !^/mysite/\.
RewriteRule ^(.*)$ %{HTTP_HOST}/mysite/$1 [R=301,L]
---------------------HTACCESS FILE----------------------------
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /mysite/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mysite/index.php [L]
RewriteRule !^mysite/ /mysite/%{REQUEST_URI} [R=301,L,NC]
</IfModule>
# END WordPress
That rule should be:
RewriteRule !^mysite/ /mysite%{REQUEST_URI} [R=301,L,NC]
%{HTTP_HOST} variable only matches domain name in the request not the REQUEST_URI.

Rewrite subdomain to main domain for images only using htaccess

I have a domain, where everything except image\css etc. are handled by a single php file. However after a reorganisation, alot of images have been moved from sub-domains to the main domain. So I'm looking for a way to redirect all image\css files to the main domain if they were originally on one of the sub-domains. My current code is
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.ico|\.bmp|\.css|\.ts|\.js)$
RewriteRule !^index\.php$ /index.php [L]
I've tried a couple of ways to redirect it, but I seem to break on of the existing rules, whatever I try.
Thanks
The final solution I came up with
RewriteEngine On
# Check the request isn't for the main domain
RewriteCond %{HTTP_HOST} !^domain\.com$
# Check the request is for a static resource
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp|css|ts|js)$
# Redirect to main domain
RewriteRule (.*) http://domain\.com/$1 [R=301,L]
# if the request isn't for index.php,
# (invisibly) redirect to index.php
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.ico|\.bmp|\.css|\.ts|\.js)$
RewriteRule !^index\.php$ /index.php [L]
For information only. As really the credit goes to jon for the answer I just tweaked it for my needs.
If the URLs in your HTML still point to the subdomains, you'll need to setup htaccess redirects on those subdomains, not on the main domain, as the requests will still be going to the subdomains.
For example in /var/www/vhosts/sub.domain.com/httpdocs/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp|css|ts|js)$
RewriteRule (.*) http://domain.com/$1 [R=301,L]
And in /var/www/vhosts/sub2.domain.com/httpdocs/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp|css|ts|js)$
RewriteRule (.*) http://domain.com/$1 [R=301,L]
UPDATE
Based on your comment that Apache handles each subdomain as if it were the main one, try this:
RewriteEngine On
# Check the request isn't for the main domain
RewriteCond %{HTTP_HOST} !(www\.)?domain\.com$
# Check the request is for a static resource
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp|css|ts|js)$
# Redirect to main domain
RewriteRule (.*) http://domain.com/$1 [R=301,L]

Redirect subdomain, but only if site root requested

I can't seem to find an answer to this specific question:
How can I forward http://play.domain.com -- but NOT http://play.domain.com/xxx
To:
http://work.domain.com
Where "xxx" is any string.
In other words, if the user requests the site root, redirect, otherwise stay on the chosen URL.
I've tried
RewriteEngine On
RewriteCond %{HTTP_HOST} !work.domain.com [NC]
RewriteRule ^(.*)$ http://work.domain.com.com/$1 [R=301,L]
But it's not doing anything :-/
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^play\.domain\.com$ [NC]
RewriteRule ^/?$ http://work.domain.com/ [R=301,L]
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} !work.domain.com [NC]
RewriteRule ^$ http://work.domain.com.com/ [R=301,L]
to only redirect the blank request.

Protecting image/video folder in cakephp 2.0

We're currently using folders inside webroot to store images and videos which we're loading in views (using the usual html image helper) that require log in.
How can I prevent outside visitors from just doing a site.com/img/photos/1.jpg url and having access to the images?
From what I understand I can't really use media views to render an image inside a proper view, and I can't figure out if there's a solution through htaccess manipulation.
Which is the best practise for this?
Perhaps choosing to go with a non-webroot folder would be best (although that would make it harder in the file-storing part)?
As poncha suggested, I tried editing the main .htaccess file into this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} !localhost
RewriteCond %{REQUEST_URI} ^app/webroot/img/
RewriteRule .* / [L,F]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
But the rewrite base line seems to be forbidding access to the whole site, and without it there seems to be no change in img access.
Edit 2:
Editing the htaccess inside webroot:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
# this RewriteCond is needed to avoid rewrite loops
RewriteCond %{REQUEST_URI} !^/app/webroot/
RewriteRule (.*) app/webroot/$1 [L,R]
RewriteCond %{HTTP_REFERER} !127.0.0.1
RewriteCond %{REQUEST_URI} ^/app/webroot/img/
RewriteRule .* - [L,F]
</IfModule>
This checks if Referer http header is set to something containing your domain, and denies access to img/ folder if not.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} !site.com
RewriteCond %{REQUEST_URI} ^img/
RewriteRule .* / [L,F]
Note: it is easy enough to "break" this protection if someone wants to steal your content, however, it does prevent hotlinking without the need to produce some sort of script that would pass thorugh all the images/videos to check if access should be granted.
EDIT:
In case your website is not in /, You have two options:
Change RewriteBase to reflect the base uri of the site (eg RewriteBase /app/webroot/)
Change RewriteCond to reflect the path from / (eg RewriteCond ^app/webroot/img/)
The second option is preferred in your case because you have other rules there
EDIT2:
In your case, the whole set should look like this:
RewriteEngine on
RewriteBase /
# this RewriteCond is needed to avoid rewrite loops
RewriteCond %{REQUEST_URI} !^/app/webroot/
RewriteRule (.*) app/webroot/$1 [L,R]
RewriteCond %{HTTP_REFERER} !localhost
RewriteCond %{REQUEST_URI} ^/app/webroot/img/
RewriteRule .* - [L,F]

ISAPI Rewrite rule changing https request to http one, for specific page

What should look isapi rewrite rule which can change https to http for a specific page, ex:
https://www.portal.com/mypage to http://www.portal.com/mypage
Regards
Try using:
RewriteCond %{HTTP:Host} (.*)
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} (/mypage)
RewriteRule .? https://%1%2 [R,L]

Resources