'How to remove index.php from the middle of URL Dreamhost
- I want to remove index.php from the middle of the URL.
- I am using Dreamhost Webhosting.
- And Believe me, Dreamhost Webhosting configuration is very different from others.
- 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]
Solution 1:[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
- I am using Dreamhost Webhosting.
- And Believe me, Dreamhost Webhosting configuration is very different from others.
- Normal Webhosting configuration is not working in Dreamhost.
FWIW, nothing mentioned in your question, or the solution, is specific to any particular web-host.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MrWhite |
