How to redirect URL in .htaccess with condition? - apache

I have domain with url https://reg.bmi.id. I'd like to make any user that type anything after the url is redirected to https://reg.bmi.id.
Example:
https://reg.bmi.id/admin will be redirected in to https://reg.bmi.id
https://reg.bmi.id/asidjadhqowidhqohuqw will be redirected in to https://reg.bmi.id
https://reg.bmi.id/asdjqoq/qdoqwun/qowidopq will be redirected in to https://reg.bmi.id
https://reg.bmi.id/contact/contact.php will be redirected in to https://reg.bmi.id
In exception, if the user precisely type https://reg.bmi.id/reg_pilot then it will not be redirected. It open the page of /reg_pilot
This is my current .htaccess which is located in root folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^reg\.bmi\.id$ [OR]
RewriteCond %{HTTP_HOST} ^www\.reg\.bmi\.id$
RewriteRule ^/?$ "https\:\/\/reg\.bmi\.id\/" [R=301,L]
reg/pilot is a reference to any other rsources on this domain. /contact/contact.php is not exist. Anything typed except /reg_pilot after the domain is should be redirected
I have little to no experience dealing about .htaccess any help is greatly appreciated

With your shown samples, could you please try following.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
##Setting rewrite base here.
RewriteBase /
##Checking for non https requests and applying https to it with/without www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?reg\.bmi\.id$ [NC]
RewriteRule ^/?$ https://reg.bmi.id/ [R=301,L]
##Stopping/forbidding direct access of index.php here.
RewriteRule ^index\.php$ - [L,NC]
##Any non existing directory/file is served by php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !reg_pilot [NC]
RewriteRule ^ https://reg.bmi.id/ [R=301]
RewriteRule ^ index.php [NC,L]

Related

How do I edit apache .htaccess correctly?

Good day! There are 3 tasks that I want to implement using the .htaccess file:
Work only with https protocol
Create a 301 redirect from https://www.example.com to https://example.com
Redirect all requests to the index.php file
My best version of .htaccess looks like this, but I'm not sure if this is the right decision:
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} .* [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php
I would be grateful for any advice!
Could you please try following, written as per your shown samples. Please make sure you clear your browser cache before you test your URLs.
RewriteEngine ON
##For applying https on each request.
RewriteCond https !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
##For removing www from request.
RewriteCond %{HTTP_HOST} ^(?:www\.)(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,NE,L]
##For applying index.php for non existing files or directories.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

htaccess remove www redirect to ssl but wrong url

I created an mvc framework with php. my only working main file is index.php.
Normally I redirect from http to https. I also remove the www domain, but when I enter with www, if my URL is for example
domain.com/admin/products/edit/1,
htaccess does this.
domain.com/index.php/products/edit/1
how can i solve this
Thank you
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
You should have your rules like this way, always keep https redirection rule at very first place only. Please clear your browser cache before testing your URLs(rest of the rules are from OP's tried rules + I have changed regex of matching hostname too here).
RewriteEngine ON
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^(?:www\.)(example\.com) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^ index.php [L]

Problem with URL path and .htacces redirect

I have the following code in my .htaccess:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
which redirects all traffic to HTTPS and non-www, perfectly. However if a user happens to visit a page on the website and includes www it doesn't keep the full path but redirects to the home page. For example:
www.example.com goes to example.com
but
www.example.com/page goes to example.com instead of example.com/page
this happens regardless of using HTTPS or HTTP. It's something to do with the www redirect that's causing the problem (?)... I just can't figure it out and I've googled many answers to no avail.
UPDATE
My full .htacces is:
RewriteEngine on
RewriteBase /
# block text files in the content folder from being accessed directly
RewriteRule ^content/(.*)\.(txt|md|mdown)$ index.php [L]
# block all files in the site folder from being accessed directly
# except for requests to plugin assets files
RewriteRule ^assets/plugins/([a-zA-Z0-9\.\-_%=]+)/(.*)$ site/plugins/$1/assets/$2 [L,N]
RewriteCond $1 !^plugins/[a-zA-Z0-9\.\-_%=]+/assets/.*
RewriteRule ^site/(.*) index.php [L]
# block direct access to kirby and the panel sources
RewriteRule ^(kirby|panel\/app|panel\/tests)/(.*) index.php [L]
# make panel links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^panel/(.*) panel/index.php [L]
# make site links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]
# HTTP to HTTPS and WWW to non WWW
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
Could you please try following, based on your shown samples. Please keep these rules above all of your rules and make sure you clear your browser cache before testing these.
RewriteEngine ON
##For www requests changing to https.
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
##For non-www requests changing to https.
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

Non HTTPS URL adds extra WWW infront

If you visit Non HTTPS version it will not redirect to the HTTPS version. Instead it will look like https://www.www.mywebsite.com and will not work. This is the code in my htaccess file.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://www.%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
Remove www. from this line:
RewriteRule .* https://www.%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Or you can do it like this :
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R=301,L]

Apache Rewrite : not matching pattern not working

I want to redirect visitors to my main domain when they perform requests on my subdomain followed by a not matching URI.
For example, a visitor can access a resource at sub.domain.com/product/10 but he should be redirected to domain.com when he attempt to access other resources that not match product/:id on my subdomain like sub.domain.com/anOtherResource.
I have to do this with apache rewrite module. I found that ! operator can do the job but it's not working for me.
Here is my Rewrite configuration from .htaccess file :
RewriteCond %{HTTP_HOST} sub\.domain\.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com [L,R]
I also tested this configuration :
RewriteCond %{HTTP_HOST} sub\.domain\.com
RewriteRule !^/product/[0-9]+$ http://www.domain.com [L,R]
I don't know where is the mistake ...
[Edit]
The .htaccess file is configured for Wordpress. Here is the entire .htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The problem was in the Wordpress Rewrite configuration so what's happened ?
Step 1
I perform a request at sub.domain.com/product/1 so it not match these RewriteCond :
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
Then, it not redirect to www.domain.com
Step 2
It continue the rewriting to the next Cond (Worpdress rewriting):
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
At this step, it send an Internal Redirect (not sent to the client) to sub.domain.com/index.php
Step 3
Because of the redirection, it apply again the previous RewriteCond
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R]
It match the Cond, then it redirect the client to www.domain.com while I didn't expect it.
The Solution
I fixed the problem by adding a RewriteCond on index.php as showing bellow :
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R]
Now it manage the Internal Redirection sent by the Worpress Rewriting.
The rewrite rules look ok, but try the following:
RewriteCond %{HTTP_HOST} ^sub[.]domain[.]com$
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com [L,R]
If this doesn't work, then you might need to copy and paste the entire contents of your VirtualHost configuration since it could be something else causing a problem.