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 7444797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:35:27+00:00 2026-05-29T11:35:27+00:00

I would like to create rewrite rules in my .htaccess file to do the

  • 0

I would like to create rewrite rules in my .htaccess file to do the following:

  • When accessed via domain.com/abc.php: remove the file extension, append a trailing slash and load the abc.php file. url should look like this after rewrite: domain.com/abc/

  • When accessed via domain.com/abc/: leave the url as is and load abc.php

  • When accessed via domain.com/abc: append trailing slash and load abc.php. url should look like this after rewrite: domain.com/abc/

  • Remove www

  • Redirect to 404 page (404.php) when accessed url doesn’t resolve to folder or file, e.g. when accessing either domain.com/nothingthere.php or domain.com/nothingthere/ or domain.com/nothingthere

  • Make some permanent 301 redirects from old urls to new ones (e.g. domain.com/abc.html to domain.com/abc/)

All php files sit in the document root directory, but if there is a solution that would make urls such as domain.com/abc/def/ (would load domain.com/abc/def.php) also work it would be great as well, but not necessary

So here is what I have at the moment (thrown together from various sources and samples from around the web

<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  # redirect from www to non-www
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

  # remove php file extension
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
  RewriteRule (.*)\.php$ /$1/ [L,R=301]

  # add trailing slash
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^.*[^/]$ /$0/ [L,R=301]

  # resolve urls to matching php files 
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule (.*)/$ $1.php [L]

With this the first four requirements seem to work, whether I enter domain.com/abc.php, domain.com/abc/ or domain.com/abc, the final url always ends up being domain.com/abc/ and domain.com/abc.php is loaded.

When I enter a url that resolves to a file that doesn’t exists I’m getting an error 310 (redirect loop), when really a 404 page should be loaded. Additionally I haven’t tried if subfolders work, but as I said, that’s low priority. I’m pretty sure I can just slap the permanent 301 redirects for legacy urls on top of that without any issues as well, just wanted to mention it. So the real issue is really the non working 404 page.

  • 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-29T11:35:28+00:00Added an answer on May 29, 2026 at 11:35 am

    I’ve had problems with getting ErrorDocument to work reliably with rewrite errors, so I tend to prefer to handle invalid pages correctly in my rewrite cascade. I’ve tried to cover a fully range of test vectors with this. Didn’t find any gaps.

    Some general points:

    • You need to use the DOCUMENT_ROOT environment variable in this. Unfortunately if you use a shared hosting service then this isn’t set up correctly during rewrite execution, so hosting providers set up a shadow variable to do the same job. Mine uses DOCUMENT_ROOT_REAL, but I’ve also come across PHP_DOCUMENT_ROOT. Do a phpinfo to find out what to use for your service.
    • There’s a debug info rule that you can trim as long as you replace DOCROOT appropriately
    • You can’t always use %{REQUEST_FILENAME} where you’d expect to. This is because if the URI maps to DOCROOT/somePathThatExists/name/theRest then the %{REQUEST_FILENAME} is set to DOCROOT/somePathThatExists/name rather than the full pattern equivalent to the rule match string.
    • This is “Per Directory” so no leading slashes and we need to realise that the rewrite engine will loop on the .htaccess file until a no-match stop occurs.
    • This processes all valid combinations and at the very end redirects to the 404.php which I assume sets the 404 Status as well as displaying the error page.
    • It will currently decode someValidScript.php/otherRubbish in the SEO fashion, but extra logic can pick this one up as well.

    So here is the .htaccess fragment:

    Options -Indexes -MultiViews
    AcceptPathInfo Off
    
    RewriteEngine On
    RewriteBase   /
    
    ## Looping stop.  Not needed in Apache 2.3 as this introduces the [END] flag
    RewriteCond %{ENV:REDIRECT_END}  =1
    RewriteRule ^                    -                       [L,NS]
    
    ## 302 redirections ##
    
    RewriteRule ^ - [E=DOCROOT:%{ENV:DOCUMENT_ROOT_REAL},E=URI:%{REQUEST_URI},E=REQFN:%{REQUEST_FILENAME},E=FILENAME:%{SCRIPT_FILENAME}]
    
    # redirect from HTTP://www to non-www
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST}        ^www\.(.+)$ [NC]
    RewriteRule ^                   http://%1%{REQUEST_URI}  [R=301,L]
    
    # remove php file extension on GETs (no point in /[^?\s]+\.php as rule pattern requires this)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_METHOD}   =GET
    RewriteRule (.*)\.php$          $1/                      [L,R=301]
    
    # add trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^.*[^/]$            $0/                      [L,R=301]
    
    # terminate if file exists.  Note this match may be after internal redirect.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^                   -                        [L,E=END:1]
    
    # terminate if directory index.php exists.  Note this match may be after internal redirect.
    RewriteCond %{REQUEST_FILENAME}    -d
    RewriteCond %{ENV:DOCROOT}/$1/index.php    -f
    RewriteRule ^(.*)(/?)$             $1/index.php          [L,NS,E=END:1]
    
    # resolve urls to matching php files 
    RewriteCond %{ENV:DOCROOT}/$1.php  -f
    RewriteRule ^(.*?)/?$              $1.php                [L,NS,E=END:1]
    
    # Anything else redirect to the 404 script.  This one does have the leading /
    
    RewriteRule ^                      /404.php              [L,NS,E=END:1]
    

    Enjoy 🙂

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

Sidebar

Related Questions

1) I would like to create a rewrite rule that removes the file at
I would like to create a COM object that runs in an out of
I would like create URL rewrite rule that will set default document for my
I'm using the following line in my .htaccess file to create custom page stubs
I am trying to create a rewrite rule that accomplishes two things: Redirect (www.)?domain.com
I am new to the URL rewrite rules and would like to rewrite some
i would like create a array of structure which have a dynamic array :
I would like to create a flash presentation for a web page. It would
I would like to create a 'New Document' dialog similar to the Office 2007
I would like to create a UIBarButtonItem on my iPhone application which has two

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.