Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5993487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:40:56+00:00 2026-05-22T23:40:56+00:00

I hate to ask this question because it’s been asked a million times, but

  • 0

I hate to ask this question because it’s been asked a million times, but the answers never seem satisfactory, and most of the threads seem abandoned without an accepted answer.

Here’s exactly what I need to do (bad urls are intentional due to low karma):

http://example.com/file.php redirects to http://example.com/file/

http://example.com/file must also redirect to http://example.com/file/

http://example.com/asdfsadf and http://examplecom/file/asdfasdf must go to the 404 page

Here’s the htaccess magic I cobbled together from posts here and elsewhere. It seems to work (unlike most of the abandoned threads on the topic, where there’s always some strange behavior).

 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME}\.php -f
 RewriteRule ^([^/]+)/$ $1.php
 RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
 RewriteRule (.*)$ /$1/ [R=301,L]
 RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/ 
 RewriteRule ^(([^/]+/)*[^.]+)\.php http://example.com/$1 [R=301,L]

Like I said, as far as I can tell, this works fine, even with subdirectories. Can any more knowledgeable folks tell me if I’m missing something. Could it be improved or shortened?

For what it’s worth, I’m also removing the www:

 RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
 RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

It seems to work fine. It is included after the other parts. Is this the best order?

Thanks everyone, I hope we can get a good, reliable answer for this out there because there’s a lot of bad ones.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:40:56+00:00Added an answer on May 22, 2026 at 11:40 pm

    Oh, I have the answer to this one!
    This little rewrite snippet to go in .htaccess will remove the extension from any file you specify in its url.

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    RewriteBase /
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://yourdomain.com/$1/ [L,R=301]
    

    Just paste it at the bottom of your .htaccess file in your root directory. The file is hidden so make sure ‘show hidden files’ is enabled in your ftp. ETA: last 3 lines should add a trailing slash to all files at yourcomain.com Do’t forget to replace youdomain.com correctly.

    That will remove the ‘.php’ from all your urls to php files! ^_^
    Hope I helped

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really hate to ask this question, because it seems like it's been asked
This question has been asked many times before but none of the previous ones
I hate to ask this question on this forum, but I'm having a lot
Okay I hate to ask this question ... but here goes. I am writing
I hate to ask the same question others have asked in Stackoverflow, but I
I hate to ask such a general question, and this is not a write
I hate to be the third person to ask this, but the previous two
I hate ask stupid questions like this, but why doesn't my code do what
I'd hate to ask an intentionally subjective question on here, but I don't have
I hate to ask another how do you do this with regex question on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.