Search code examples
.htaccesshttp-redirect

.htaccess issue with url


This url format https://portal.com/pages does not work but this https://portal.com/index.php/pages works.

I want to be able to remove the index.php from the url.

My root .htaccess file is

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTPS} off
  # First rewrite to HTTPS:
  # Don't put www. here. If it is already there it will be included, if not
  # the subsequent rule will catch it.
  # RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  # RewriteEngine On
  RewriteRule ^$ public/ [L]
  RewriteRule (.*) public/$1 [L]
  
</IfModule>

While public folder .htaccess file is

<IfModule mod_rewrite.c>
  Options -Multiviews
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>

Solution

  • Try following rules htaccess file. Make sure your index.php is present inside public folder. Please make sure to clear browser cache before testing your URLs.

    root htaccess rules file:

    <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{HTTPS} off
      RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
      RewriteCond %{REQUEST_URI} !^/public [NC]
      RewriteRule ^(.*)/?$ public/$1 [L]
    </IfModule>
    

    Public folder inside htaccess Rules file:

    <IfModule mod_rewrite.c>
      RewriteOptions InheritBefore
      Options -Multiviews
      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(.+)/?$ index.php?url=$1 [QSA,L]
    </IfModule>
    

    Fixes done in OP's attempt:

    • root htaccess Rules file didn't have https apply rule, so have added it.
    • Made public applying rule for all kind of urls.
    • Then in public folder rules file added InheritBefore option to make sure root's file is being inherited to public folder's htaccess rules file.