.htaccess not redirecting properly when in domain folder

Viewed 245

needing my domain to redirect

FROM => mydomain.com/Section

TO => mydomain/index.php?controller=Section

HOW I WANT THE USER TO SEE URL=> mydomain.com/Section

THIS IS .htaccess file I Wrote

AddHandler application/x-httpd-php56 .php
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# above code does work at least the https redirect but below wont

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_URI} !(\.$)
RewriteRule /([0-9a-zA-Z-_]+)/? index.php?controller=$1 [NC,QSA,L]
<filesmatch "\.(html|htm|js|css)$"="">
  FileETag None
  <ifmodule mod_headers.c="">
     Header unset ETag
     Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
     Header set Pragma "no-cache"
     Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
  </ifmodule>
</filesmatch>

NOTE: in my file system this works when the .htaccess is in the root (root/.htaccess) but wont work completely when in domain folder (root/mydomain.com/.htaccess), I need it to work in the domain folder if possible, because host keep auto-generating a .htaccess file in the domain folder which messes the .htaccess in the root.

Thanks for any help!

UPDATE: September.6/2018

It seems HostGator read through and updated my .htaccess file and is forcing a directory check, i didn't add this redirect to my cpanel check image below.

enter image description here

AddHandler application/x-httpd-php56 .php 
Options -MultiViews 
RewriteBase / 
RewriteEngine On 
#redirect to https 
RewriteCond %{HTTPS} Off 
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R,L] 
#end of https redirect 

#Pretty url code 
RewriteCond %{HTTP_HOST} ^(www\.)?6thsense.co$ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond $1 !^(index\.php|robots\.txt) 
RewriteCond %{REQUEST_URI} !(\.$) 
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$ 
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$ 
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ 
RewriteRule /([0-9a-zA-Z-_]+)/? /index.php?controller=$1 [NC,QSA,L]
#endof pretty url 

#<filesmatch "\.(html|htm|js|css)$"="">
#  FileETag None
#  <ifmodule mod_headers.c="">
#     Header unset ETag
#     Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
#     Header set Pragma "no-cache"
#     Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
#  </ifmodule>
#</filesmatch>
2 Answers

CORRECT REWRITE

RewriteRule ([0-9a-zA-Z-_]+) /index.php?controller=$1 [NC,QSA,L] 
#endof pretty url

INCORRECT REWRITE

RewriteRule /([0-9a-zA-Z-_]+)/? /index.php?controller=$1 [NC,QSA,L]
#endof pretty url 

You can use:

RewriteRule ([0-9a-zA-Z-_]+?)/?$ /index.php?controller=$1 [NC,QSA,L] 

that works with mydomain.com/Section and with mydomain.com/Section/

Related