htaccess redirect specific IP - apache

I want to redirect some IPs to a error.php . Ofc I don't want redirects from error.php to itself, so I excluded that. Bottom line, i don't figure it out where i'm wrong.
In this one i'm redirecting to google. But still failing.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} ^xxx\.xxx\.xxx\.xxx [OR]
RewriteCond %{REMOTE_HOST} ^yyy\.yyy\.yyy\.yyy
RewriteCond %{SCRIPT_FILENAME} !\/error\.php [NC]
RewriteRule .* http://google.com [R=302,L]
</IfModule>

Have you tried just using a negative lookahead?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} ^xxx\.xxx\.xxx\.xxx [OR]
RewriteCond %{REMOTE_HOST} ^yyy\.yyy\.yyy\.yyy
RewriteRule ^(?!error\.php) http://google.com [R=302,L]
</IfModule>

Related

mod_rewrite subdomain (WITH a path) to URL

Struggling with this and have already done about 30+ min of Googling.
I would like to redirect:
sub.domain.com/path1 to https://anotherdomain.com/path3
sub.domain.com/path2 to https://anotherdomain.com/path4
This code below does not work but demos my approach/thinking so far
#CLIENT 1
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mockups.domain.com$
RewriteCond %{REQUEST_URI} ^/client1/
RewriteRule (.*) https://xd.adobe.com/view/xxxxxxxxxxxx/ [L,R]
</IfModule>
# CLIENT 2
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mockups.domain.com$
RewriteCond %{REQUEST_URI} ^/client2/
RewriteRule (.*) https://xd.adobe.com/view/xxxxxxxxxxxx/ [L,R]
</IfModule>
Can anyone sort me out on this?
With your shown samples only, could you please try following.
Make sure you keep these rules on TOP of your .htaccess file and keep other rules below these.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mockups\.domain\.com$ [NC]
RewriteRule ^client1 https://xd.adobe.com/view/your_new_path_here [L,R=302,NE]
RewriteCond %{HTTP_HOST} ^mockups\.domain\.com$ [NC]
RewriteRule ^client2 https://xd.adobe.com/view/your_2nd_path_here [L,R=302,NE]
</IfModule>
Also please do clear your browser cache before testing your URLs.

Mod_rewrite working on http but not https

I have a mod_rewrite that works fine on http, but not on https.
Some answers on stack overflow to similar questions suggest adding RewriteBase /, but I already have that.
I'm hosted on godaddy on a shared server. I have contacted godaddy 3 times and they say they can't do anything.
Here is an example of the rewrite:
URL Without Rewrite:
https://www.funded.today/stats/?p=bloodstained-ritual-of-the-night
Mod_Rewrite URL:
https://www.funded.today/stats/iga/bloodstained-ritual-of-the-night
My htaccess file is the following:
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?funded\.today$ [NC]
RewriteRule ^$ https://www.funded.today/%{REQUEST_URI} [R,L]
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^stats/(.*)/(.*)$ /stats/?p=$2 [P,QSA]
# BEGIN s2Member GZIP exclusions
<IfModule rewrite_module>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} (^|\?|&)s2member_file_download\=.+ [OR]
RewriteCond %{QUERY_STRING} (^|\?|&)no-gzip\=1
RewriteRule .* - [E=no-gzip:1]
</IfModule>
# END s2Member GZIP exclusions
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^stats/(.*)/(.*)$ - [L,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Any help or ideas on this is greatly appreciated!

htaccess subdirectory + 301 on www

I'd like to do 2 things with htaccess but I can't figure out how to do it.
Let's say my domain is domain.com
First, I'd like to force www. in the url with a 301 redirect.
Another thing, my website is not hosted in the root directory but in /laravel/public/
So I'd like to set this subdirectory as root, and remove it from the URL if someone try www.domain.com/laravel/public/ => www.domain.com
How can I do that?
Thanks in advance.
Assuming that you can't change the document root to point to your public folder, you can try adding these rules to document root (not your public folder):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{THE_REQUEST} \ /+lavarel/public/([^\?\ ]*)
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/laravel/public/
RewriteRule ^(.*)$ /laravel/public/$1 [L]
You'll need to have your own .htaccess file in the root of your domain, along with another in the laravel/public directory (there already is one there, but you need to modify it.
Below are the two files in full as I have them on my testing server.
/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Rewrite requests to the laravel/public index
RewriteCond %{REQUEST_URI} !^/laravel/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /laravel/public/$1 [L]
</IfModule>
/laravel/public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Force www. (also strips laravel/public)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Strip laravel/public, prevent loops
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_URI} ^/laravel/public(/.+)/? [NC]
RewriteRule ^ %1 [R=301,L]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Forcing https:// through htaccess

