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>

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

13 Comments

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.
See update now, For http://localhost/foo/index.php/foo/bar I am getting http://localhost/foo/foo/bar
Make sure this rule is your first rule.
|
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]

1 Comment

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/
0

You can use like this

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.