The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Modification to the suggested `.htaccess` rules

alex's Avatar

alex

26 Jun, 2015 11:21 PM

After struggling with this for a day now, I got some help from someone on SO that I think would be useful to other Slim users.

The problem is that with the current suggested rewrite rules, /index.php/* URLs are not automatically redirected to the canonical, pretty URLs. So for example, these URLs:

http://example.com/baseball/index.php/gloves
http://example.com/baseball/gloves

Are both valid and both take you to the same route, but are displayed as different URLs in the browser. This can cause problems with search engines in determining which is the canonical URL. By adding these lines to .htaccess:

# Dynamically rewrite base directory (see http://stackoverflow.com/questions/31062479/301-redirect-urls-that-are-also-being-rewritten)
RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=BASE:%2]

# Forward any requested URLs that specifically contain index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^index\.php(?:/(.*))?$ %{ENV:BASE}$1 [L,R=301,NC,NE]

This will allow you to 301 redirect your URLs that contain index.php to your canonical, pretty URLs.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac