1
RewriteCond %{HTTP_HOST} !^\.site\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

I added this to my .htaccess file to remove the trailing slash and it has caused a "Too Many Redirects" browser error when trying to access the control panel at site.com/system or site.com/system/. I think EE is trying to add a url back in on refresh and then the htaccess is trying to remove it again. Any ideas how to fix this?

The CP is accessible through site.com/admin.php so my client isn't completely locked out.

EE 2.5.2

Update with full .htaccess file

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php/$1 [L] 
RewriteCond %{HTTP_HOST} !^\.site\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

ErrorDocument 404 /error/404

<FilesMatch "(\.jpe?g|gif|png|bmp|css|js|flv|pdf)$">
ErrorDocument 404 "File Not Found"
</FilesMatch>

1 Answer 1

2

Updated:

The issue caused by the trailing slash redirect happening after your EE index.php rewrite. You need to have this redirect happen before rewriting to index.php. (Note: almost always, you want all redirects to happen before the rewrite to index.php.)

So, the simplest way to correctly remove the trailing slash, that shouldn't cause you issues, looks like this:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^(.+)/$ /$1 [R=301,L]

  RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

In addition to the re-ordering of rules, you don't need to have the full {HTTP_HOST} reference in the trailing slash redirect, so I changed your original:

RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

to the more simple (but, effectively, the same):

RewriteRule ^(.+)/$ /$1 [R=301,L]

And, there's no reason to include the condition that this rule applies only to site.com, unless you're running other domains on this same virtual host, and want requests to some domain(s) skip this rule. So, I altogether removed this line from your original:

RewriteCond %{HTTP_HOST} !^\.site\.com$ [NC]

But, if you want to remove the trailing slash on requests only to site.com, but not on requests to other domains (say, site2.com), you could add the condition, like this:

RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteRule ^(.+)/$ /$1 [R=301,L]

Or, if you wanted it to apply to all domains except site.com, you'd do this, instead:

RewriteCond %{HTTP_HOST} !^site\.com$ [NC]
RewriteRule ^(.+)/$ /$1 [R=301,L]

Again, the removal of the trailing slash, which is a redirect, should occur before the rewrite to index.php (redirect rules above the rewrite to index.php in htaccess).

Finally, once you make this change, be sure to restart your browser and/or test in a different browser. Browsers can cache 301 redirects, and may not immediately pick-up these changes.

If you're ever testing a redirect, and want to avoid browser caching, use a 302 (temporary) redirect until you're done testing, and then change to a 301 (permanent) redirect. E.g., you can do this for testing:

RewriteRule ^(.+)/$ /$1 [R=302,L]

Added:

If you still have any trouble, you can be even more explicit about where this rule should not apply, e.g., to not apply to your themes and system folders, etc:

RewriteCond $1 !^(css|images|themes|system)/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/$ /$1 [R=301,L]

Original:

I'm not sure if this is the cause of what you're seeing, but your first line of the above .htaccess doesn't seem right:

RewriteCond %{HTTP_HOST} !^\.site\.com$ [NC]

The dot at the beginning means it's excluding the host name .site.com (i.e., extra dot at the beginning).

So, I think you want to remove that . to make it just:

RewriteCond %{HTTP_HOST} !^site\.com$ [NC]

Does making that change resolve your issue?

8
  • Jay, I tried removing it and the forward slash is no longer removed. Sep 17, 2013 at 20:59
  • OK, two questions: 1) does changing that line get rid of the too many redirects problem? And, 2) do you, or do you not, want to remove the trailing slash on site.com URLs?
    – Jay F
    Sep 17, 2013 at 21:44
  • removing both lines or changing that line means the redirect problem goes away. But I DO want to remove the trailing slashes. Sep 17, 2013 at 22:08
  • OK. Can you update your original post and add in all of your htaccess rewrite rules? Definitely sounds like two rules are fighting with each other, so need to see if we can figure out which two.
    – Jay F
    Sep 17, 2013 at 22:22
  • Thanks Jay for taking the time to look into this for me. I have updated the original post. Sep 18, 2013 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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