htaccess RewriteCond with HTTP_HOST and specific REMOTE_ADDR - .htaccess

I have this code in my .htaccess file in my webpage's root dir on my local test server:
RewriteCond %{HTTP_HOST} ^localhost:8081 [NC]
RewriteCond %{REQUEST_URI} /php/([a-zA-Z0-9\-]+).php [NC]
RewriteRule ^ index.php?s=%1 [QSA,L]
It translates any URL of myserver.com/php/something.php to myserver.com/?s=something very fine as expected, but now I need I could test it also via my smartphone which IP is static of 192.168.1.11 which I am not able to do.
I tried this but it does not work:
RewriteCond %{HTTP_HOST} ^localhost:8081 [NC]
RewriteCond %{REMOTE_ADDR} ^192\.168\.1\.11 [NC]
RewriteCond %{REQUEST_URI} /php/([a-zA-Z0-9\-]+).php [NC]
RewriteRule ^ index.php?s=%1 [QSA,L]
I need that both my local PC that is running the test server could open my page but also my smartphone so I would be able testing/seeing responsive designs.

Related

How to redirect one visitor IP address to a specified page on my website

I'm trying to use .htaccess to redirect a visitor with a specified IP address to always be redirected to a specified page when visiting any page on my website. I've tried the code below but it is causing an error. I'm using a wordpress site.
RewriteCond %{REQUEST_URI} !/specified-page/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC]
RewriteCond %{REMOTE_HOST} !^1\.2\.3\.4
RewriteRule $ /specified-page/ [R=302,L]
It is causing "ERR_TOO_MANY_REDIRECTS" message.
As a side-note, my domain is pointing to a sub-folder in my webspace. As are some other websites. This is all directed from another .htaccess file in the root of my webspace with code like this.
RewriteCond %{HTTP_HOST} ^([^.]+\.)?mydomain1\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mywebsitefolder1/
RewriteRule (.*) /mywebsitefolder1/$1 [L]
RewriteCond %{HTTP_HOST} ^([^.]+\.)?mydomain2\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mywebsitefolder2/
RewriteRule (.*) /mywebsitefolder2/$1 [L]
and so on...
(The .htaccess I'm trying to edit to create the IP redirect is in 'mywebsitefolder1' folder.)
Try it like that:
RewriteCond %{REQUEST_URI} !/specified-page/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC]
RewriteCond %{REMOTE_HOST} !^1\.2\.3\.4
RewriteRule ^ /specified-page/? [R=302,L]
This will also remove an optional query string that could cause the redirect loop.
You have indeed a looping condition. Let's try it like this:
RewriteCond %{REMOTE_HOST} ^1\.2\.3\.4
RewriteCond %{REQUEST_URI} !/specified-page/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC]
RewriteRule ^ /mywebsitefolder1/specified-page/ [R=302,L]
RewriteCond %{HTTP_HOST} ^([^.]+\.)?mydomain1\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mywebsitefolder1/
RewriteRule (.*) /mywebsitefolder1/$1 [L]
RewriteCond %{HTTP_HOST} ^([^.]+\.)?mydomain2\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mywebsitefolder2/
RewriteRule (.*) /mywebsitefolder2/$1 [L]

.htaccess file not redirecting properly in Codeigniter app

I have an app that works on my local machine using MAMP, but fails to redirect to a certain page on the live server. I have narrowed this down to the .htaccess file but can't seem to get the exact configuration. It happens when I sign up a user on my Codeigniter-based app and it's supposed to redirect to another page, but doesn't.
This is what I have currently,
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://website.com/$1 [R,L]
# Hide all PHP files so none can be accessed by HTTP
RewriteRule (.*)\.php$ index.php?/$1
# If the file/dir is NOT real go to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]
Try to used like this

htaccess redirect to https://www and to root page of site

