简体   繁体   中英

How to redirect URL in .htaccess with condition?

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM