0

To note, I've found similar questions on StackOverflow but they have not worked as I need.

I have a URL such as:

http://www.example.com/index.php/test

I'd like to remove the index.php directory, so if the above is entered it would go to:

http://www.example.com/test

This appears to work

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

However if the url is:

http://www.example.com/index.php?option=example

It turns into

http://www.example.com/?option=example

So index.php should only be removed if it's a directory like in my first example.

Also if you type in for instance:

http://www.test.example.com/index.php/index.php/dfd

It should go to

http://www.test.example.com/dfd

1 Answer 1

1

the rules below will:

  • not apply for /index.php?o=a

  • redirect /index.php/index.php/dfd to /dfd

  • redirect /index.php/index.php/dfd?a=b to /dfd?a=b

  • redirect /index.php/index.php?a=b to /index.php?a=b

.

RewriteCond %{QUERY_STRING} .+  
RewriteRule ^index\.php(/index\.php)+/?$ /index.php [R=302,L,QSA,NC]

RewriteCond %{QUERY_STRING} ^$ [OR]
RewriteCond %{REQUEST_URI} !index\.php/?$
RewriteRule ((^|/)index\.php)+/?(.*)$ /$3 [R=302,L,QSA,NC]
Sign up to request clarification or add additional context in comments.

6 Comments

Looks very close to working as needed. When I try this for example: example.com/index.php/index.php?option=random it doesn't remove the /index.php/ That url should go to example.com/index.php?option=random Otherwise your solution seems perfect.
Thanks for the quick response. When using the updated code the site 500s.
To note I think the actual error it's putting in the log if it helps is: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
Thanks, it no longer 500s, but the other item still happens. "When I try this for example: example.com/index.php/index.php?option=random it doesn't remove the /index.php/ That url should go to example.com/index.php?option=random "
Just to note, tried the latest updated version and while the issue from the last comment now works, the other items don't know such as going to example.com/index.php/eee goes to example.com/index.php/ rather than example.com/eee . Sorry to be a pain. If it's not dooable the original option you posted is pretty close and I can just hope that the other use case doesn't happen.
|

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.