how to check a substring in an IF htaccess statement - .htaccess

I'm trying to find a way to filter on a substring in htaccess but cannot find any clues :-(
In this example I need to avoid redirection on the development server:
example.lan
while all the other domains, i.e. example.com will need to be redirected.
<If "%{HTTP_HOST} == 'example.lan'">
#
# LOCAL DEVELOPMENT SERVER
#
</If>
<Else>
#
# PRODUCTION SERVER
#
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</Else>
How do I perform a substring search?
Thank you!
I searched on google but cannot find an answer :-(

Related

Issue with .htaccess rewrite

I have several servers that have different catalytic structure with the same files. I want to redirect all addresses that have at least 1 phrase ".php/" for example: http://localhost/kat1/kat2/kat3/index.php/abc to http://localhost/kat1/kat2/kat3/index.php.
and
http://localhost/kat1/kat2/kat3/kat4/index.php/abc/index.php
to
http://localhost/kat1/kat2/kat3/index.php.
I try that but it does not work:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*).php\/(.*)$ index.php
How can i do that?
You can use this single rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.+?\.php)/ [NC]
RewriteRule ^ /%1 [L,NE,R=301]
# remaining rules go below

RewriteRule for HTTP to HTTPS and WWW ISAPI Rewrite

I've trawled many forums and tried many solutions. None work correctly. I am using ISAPI Rewrite 3 for IIS.
I need to change all requests to our website to WWW and HTTPS.
For example:
https://example.com/a-page-here/
http://example.com/a-page-here/
http://www.example.com/a-page-here/
www.example.com/a-page-here/
example.com/a-page-here/
to all change to:
https://www.example.com/a-page-here/
I've used http://htaccess.madewithlove.be, which may be buggy because I'm getting seemingly incorrect results for so-called working solutions. I don't want to be testing umpteen things on the live site.
This supposedly correct example (one of many) I found gives incorrect results:
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !443
# Extract non-www portion of HTTP_HOST
RewriteCond %{HTTP_HOST} ^(www\.)?(.*) [NC]
# Redirect to HTTPS with www
RewriteRule (.*) https://www.%2/$1 [R=301]
Example tests:
example.com/a-page-here/ = https://www./example.com/a-page-here
www.example.com/a-page-here/ = https://www./www.example.com/a-page-here/
Can anyone give me a set of rules that will cleanly and reliably turn any non www request to our website to the correct https://www version, and not add invalid slashes etc?
Try this one:
RewriteEngine On
# non-www to www
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https\://www.example.com/$1 [R=301]
# HTTP to HTTPS
RewriteCond %{HTTPS} off [NC]
RewriteRule (.*) https\://www.example.com/$1 [R=301]

.htaccess not working properly while setting SSL redirects only on certain pages

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

.htaccess redirect to all IP's but mine

Basically, I am trying to work on the front end of a website, but I would like everyone else but myself to be redirected to a construction page if you like. I currently have:
redirect 301 /index.php http://www.domain.com/construction.php
While this works, it works to well, I would like to be able to still see the live site myself, is it possible to exclude everyone but my IP?
Thanks again.
You could do it with mod_rewrite
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteRule index.php$ /construction.php [R=301,L]
You'll need some conditions before redirecting:
RewriteCond %{REMOTE_ADDR} !=1.3.3.7
RewriteCond %{REQUEST_URI} !=/construction.php
RewriteRule .* /construction.php [L]
Also, to make sure after the lock-out is removed, clients will see the actual page, this solution does not redirect clients permanently (using a 301 redirect), but internally redirects. Substitute 1.3.3.7 for the actual IP address you're using.
If your apache version is 2.4* , You can redirect your visiters to construction page using the following directives in htaccess :
<If "%{REMOTE_ADDR} !='yourIp'">
RedirectMatch ^/((?!construction.php).*)$ /construction.php
</If>
It says if the ip address is not yourIp redirect all requests to /construction.php .
On older versions of apache, you can use the following mod-rewrite based solution :
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^myIP$
RewriteRule !construction\.php /construction.php [L]
This internally forwords the request to /construction.php if the RewriteCondition meets. You can Replace L with R if you want to see the redirected url in browser address bar.
hi there you could do the following in .htaccess file
RewriteEngine on
# Redirect all except allowed IP
RewriteCond %{REMOTE_ADDR} !^12.345\.678\.901$
RewriteRule /index.php http://www.domain.com/construction.php [R=302,L]
putting your IP instead of 12.345.678.901
If you have a range of IPs you want to exclude from seeing 'under construction' page you can use |
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^127.0.0.1|212.250.141.228
RewriteRule ! construction\.html /construction.html [R]
It is important to put the 2 last lines at the end of your .htaccess file, especially when it contains more rewriting rules.
The following worked for me
Deny from all
Allow from xxx.xxx.xx.xxx
If you are interested on having a background image referenced on your construction.php, the code below avoids the image to be redirected:
RewriteCond %{REMOTE_ADDR} !=THE_IP
RewriteCond %{REQUEST_URI} !^\/construction\.php|\/YOUR_IMAGE\.jpg
RewriteRule .* /construction.php [R=302,L]
In addition to using the if directive as other answers suggested, you can also add multiple IPs by including other conditions into one directive using the && operator as such:
<If "%{REMOTE_ADDR} != '127.0.0.1' && %{REMOTE_ADDR} != '192.168.1.1'">
RedirectMatch ^/((?!construction.php).*)$ /construction.php
</If>
See the docs here: http://httpd.apache.org/docs/2.4/mod/core.html#if
Another idea is to give access only to a certain range
RewriteEngine on
RewriteBase /
# Validator
SetEnvIf Remote_Addr "^128.30." IsInt
# Local
SetEnvIf Remote_Addr "^192\.168" IsInt
Order allow,deny
Allow from env=IsInt
Not any one worked until I find my own solution
URL in code: http://www.example.com/index_cons.php
IP address in example is: 75.85.95.105
Tested on lastest version of Cpanel.
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^75\.85\.95\.105
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "http\:\/\/example\.com\/index_cons\.php" [R=302,L]

HTACCESS: *.subdomain.domain.tld redir to subdomain.domain.tld/*

sorry for the vague question title.
But anyways,
I have here a subdomain which i wish to pass on wildcard sub-subdomains and make a proper htaccess redirect to a sub-folder (relative to the server root) equivalent to the wildcard value such that
*.subdomain.domain.tld will redirect to subdomain.domain.tld/*
where * = wildcard value
I hope you get my question. Can someone shed some light on this? I would appreciate it very much =)
RewriteRule ^(.*).subdomain.domain.tld$ http://subdomain.domain.tld/$1 [R]
That should do the trick.
The [R] tells the server that this is a redirect.
You can also use optional codes, say
[R=301] # Moved permanently
[R=302] # Moved temporarily
[R=403] # Forbidden
[R=404] # Not Found
[R=410] # Gone
You have to read the subdomain's name with RewriteCond and then you can do the Redirect with RewriteRule, while the subdomain is saved in %1.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.subdomain\.domain\.tld$ [NC]
RewriteRule .* http://subdomain.domain.tld/%1 [L,R=301]
If you want to redirect *.subdomain.domain.tld/file.html to subdomain.domain.tld/*/file.html, you can replace the RewriteRule with this:
RewriteRule ^(.*)$ http://subdomain.domain.tld/%1/$1 [L,R=301]

Resources