2

I am a little afraid to post this, but I'm running into an issue and haven't found a solution yet. I've been able to successfully remove the "index.php" from all my URLs, however I'm still finding that if I manually add "index.php" to any URL, I can still view the page and am not redirected to a URL without "index.php". For example:

http://domain.com/events/

Works just fine, but if I visit http://domain.com/index.php/events/ I can still view the page. Any idea on how to force it to redirect me to just http://domain.com/events/?

Here's my .htaccess file for reference:

# ----------------------------------------------------------------------
# Remove index.php from URLs
# ----------------------------------------------------------------------
<IfModule mod_rewrite.c>
  RewriteEngine On

  # Force trailing slash
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

  # Force www
  RewriteCond %{HTTP_HOST} =domain.com [NC]
  RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

  # Send would-be 404 requests to Craft
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
  RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>

Is this even possible with Craft? Thanks in advance!

2

There's nothing unique or special for doing this with Craft CMS - you should be able to use this method for just about any web app running in a LAMP stack. See this post on removing index.php from the URL with Apache/mod_rewrite/.htaccess.

TLDR: add this bit to the end of your .htaccess file before the closing tag.

If Craft's index.php is at the domain root, such as example.com/index.php, this should do it:

# Redirect to same url without index.php
RewriteCond %{THE_REQUEST} ^GET./index\.php(/.*)?$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.php/admin\b [NC]
RewriteRule ^index\.php(/(.*))?$ /$2 [R=302,NE,L]

If it lives in a subfolder, do this instead:

# Redirect to same url without index.php
RewriteCond %{THE_REQUEST} ^GET./subfolder/index\.php(/.*)?$ [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/index\.php/admin\b [NC]
RewriteRule ^subfolder/index\.php(/(.*))?$ /$2 [R=302,NE,L]

(In both examples, replace admin on the 2nd line with whatever your site’s CP trigger is set to, if you’ve changed it.)

2

After playing around, I found a solution. I hope this helps anyone in the future:

# ------------------------------------------------------------
# Remove index.php
# ------------------------------------------------------------
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

# ----------------------------------------------------------------------
# Send would-be 404 requests to Craft
# ----------------------------------------------------------------------
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) /index.php?p=$1 [QSA,L]

Thanks, Nick for taking the time to help! It got me going in the right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.