mod_rewrite Allow only one file and restrict the others - regex

RewriteEngine On
RewriteCond $1 !^(allow\.php)
RewriteRule ^(.*)$ /allow.php/$1 [L,R=301]
RewriteCond %{HTTP_USER_AGENT} !MYUSERAGENT [NC]
RewriteCond %{REMOTE_ADDR} !^12.12\.12\.12$
RewriteRule .* http://www.google.com [R=302,L]
I wrote this code for restricting access to my page. But I want everybody to access allow.php
This code is not working it still redirects.
Why is that happening?

Have your rules like this:
RewriteEngine On
RewriteRule ^sub\.php - [L,NC]
# if not sub.php, not desired user agent and not your IP then redirect
RewriteCond %{HTTP_USER_AGENT} !MYUSERAGENT [NC]
RewriteCond %{REMOTE_ADDR} !^12.12\.12\.12$
RewriteRule ^ http://www.google.com [R=302,L,NC]

You need to remove this part:
RewriteCond %{HTTP_USER_AGENT} !MYUSERAGENT [NC]
RewriteCond %{REMOTE_ADDR} !^12.12\.12\.12$
RewriteRule .* http://www.google.com [R=302,L]
Since the rewrite engine will loop and the rewritten /allow.php/something/something will the hit the second rule and get redirected.

Related

.htaccess RewriteRule keeping URL structure

My current rewrite rule:
RewriteEngine on
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?a=$1&v=$2&id=$3 [L]
The result above works great so I can format the URL like
domain.com/a/b/c
I would like to add in a domain switch as well so the results I want is
sub.domain.com/a/b/c when you access it using domain.com/a/b/c
Currently here is what I have tried
RewriteEngine on
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?a=$1&v=$2&id=$3 [L]
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule (.*)$ http://sub.domain.com/ [R=301,L]
But the result of this is
http://sub.domain.com/a=a&v=b&id=c
and needs to be
http://sub.domain.com/a/b/c
Thanks for the help!!
Reverse the order of your rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule (.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)?$ index.php?a=$1&v=$2&id=$3 [L,QSA]
Make sure to test this after clearing your browser cache.

how to redirrect using .htaccess

I am trying to redirect every request of my site like this:
http://www.example.com/four_digit_number
to
http://www.example.com/index.php?m=four_digit_number
I already have a redirection rule like this:
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^$ [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?sipem.com
RewriteRule ^([^\/]+\.jpg)$ /images/managers/$1 [L]
which in redirects visitors from
www.example.com/four_digit_number.jpg
to
www.example.com/images/managers/four_digit_number.jpg
I need to add this rule to that.
You can use a new rule for this:
RewriteEngine On
RewriteRule ^(\d{4})/?$ /?m=$1 [L,QSA]
RewriteCond %{HTTP_REFERER} ^$ [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?sipem.com
RewriteRule ^([^/]+\.jpg)$ /images/managers/$1 [L]

htaccess redirect to HTTPS except a few urls

I am new to the htaccess redirect stuff but want to do smth special - and I dont know whats the recommend way and dont know if this is still possible or not.
I have this in my .htaccess file:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Now every URL is redirected to the HTTPS version - this is fine and necessery. But now there are a few exceptions.
For example these urls HAS to be HTTP instead of HTTPS:
http://www.mywebsite.com/another/url/which/has/to/be/http
http://www.mywebsite.com/and_again?var=a
Is it possible to solve this with the htaccess and when its possible maybe you can send me a reference link or describe how to do this.
Edit
I now have this code:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+(/commerce_paypal/*)\s [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The goal is that every (!) url gets redirected to HTTPS except ANY url which has commerce_paypal at the beginning.
For example:
mydomain.com/commerce_paypal <- http
mydomain.com/commerce_paypal/smth/else <- http
mydomain.com/what/ever <- https
You can have a RewriteCond to add exceptions in the http->http rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force https:// for all except some selected URLs
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/commerce_paypal/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force http:// for selected URLs
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /commerce_paypal/ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Reference: Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details
RewriteCond %{THE_REQUEST} !/commerce_paypal/ [NC]
worked for me. I tried many similar condition rewrites without luck.

.htaccess - add www and direct all to root level

I would like all requests to be directed to the root page. I also need www to be added if it has been missed out. I have tried numerous attempts but can not seem to get it to work, latest attempt
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [L,R=301]
RewriteRule !^.*/ / [NC,L]
This code below works providing that they do not use www within their requests.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [L,R=301]
You can use this single rule as your first rule in your DOCUMENT_ROOT/.htaccess file::
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{REQUEST_URI} ^/.+
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ http://www.domain.com/? [L,R=301]

how to make mod_rewrite to redirect from subdomains to querystring?

I want to make mod rewrite as the following example :
from google.com.mydomain.com to mydomain.com/index.php?domain=google.com
I use like
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{QUERY_STRING} !domain=
RewriteCond %{HTTP_HOST} ^([^\.]+)\.mydomain.com$ [NC]
RewriteRule ^(.*)$ /index.php?domain=%1 [L,QSA]
but this redirect google.mydomain.com to mydomain.com/index.php?domain=google
I want rule to redirect google.com not google
thanks
It is due to the incorrect regex. Try this rule:
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{QUERY_STRING} !domain=
RewriteCond %{HTTP_HOST} ^(.+?)\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ /index.php?domain=%1 [L,QSA]
In your rule you're using [^\.]+ which matched until a dot is found therefore it is matching google instead of google.com.