i am trying to amend my ht access files to redirect access to the site via the https site (rather than the old site)
the first paragraph of ht code below is the part of the access code that only permits access via the root file page.this part works perfeclty.
the second part however is the part that is suppose to re-direct everything to the HTTPS site and prevent access to the http site.This is not working and its still possible to gain access to the site via the http.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
i would really apprciate advise on how to amend the code.
Try and remove the L from the rewrite rule. Once the rule is met it will stop processing so it will always stop.
Change this
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
To
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC]
See how that works for you.
Edit:
If you want to just direct all traffic and non https to https and the index page the code below does the same thing. Notice I used the domain name. Replace yoursite.com with your real site. See how that works.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*)$ https://www.yoursite.com/ [R=301,L]
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !/index.php
RewriteRule ^(.*)$ / [NC,R=301,L]

How do I redirect subdomain to subdirectory (with default folder) on htaccess without change address

I'm trying to do a Rewrite subdomains to subdirectories on htaccess.
Searching here I did the rule below:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com\.br
RewriteCond %{REQUEST_URI} !^/sd_
RewriteRule ^(.*)$ http://domain.com.br/sd_%1/$1 [P,L,NC,QSA]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.*)\.domain\.com\.br
RewriteCond %{REQUEST_URI} !^/sd_
RewriteRule ^(.*)$ http://domain.com.br/sd_%1/$1 [P,L,NC,QSA]
#Rewrite for my Framework
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
When I access the address: http://user.domain.com.br or the address: http://www.user.domain.com.br they're redirected without change the URL address to: http://www.domain.com.br/sd_user/
Well, fine. My problem is:
When someone access a adrees with haven't a folder, .htaccess redirect it to a specific folder. For example:
I haven't a folder called "sd_joecompany", then I want to redirect it to a default folder named "sd_system". If I access http://joecompany.domain.com.br/ and not exists the respective folder, this address may point to "sd_system" keeping the address.
With this code, when I access a subdomain without a respective folder, the following error appear:
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /index.php.
Reason: Max-Forwards has reached zero - proxy loop?
I'm not sure about the clarity of my doubt, and I'm sorry for my poor english.
I know a little about .htaccess and these rules. Everithing on the code above, I gathered here!
Thank you since now!
If they are on the same server and have the same document root, then you really don't want to proxy at all. Remove the host part of the rule and get rid of the "P":
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/sd_
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com\.br$ [NC]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /sd_%1/$1 [L,NC,QSA]
RewriteCond %{REQUEST_URI} !^/sd_
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.com\.br$ [NC]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /sd_%1/$1 [L,NC,QSA]
#Rewrite for my Framework
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
Cleaned up some of the regex and got rid of the extra RewriteEngine On's.

Dynamic subdomain with htaccess (not redirect)

Currently, I'm having an url system like this by using the htaccess:
- www.domain.com/member/username
- www.domain.com/member/username/contact
- www.domain.com/member/user/album/title-of-the-album/albumID
.. something like that
Here is my htaccess, and it worked perfectly.
RewriteRule ^member/([a-zA-Z0-9_-]+)$ member.php?username=$1
RewriteRule ^member/([a-zA-Z0-9_-]+)/contact$ contact.php?username=$1
RewriteRule ^member/([a-zA-Z0-9_-]+)/album/([a-zA-Z0-9_-]+)/([0-9]+)$ album.php?username=$1&title=$2&album_id=$3
Now I want to setup a dynamic subdomain system for the user, likes "username.domain.com"
So I decided to used the below htaccess:
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^(.*)$ www.domain.com/member/%1 [L]
But this redirect the user to their old domain "www.domain.com/member/username" instead of "www.domain.com/member/username"
I want to keep the user stay in "username.domain.com" (no url change in the address bar).
If possible, is there any chance to keep the same structure for other new url, such as when I type:
"username.domain.com/contact" will load the content of "www.domain.com/member/username/contact" (no url change in the address bar)
"username.domain.com/album/title-of-the-album/albumID" will load the content of "www.domain.com/member/username/album/title-of-the-album/albumID" (no url change in the address bar)
Please help !!
Try:
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^/?$ /member.php?username=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^/?contact$ /contact.php?username=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^/?album/([a-zA-Z0-9_-]+)/([0-9]+)$ /album.php?username=%1&title=$1&album_id=$2 [L]

Resources