1

I have an index.php controller what all URLs that aren't existing files redirect to. The .htaccess rules I currently have look like this:

RewriteEngine On
DirectorySlash Off

# Remove Trailing Slashes
RedirectMatch 302 ^(.*)/$ $1

# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=302]

# Reroute to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

What I want to do is use a 301 redirect if index.php is included in the URL. BUT! I can't use RewriteBase or queries that start with a slash because the webapp I'm developing could be in a subfolder. So basically, I only want to rewrite for the current directory:

example.com/foo/index.php/bar/whatever should redirect to example.com/foo/bar/whatever example.com/foo/index/bar/whatever should also redirect to example.com/foo/bar/whatever

localhost/place/foo/index.php/bar/whatever should redirect to localhost/place/foo/bar/whatever localhost/place/foo/index/bar/whatever should also redirect to localhost/place/foo/bar/whatever

How can I accomplish this with .htaccess?

UPDATED CODE: Everything here works EXCEPT: index.php is not removed from anywhere in the URL.

Here's the code:

<IfModule mod_rewrite.c>
RewriteEngine On
DirectorySlash Off

# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]

# Remove Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1 [R=302,L]

# Reroute to index.php
#RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?cURL=/$1 [L,QSA]

</IfModule>
Michelle
  • 560
  • 1
  • 7
  • 16

3 Answers3

2

Not a good idea to mix mod_rewrite rules with mod_alias ones. Try this updated .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
DirectorySlash Off

# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]

# Remove Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1 [R=302,L]

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

# Reroute to index.php
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?cURL=/$1 [L,QSA]
</IfModule>
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • With example.com/index.php/foo/ I get example.com//foo/ and with example.com/foo/index.php/foo/bar/ I get example.com//foo/bar/ And without the trailing slash (which the URLs I'm generating do not have) I get a redirect loop. – Michelle Jan 28 '14 at 04:43
  • See update now, For `http://localhost/foo/index.php/foo/bar` I am getting `http://localhost/foo/foo/bar` – anubhava Jan 28 '14 at 04:46
  • Make sure this rule is your first rule. – anubhava Jan 28 '14 at 04:47
  • http://localhost/foo/index.php/foo/bar is getting http://localhost/foo/bar for me with this. – Michelle Jan 28 '14 at 05:22
  • @rakesh-sharma answer is working, except for removing a trailing slash. If you have any idea how to make this work with his answer: RedirectMatch 302 ^(.*)/$ $1 – Michelle Jan 28 '14 at 05:25
  • This properly removes the trailing slashes, but doesn't remove index.php from the URL. – Michelle Jan 28 '14 at 14:31
  • 1
    With this, http://example.com/index.php/blah/blah => http://example.com//blah/blah and http://example.com/foo/index.php/blah/blah => http://example.com/foo//blah/blah. Also http://localhost/foo/index.php/blah/blah => http://localhost//blah/blah (removing "foo") ...I think we are close! Thank you for all your help! – Michelle Jan 28 '14 at 15:24
  • Did you not write this in your question: `example.com/foo/index.php/bar/whatever should redirect to example.com/foo/bar/whatever` Also your 2nd and 3rd tests cases are contradictory. Both have `/foo/index.php/blah/blah` as source which as per this answer will become: `/foo/blah/blah` – anubhava Jan 28 '14 at 15:35
  • Yes, that's what it SHOULD be doing. Instead, my local server is going up to the root directory. Localhost/folder/index.php/foo/bar is changing to localhost/foo/bar INSTEAD OF localhost/folder/foo/bar – Michelle Jan 28 '14 at 15:55
  • And, on my live server example.com/folder/index.php/foo/bar is redirecting to example.com/folder//foo/bar... How do I get rid of the extra slash? – Michelle Jan 28 '14 at 15:57
  • Most likely on your localhost you don't have this .htaccess in `DocumentRoot` Also see updated code. – anubhava Jan 28 '14 at 16:07
  • What would be the easiest way to specify that the rules should only apply to a subdirectory? I'm assuming I could write it into the last two rewrite rules somehow. It's not really NECESSARY but it would be nice to know. – Michelle Jan 28 '14 at 16:46
  • Simply place these rules into `/subdir/.htaccess` to make sure rules apply only to that subdir and below paths. – anubhava Jan 28 '14 at 16:48
1

Make sure you have mod rewrite on on your server try

RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
  • This seems to work for everything...except removing trailing slashes. I get a query string of the same URL when it removes a slash. IE: example.com/test/123/456/ becomes example.com/test/123/456?/test/123/456/ – Michelle Jan 28 '14 at 05:15
0

You can use like this

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Agha Umair Ahmed
  • 1,037
  • 7
  • 12