1
  1. I want to remove index.php from the middle of the URL.
  2. I am using Dreamhost Webhosting.
  3. And Believe me, Dreamhost Webhosting configuration is very different from others.
  4. Normal Webhosting configuration is not working in Dreamhost.

Here is the problem I am facing:

PROBLEM 1:

The link below:
https://www.example.com/index.php/gallery

Should redirect to:
https://www.example.com/gallery

PROBLEM 2:

The link below:
https://www.example.com/index.php/profile/juhi/168

Should redirect to:
https://www.example.com/profile/juhi/168

PROBLEM 3:

The link below:
https://www.example.com/index.php

Should redirect to:
https://www.example.com/

htaccess code is below:

RewriteEngine On

# Existing files and directories remain accessible
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.* - [L]

# Redirect the rest
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [QSA,L]
MR JHON
  • 21
  • 4
  • And what is your actual question? That configuration file you posted does not implement any redirection along what you described above. It implements internal rewrites, but in the opposite direction. – arkascha Feb 16 '22 at 07:55
  • Which URLs are you linking to? (_Aside:_ Your example URLs are using path-info, but your internal rewrite is appending a query string - two different URL structures - is that intentional?) – MrWhite Feb 16 '22 at 09:59
  • I just want to remove index.php from the middle of the URL. Since both the URL is opening I want to open them without index.php in between. – MR JHON Feb 16 '22 at 11:59
  • I want a .htaccess code that will redirect the link: https://www.example.com/index.php/gallery to: https://www.example.com/gallery – MR JHON Feb 16 '22 at 12:01

1 Answers1

1
https://www.example.com/index.php/gallery

The part of the URL-path after /index.php (a valid file), ie. /gallery in this example, is called "additional pathname information" (aka path-info for short). (This is used by many CMS to route the URL when mod_rewrite/.htaccess is not available.)

To remove the /index.php part of the URL you could add the following redirect directive immediately after the RewriteEngine directive.

# Redirect URLs of the form "/index.php/<URL>" to "/<URL>"
# Also handles "/index.php" only
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index.php(?:/(.*))?$ /$1 [R=301,L]

Test first with a 302 (temporary) redirect and only change to 301 (permanent) - if that is the intention - once you have confirmed that it works as intended. 301s are cached persistently by the browser so can make testing problematic.


Aside:

RewriteRule ^(.*)$ /index.php?/$1 [QSA,L]

As I mentioned in comments, your .htaccess directives are rewriting the request to a query string (not path-info as mentioned above). This means that your pages are accessible via a 3rd URL. For example:

/index.php?/gallery
/index.php?/profile/juhi/168
  1. I am using Dreamhost Webhosting.
  2. And Believe me, Dreamhost Webhosting configuration is very different from others.
  3. Normal Webhosting configuration is not working in Dreamhost.

FWIW, nothing mentioned in your question, or the solution, is specific to any particular web-host.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • 1
    @MRJHON You're welcome. Please mark the answer as "accepted" (grey/green checkmark below the voting arrows on the right of the answer) to help other readers and remove the question from the unanswered question queue. Once you have 15 rep you can also upvote answers you find helpful. Thanks, much appreciated. :) – MrWhite Feb 17 '22 at 09:39