3

I want user to be able use $_SERVER['PATH_INFO'] as a placeholder for which file to serve and get rid of index.php in the address bar

For example I want to serve me.com/index.php/settings1 as me.com/settings1 and so on for any PATH_INFO that the user goes to.

How would I write this as a htaccess rule? I don't even know where to start

If it helps, I'm on a shared hosting plan from hostgator.

Based on @EddyFreddy suggestion I tried both ways mentioned in that question with no luck. Here's what the file looks like so far:

suPHP_ConfigPath /home/user/php.ini
    <Files php.ini>
        order allow,deny    
        deny from all   
    </Files>

RewriteEngine On
RewriteBase /   

RewriteCond %{HTTP_HOST} ^www.mysite.com$ [NC]
RewriteRule .? http://mysite.com%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L] # tried with and without the `?`
anubhava
  • 761,203
  • 64
  • 569
  • 643
qwertymk
  • 34,200
  • 28
  • 121
  • 184
  • 3
    see this [question][1] [1]: http://stackoverflow.com/questions/4365129/htaccess-remove-index-php-from-url – Eddy Freddy Mar 19 '12 at 03:58
  • @EddyFreddy: it's not working see edit – qwertymk Mar 19 '12 at 10:55
  • @EddyFreddy: upon further inspection, I see that I don't *need* to have `index.php` in the address bar but I also want it to be removed if it is there. For example `me.com/settings1` works but I also want `me.com/index.php/settings1` to redirect to `me.com/settings1` – qwertymk Mar 19 '12 at 11:02

2 Answers2

8

This can be easily handled by mod_rewrite. Use this code in .htaccess under DOCUMENT_ROOT:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# external redirect from /index.php/foo to /foo/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php(/[^\s\?]+)? [NC]
RewriteRule ^ %1/ [L,R]

# external redirect to add trailing slash
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+((?!.+/[\s\?])[^\s\?]+) [NC]
RewriteRule ^ %1/ [L,R]

# internal forward from /foo to /index.php/foo
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php%{REQUEST_URI} [L,NC]

After you verify that everything is working fine then change R to R=301.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • For me this works, but `http://localhost/index.php` is not redirected to `http://localhost/`. – Eddy Freddy Mar 20 '12 at 13:34
  • @EddyFreddy: I wasn't sure if OP wanted to redirect `http://localhost/index.php` as well however now I have made an edit that will take care of this edge case as well. – anubhava Mar 20 '12 at 13:39
  • Amazing, I always wonder again about the crazy things that can be done with cryptic expressions :-) – Eddy Freddy Mar 20 '12 at 14:17
  • Is there a possibility for you to combine your ruleset for the external redirect with the "add trailing slash like WP"-section from my 2. edit to one rule? Could you please add a solution with a trailing slash (compatible to %{QUERY_STRING}, means /path?var= to /path/?var=) in another code-section to your post? This would be the greatest thing for all! – Eddy Freddy Mar 20 '12 at 18:52
  • @EddyFreddy: Pls check my edited answer above. Now these redirects will always add a trailing slash `/` with or without Query String. – anubhava Mar 20 '12 at 19:19
  • Thank you very much, you are my man! This works pretty after removing the trailing slash from the first rewrite rule. Otherwise it produces in some situations a double slash. – Eddy Freddy Mar 20 '12 at 23:32
1

After longterm evaluation I found out, that the version from anubhava doesn't work for me in all cases. So I tried this modified version. It will handle existing files in existing dirs correct and produces no double-slashes.

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

###### Add trailing slash (optional) ######
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ $1/ [R=301,L]

###### external redirect from /index.php/foo to /foo ######
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php(/.+)?[\s\?] [NC]
RewriteRule ^ %1 [L,R=301]

###### internal forward from /foo to /index.php/foo ######
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php%{REQUEST_URI} [L]
Eddy Freddy
  • 1,820
  • 1
  • 13
  • 18
  • Should I care enough that people may link to index.php that it will mess with my ranking? – qwertymk Mar 19 '12 at 21:23
  • I would ignore the `index.php/settings1`redirecting to `/settings1` - this makes only sense if you want to rescue old Pagerank to new URLs. It seems quite difficult. Maybe you should have a look at different OSS to see how they do it. – Eddy Freddy Mar 20 '12 at 03:34
  • @qwertymk are you using a framework like codeignitter or zend, or do you want to build an own solution? – Eddy Freddy Mar 20 '12 at 11:58
  • @qwertymk This is just quick and dirty. I would recommend you to use a frameworks like Zend or Codeignitter - they do a good job for building websites and applications very fast and have good approaches for using ajax. – Eddy Freddy Mar 20 '12 at 13:07