2

This is for basic HTML/PHP pages, no query strings, etc.. I have searched high and low and find resources for removing the 'index.php' from the URI, or removing '.php' and other file extensions.. and even adding a trailing slash. But, everytime I try to use them all, or use examples that I have found, I get a 500 server error.

I may pass a query string on one page, but really I just want to remove 'index.php' from the index page and have all sub pages without the file extension. So, something like this:

domain.com/index.php to domain.com/ domain.com/page1.php to domain.com/page1/ domain.com/page2.php to domain.com/page2/ domain.com/page3.php to domain.com/page3/

All the examples I can find are focused on CMSs, etc.. with query strings, etc.. nothing just focusing on the base URI, which I think might be what was causing the errors i was getting.

Any help is appreciated !! Thanks

2 Answers 2

7

I got this from a rather helpful chap on a forum once - never fully understood it, and there is one caveat; it implies no trailing slash unless the request is a directory.

However, I thought it was worth posting - a guru out there may easily spot the fix!?

# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{HTTP_HOST} ^www\.domain\.com
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

# remove index
RewriteRule (.*)index$ $1 [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! I'll give this a go and see what we can figure out.. any gurus care to add to/adjust this to include a trailing slash to all requests??
FINALLY, an answer that works. I searched high and low. One note, I had to add "RewriteBase /" to the beginning because my files are nested in other folders that go below the domain, and it was routing them to lower levels, which showed up in the URL. = no good.
I too have been searching high and low for an answer to this and this is it. Thank you!
0

You can try this in your htaccess file:

Options +MultiViews

But it depends on wheter it is enabled by your webserver. Also, do note that htaccess files are specific to Apache webservers, so it won't work if you're using something else.

2 Comments

Do you still know somebody who is not using Apache? :)
@Marco Demaio: actually... I mostly use Nginx these days. Apache has some advantages but usually you don't need all those features.

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.