I'm very new to coding in general so please forgive me for my ignorance in the subject. I understand that their is a few questions similar to this, but none have seemed to work for me. Useful information is always welcome please due you're best to explain everything to me even if it seems a bit excessive because I'm fairly uneducated in theses particular subjects.
Here is my entire htaccess file located in my root forum directory.
RewriteOptions inherit
RewriteEngine on
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /forums/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpeg|jpg|gif|png)$ /forums/public/404.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /forums/index.php [L]
</IfModule>
<Files 403.shtml>
order allow,deny
allow from all
</Files>
RewriteCond %{HTTP_HOST} ^perspectiverp\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.perspectiverp\.com$
RewriteRule ^home$ "https\:\/\/www\.perspectiverp\.com\/forums\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^perspectiverp\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.perspectiverp\.com$
RewriteRule ^Home$ "https\:\/\/www\.perspectiverp\.com\/forums\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^perspectiverp\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.perspectiverp\.com$
RewriteRule ^/?$ "https\:\/\/www\.perspectiverp\.com\/forums\/" [R=301,L]
My site will always connect to the website through https:// unless it is being directly told to do otherwise e.g. http://perspectiverp.com/forums/. My issue is that as my general connection is being directed through https:// users can still connect through http:// which is a major security flaw. I basically need it to be similar to how google has their system to automatically redirect http:// to https:// even if the user attempts to change it in the web address
e.g. http:/ww.google.com/ gets redirected to https:/ww.google.com/?gws_rd=ssl if the user attempts to change it to*http:/ww.google.com/.
As for anyone wanting to know what the forum software is, I'm currently using a Licensed version of Invision Powers Community Suit.
// UPDATED
First attempt Result was "Webpage has Redirect Loop"
RewriteOptions inherit
RewriteEngine on
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /forums/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpeg|jpg|gif|png)$ /forums/public/404.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /forums/index.php [L]
</IfModule>
<Files 403.shtml>
order allow,deny
allow from all
</Files>
RewriteCond %{HTTPS} =off
RewriteRule ^ https://www.perspectiverp.com%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?perspectiverp\.com$ [NC]
RewriteRule ^(home)?$ https://www.perspectiverp.com/forums/ [R=301,NC,L]
Second attempt Result was "Webpage has Redirect Loop"
RewriteOptions inherit
RewriteEngine on
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /forums/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpeg|jpg|gif|png)$ /forums/public/404.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /forums/index.php [L]
</IfModule>
<Files 403.shtml>
order allow,deny
allow from all
</Files>
RewriteCond %{HTTPS} =off
RewriteRule ^ https://www.perspectiverp.com%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?perspectiverp\.com$ [NC]
RewriteRule ^home$ https://www.perspectiverp.com/forums/ [R=301,NC,L
Third attempt Result was "Webpage has Redirect Loop"
RewriteOptions inherit
RewriteEngine on
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /forums/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpeg|jpg|gif|png)$ /forums/public/404.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /forums/index.php [L]
</IfModule>
<Files 403.shtml>
order allow,deny
allow from all
</Files>
RewriteCond %{HTTPS} =off
RewriteRule ^ https://www.perspectiverp.com%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^perspectiverp\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.perspectiverp\.com$
RewriteRule ^home$ "https\:\/\/www\.perspectiverp\.com\/forums\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^perspectiverp\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.perspectiverp\.com$
RewriteRule ^Home$ "https\:\/\/www\.perspectiverp\.com\/forums\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^perspectiverp\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.perspectiverp\.com$
Add the following just before your last three https:// rewrite rules.
RewriteCond %{HTTPS} =off
RewriteRule ^ https://www.perspectiverp.com%{REQUEST_URI} [R,L]
Once everything works as expected change R to R=301 above. Also, notice how the URL in my rewrite rule does not quote and escape any forward slashes as it isn't required.
You can also replace your last three rules with just this one:
RewriteCond %{HTTP_HOST} ^(www\.)?perspectiverp\.com$ [NC]
RewriteRule ^(home)?$ https://www.perspectiverp.com/forums/ [R=301,NC,L]
The NC flag makes the rule case-insensitive so home and Home both will match. The ? makes the preceding group () optional; so the RewriteCond works with both www and without it, and the RewriteRule matches on / as well.
I forced https:// through a reverse proxy server (Cloudflare) and was able to force my website to accept https:// traffic only.

How to redirect specific links to https:// everything else to http:// using .htaccess?

I am trying to use htaccess to automatically direct all requests to certain pages to https and everything else to http://
Here is the code I've got the code to force ssl working below
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-homebuyers-guide https://domain.com/va-homebuyers-guide/ [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-purchase-request https://domain.com/va-purchase-request/ [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-streamline-refinance https://domain.com/va-streamline-refinance/ [R=301,L]
This is working as expected and redirects to the ssl versions of those pages. I then tried adding the following to redirect everything else to http:
RewriteCond %{SERVER_PORT} !^80
RewriteCond %{REQUEST_URI} !^va-homebuyers-guide$
RewriteCond %{REQUEST_URI} !^va-purchase-request$
RewriteCond %{REQUEST_URI} !^va-streamline-refinance$
RewriteRule ^(.*)$ http://domain.com/$1 [R,L]
This code results in a redirect loop when going to /va-purchase-request /va-homebuyers-guide and /va-streamline-refinance and does not redirect the other pages at all.
I'm totally stuck with this and any help would be massively appreciated!
Edit:
I also have this code in the .htaccess file added by wordpress... Could it be interfering with the other redirects?
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Replace your .htaccess code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^va-(homebuyers-guide|purchase-request|streamline-refinance)(/.*|)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]
RewriteCond %{HTTPS} on
RewriteRule (?!^va-(homebuyers-guide|purchase-request|streamline-refinance)(/.*|)$)^.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]