Prevent redirect coming from external domains - apache

I need to prevent that an external domain make a redirect to my domain. I tried using the following rules:
RewriteCond %{HTTP_REFERER} ^http://www.extenaldomain.com [NC]
RewriteRule ^$ /my_error_page.htm [R,L]
but it doesn't work. I tried also:
RewriteCond %{HTTP_FORWARDED} ^http://www.extenaldomain.com [NC]
RewriteRule ^$ /my_error_page.htm [R,L]
but it doesn't work too. any idea?

Why don't you simply precise the domain name without adding extra stuff?
Try this, it should work:
RewriteCond %{HTTP_REFERER} extenaldomain\.com [NC]
RewriteRule (.*) /my_error_page.htm [R,L]
Tell me if it does

SOLVED: it works:
RewriteCond %{HTTP_REFERER} extenaldomain\.com [NC]
RewriteCond %{REQUEST_URI} !^/my_error_page.htm
RewriteRule (.*) /my_error_page.htm [R,L]

Related

Redirecting to https for any URI

In my .htaccess file, I have the below.
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301]
This works fine until someone goes to http://www.example.com/test because that does not force a redirect to https.
Is there a better way of doing this?
Yes, change your last two lines to:
RewriteCond %{HTTP_HOST} !^(www\.example\.com|)$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

htaccess condition and rewrite

i have this link
http://localhost/fukhtaccess
and i want redirect the link to http://server.localhost/index.php with GET params.
this is my htaccess, but don't work and not possibile test with some error for debug (thanks apache foundation!!!)
RewriteEngine On
RewriteCond %{HTT_HOST} ^localhost$
RewriteCond %{REQUEST_URI} fukhtaccess
RewriteRule (.*) http://server.localhost/index.php [QSA,L]
You have a typo: change HTT_HOST to HTTP_HOST.
Also, your second RewriteCond just complicates things unnecessarily. Do this instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule fukhtaccess http://server.localhost/index.php [QSA,L]
better, this work fine
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost [NC]
RewriteCond %{REQUEST_URI} fukhtaccess$ [NC]
RewriteRule (.*) http://server.localhost/index.php [QSA,L]

RewriteCond www and non www to subpage

I'm a newbie and the question is simple, I want mysite.eu and www.mysite.eu redirect to a subpage of their website,
but I can't get it to work for both because when I add something like this I got stuck in a loop so how can I fix this that both wil direct to that subpage without getting a loop.
My example:
RewriteCond %{HTTP_HOST} www\.mysite\.eu [NC]
RewriteRule . http://www.mysite.eu/page?page=webshop_fixol&lng=1 [L]
RewriteCond %{HTTP_HOST} mysite\.eu [NC]
RewriteRule . http://www.mysite.eu/page?page=webshop_fixol&lng=1 [R=permanent,L]
Why do you have the . there? sure you did not just want this:
RewriteCond %{HTTP_HOST} www\.mysite\.eu [NC]
RewriteRule ^/$ http://www.mysite.eu/page?page=webshop_fixol&lng=1 [L]
RewriteCond %{HTTP_HOST} mysite\.eu [NC]
RewriteRule ^/$ http://www.mysite.eu/page?page=webshop_fixol&lng=1 [R=permanent,L]
It can be done in a single rewrite rule like this:
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.eu$ [NC]
RewriteRule . page?page=webshop_fixol&lng=1 [L,QSA]
^(www\.)?mysite\.eu$ will match both sites www and non www.

Help with mod_rewrite rule

Need a little help getting the following rule working:
RewriteCond %{HTTP_HOST} ^asset.*.domainone\.com [NC,AND]
RewriteCond %{REQUEST_URI} !^/FILE/.* [NC]
RewriteRule ^(.*)$ "http://domaintwo\.com/$1" [L]
For requests coming to this server with a domain of 'asset(anything).domain.com' which does not start with 'FILE', redirect to another server.
Thanks.
RewriteCond %{HTTP_HOST} ^asset.*?\.domainone\.com [NC]
RewriteCond %{REQUEST_URI} !^/FILE/.* [NC]
RewriteRule ^/(.*)$ http://domaintwo.com/$1 [R,L]

mod_rewrite rules, how to test for 'not a string'?

I have a blog installed in www.foo.com/wp/ and would like all requests that go to any page that doesn't start with /wp/ to be redirected to www.foo.com/wp/ (without preservation of the URL).
E.g.:
www.foo.com/bar/baz/hello.gif > www.foo.com/wp/
Also, at the same time, I need the URL to have www. added if it doesn't (which might be where my rules are getting confused)
At the moment, I have:
RewriteCond %{HTTP_HOST} !^www.foo.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.foo.com/$1 [R=permanent]
RewriteRule ^(?!wp)/(.*)$ http://%{HTTP_HOST}/wp/($2) [R=permanent]
But I think this is currently completely broken.
Help much appreciated.
RewriteCond %{HTTP_HOST} !^www.foo.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule (.*) http://www.foo.com/$1 [R=permanent,L]
RewriteRule ^(?!wp/)(.*) http://%{HTTP_HOST}/wp/$1 [R=permanent]
The path portion of the URL being matched never has a leading slash, and you don't need to anchor a .*.
L modified prevents rewriting from continuing after you arrive at your first redirect.
Negative-lookbehind (?!) is not a capturing construct, and you substitute in captured patterns by writing $1, not ($1).
Edit: Apparently for OP the last rule doesn't work, failing to exclude the cases that begin with /wp/, which makes no sense to me but whatever. Here's an attempt at a workaround:
RewriteCond %{HTTP_HOST} !^www.foo.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule (.*) http://www.foo.com/$1 [R=permanent,L]
RewriteCond %{REQUEST_URI} !^/wp/
RewriteRule (.*) http://%{HTTP_HOST}/wp/$1 [R=permanent]
Try these rules:
RewriteCond %{HTTP_HOST} !^www.exmaple.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /wp/
RewriteRule ^wp/(.*) /$1 [R=301,L]
RewriteRule !^wp/ wp%{REQUEST_URI} [L]
The first rule is for the host name. The second is to remove the /wp path prefix externally. And the third is to add the prefix again for internal redirection.