htaccess RewriteRule results in 404 responseWhy do I get a double trailing slash depending on where my RewriteRule is located?Additionally, a 404 Not Found.htaccess & Mod_RewriteHow to exclude a sub-folder from HTaccess RewriteRuleInstalling wordpress in sub-directory with .htaccess in root directory.htaccess https redirect best methodExecute a PHP File Without the .PHP Extension on IISNginx rewrite, from htaccess-filesFix ERR_TOO_MANY_REDIRECTS after adding trailing slash in URLmod_rewrites in nested .htaccess causing strange 404

A map of non-pathological topology?

Sci-fi novel: ark ship from Earth is sent into space to another planet, one man woken early from cryosleep paints a giant mural

Teaching a class likely meant to inflate the GPA of student athletes

Is this a bug in plotting step functions?

First sign that you should look for another job?

If I leave the US through an airport, do I have to return through the same airport?

Separate SPI data

Longest bridge/tunnel that can be cycled over/through?

How to trick the reader into thinking they're following a redshirt instead of the protagonist?

Fermat's statement about the ancients: How serious was he?

Why did Intel abandon unified CPU cache?

Proving that a Russian cryptographic standard is too structured

Russian word for a male zebra

Advantages of the Exponential Family: why should we study it and use it?

What would be the way to say "just saying" in German? (Not the literal translation)

How can I remove material from this wood beam?

Why am I getting a strange double quote (“) in Open Office instead of the ordinary one (")?

Is it possible to have 2 different but equal size real number sets that have the same mean and standard deviation?

What is the logic behind taxing money for property?

With Ubuntu 18.04, how can I have a hot corner that locks the computer?

How to publish items after pipeline is finished?

Next date with distinct digits

Does putting salt first make it easier for attacker to bruteforce the hash?

What differences exist between adamantine and adamantite in all editions of D&D?



htaccess RewriteRule results in 404 response


Why do I get a double trailing slash depending on where my RewriteRule is located?Additionally, a 404 Not Found.htaccess & Mod_RewriteHow to exclude a sub-folder from HTaccess RewriteRuleInstalling wordpress in sub-directory with .htaccess in root directory.htaccess https redirect best methodExecute a PHP File Without the .PHP Extension on IISNginx rewrite, from htaccess-filesFix ERR_TOO_MANY_REDIRECTS after adding trailing slash in URLmod_rewrites in nested .htaccess causing strange 404






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have a website that is in the root folder. It has a .htaccess with teh code below:



RewriteEngine on
RewriteRule ^(.*)/$ $1.php


When I open http://example.com/login/ I get a "404 Not Found" error but when I open http://example.com/login without the trailing / the URL opens without any problem.



I need .htaccess code to set in the root directory to open PHP file like login.php as /login/ or /login (without trailing /).










share|improve this question






























    1















    I have a website that is in the root folder. It has a .htaccess with teh code below:



    RewriteEngine on
    RewriteRule ^(.*)/$ $1.php


    When I open http://example.com/login/ I get a "404 Not Found" error but when I open http://example.com/login without the trailing / the URL opens without any problem.



    I need .htaccess code to set in the root directory to open PHP file like login.php as /login/ or /login (without trailing /).










    share|improve this question


























      1












      1








      1








      I have a website that is in the root folder. It has a .htaccess with teh code below:



      RewriteEngine on
      RewriteRule ^(.*)/$ $1.php


      When I open http://example.com/login/ I get a "404 Not Found" error but when I open http://example.com/login without the trailing / the URL opens without any problem.



      I need .htaccess code to set in the root directory to open PHP file like login.php as /login/ or /login (without trailing /).










      share|improve this question
















      I have a website that is in the root folder. It has a .htaccess with teh code below:



      RewriteEngine on
      RewriteRule ^(.*)/$ $1.php


      When I open http://example.com/login/ I get a "404 Not Found" error but when I open http://example.com/login without the trailing / the URL opens without any problem.



      I need .htaccess code to set in the root directory to open PHP file like login.php as /login/ or /login (without trailing /).







      .htaccess mod-rewrite rewrite






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 24 at 21:57









      DocRoot

      20519




      20519










      asked Aug 19 '12 at 15:48









      MahmoodMahmood

      62




      62




















          2 Answers
          2






          active

          oldest

          votes


















          0














          Try ^([^/]+)/?$ and see if it works as expected.






          share|improve this answer

























          • After try this code RewriteEngine on RewriteRule ^([^/]+)/?$ $1.php i got 500 error

            – Mahmood
            Aug 19 '12 at 16:21












          • By simply making the trailing slash optional (and still having an all-encompassing capturing subpattern) you would expect this to create a rewrite loop (hence the 500 error). A request for /login/ would rewrite to /login.php to /login.php.php to /login.php.php.php, etc.

            – DocRoot
            May 24 at 21:33


















          0














          This behavior is typical of a conflict with MuiltiViews. You should disable MultiViews at the top of your .htaccess file:



          Options -MultiViews


          This explains why /login without the trailing / successfully serves the file - your RewriteRule is entirely bypassed in this scenario.



          With MultiViews enabled, when requesting /login/, mod_negotiation mocks up a rewrite map and issues an internal subrequest for /login.php before mod_rewrite gets to process the URL. The path-info (trailing /) is added back, to become /login.php/. This is passed back for further processing and matches your overly-generic (^(.*)/$) RewriteRule pattern, resulting in an internal rewrite to /login.php.php/ etc. Depending on your config, this could result in a 403 (as it can result in a malformed request), or a 404 (as you appear to be seeing).



          Disabling MultiViews alone (as mentioned above) would solve this problem. However, you should also consider making your RewriteRule pattern more restictive. For example, to avoid matching URLs that already contain a dot (ie. have a file extension):



          RewriteRule ^([^.]+)/$ $1.php [L]


          Simply making the regex more restrictive may even "fix" the error. However, it would be MultiViews making the internal subrequest, not mod_rewrite (your rule would simply be bypassed).




          as /login/ or /login (with or without the trailing /)




          To allow the rule to work with either /login/ or /login (ie. with or without the trailing slash) you would need to make the trailing slash optional, ie. /?. But you would also need to either make the preceding pattern non-greedy or include the slash in the negated character class in order to avoid capturing the trailing slash in the backreference.



          For example, either:



          RewriteRule ^([^.]+?)/?$ $1.php [L]


          Or,



          RewriteRule ^([^./]+)/?$ $1.php [L]


          But the second rule would only work with URLs that contain a single path segment, whereas the first would work with a URL of the form /foo/bar/login.



          In Summary



          Options -MultiViews

          RewriteEngine On
          RewriteRule ^([^.]+?)/?$ $1.php [L]





          share|improve this answer

























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "2"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f419092%2fhtaccess-rewriterule-results-in-404-response%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Try ^([^/]+)/?$ and see if it works as expected.






            share|improve this answer

























            • After try this code RewriteEngine on RewriteRule ^([^/]+)/?$ $1.php i got 500 error

              – Mahmood
              Aug 19 '12 at 16:21












            • By simply making the trailing slash optional (and still having an all-encompassing capturing subpattern) you would expect this to create a rewrite loop (hence the 500 error). A request for /login/ would rewrite to /login.php to /login.php.php to /login.php.php.php, etc.

              – DocRoot
              May 24 at 21:33















            0














            Try ^([^/]+)/?$ and see if it works as expected.






            share|improve this answer

























            • After try this code RewriteEngine on RewriteRule ^([^/]+)/?$ $1.php i got 500 error

              – Mahmood
              Aug 19 '12 at 16:21












            • By simply making the trailing slash optional (and still having an all-encompassing capturing subpattern) you would expect this to create a rewrite loop (hence the 500 error). A request for /login/ would rewrite to /login.php to /login.php.php to /login.php.php.php, etc.

              – DocRoot
              May 24 at 21:33













            0












            0








            0







            Try ^([^/]+)/?$ and see if it works as expected.






            share|improve this answer















            Try ^([^/]+)/?$ and see if it works as expected.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 19 '12 at 18:15









            HopelessN00b

            48.7k25117194




            48.7k25117194










            answered Aug 19 '12 at 16:16









            MoakMoak

            2442627




            2442627












            • After try this code RewriteEngine on RewriteRule ^([^/]+)/?$ $1.php i got 500 error

              – Mahmood
              Aug 19 '12 at 16:21












            • By simply making the trailing slash optional (and still having an all-encompassing capturing subpattern) you would expect this to create a rewrite loop (hence the 500 error). A request for /login/ would rewrite to /login.php to /login.php.php to /login.php.php.php, etc.

              – DocRoot
              May 24 at 21:33

















            • After try this code RewriteEngine on RewriteRule ^([^/]+)/?$ $1.php i got 500 error

              – Mahmood
              Aug 19 '12 at 16:21












            • By simply making the trailing slash optional (and still having an all-encompassing capturing subpattern) you would expect this to create a rewrite loop (hence the 500 error). A request for /login/ would rewrite to /login.php to /login.php.php to /login.php.php.php, etc.

              – DocRoot
              May 24 at 21:33
















            After try this code RewriteEngine on RewriteRule ^([^/]+)/?$ $1.php i got 500 error

            – Mahmood
            Aug 19 '12 at 16:21






            After try this code RewriteEngine on RewriteRule ^([^/]+)/?$ $1.php i got 500 error

            – Mahmood
            Aug 19 '12 at 16:21














            By simply making the trailing slash optional (and still having an all-encompassing capturing subpattern) you would expect this to create a rewrite loop (hence the 500 error). A request for /login/ would rewrite to /login.php to /login.php.php to /login.php.php.php, etc.

            – DocRoot
            May 24 at 21:33





            By simply making the trailing slash optional (and still having an all-encompassing capturing subpattern) you would expect this to create a rewrite loop (hence the 500 error). A request for /login/ would rewrite to /login.php to /login.php.php to /login.php.php.php, etc.

            – DocRoot
            May 24 at 21:33













            0














            This behavior is typical of a conflict with MuiltiViews. You should disable MultiViews at the top of your .htaccess file:



            Options -MultiViews


            This explains why /login without the trailing / successfully serves the file - your RewriteRule is entirely bypassed in this scenario.



            With MultiViews enabled, when requesting /login/, mod_negotiation mocks up a rewrite map and issues an internal subrequest for /login.php before mod_rewrite gets to process the URL. The path-info (trailing /) is added back, to become /login.php/. This is passed back for further processing and matches your overly-generic (^(.*)/$) RewriteRule pattern, resulting in an internal rewrite to /login.php.php/ etc. Depending on your config, this could result in a 403 (as it can result in a malformed request), or a 404 (as you appear to be seeing).



            Disabling MultiViews alone (as mentioned above) would solve this problem. However, you should also consider making your RewriteRule pattern more restictive. For example, to avoid matching URLs that already contain a dot (ie. have a file extension):



            RewriteRule ^([^.]+)/$ $1.php [L]


            Simply making the regex more restrictive may even "fix" the error. However, it would be MultiViews making the internal subrequest, not mod_rewrite (your rule would simply be bypassed).




            as /login/ or /login (with or without the trailing /)




            To allow the rule to work with either /login/ or /login (ie. with or without the trailing slash) you would need to make the trailing slash optional, ie. /?. But you would also need to either make the preceding pattern non-greedy or include the slash in the negated character class in order to avoid capturing the trailing slash in the backreference.



            For example, either:



            RewriteRule ^([^.]+?)/?$ $1.php [L]


            Or,



            RewriteRule ^([^./]+)/?$ $1.php [L]


            But the second rule would only work with URLs that contain a single path segment, whereas the first would work with a URL of the form /foo/bar/login.



            In Summary



            Options -MultiViews

            RewriteEngine On
            RewriteRule ^([^.]+?)/?$ $1.php [L]





            share|improve this answer





























              0














              This behavior is typical of a conflict with MuiltiViews. You should disable MultiViews at the top of your .htaccess file:



              Options -MultiViews


              This explains why /login without the trailing / successfully serves the file - your RewriteRule is entirely bypassed in this scenario.



              With MultiViews enabled, when requesting /login/, mod_negotiation mocks up a rewrite map and issues an internal subrequest for /login.php before mod_rewrite gets to process the URL. The path-info (trailing /) is added back, to become /login.php/. This is passed back for further processing and matches your overly-generic (^(.*)/$) RewriteRule pattern, resulting in an internal rewrite to /login.php.php/ etc. Depending on your config, this could result in a 403 (as it can result in a malformed request), or a 404 (as you appear to be seeing).



              Disabling MultiViews alone (as mentioned above) would solve this problem. However, you should also consider making your RewriteRule pattern more restictive. For example, to avoid matching URLs that already contain a dot (ie. have a file extension):



              RewriteRule ^([^.]+)/$ $1.php [L]


              Simply making the regex more restrictive may even "fix" the error. However, it would be MultiViews making the internal subrequest, not mod_rewrite (your rule would simply be bypassed).




              as /login/ or /login (with or without the trailing /)




              To allow the rule to work with either /login/ or /login (ie. with or without the trailing slash) you would need to make the trailing slash optional, ie. /?. But you would also need to either make the preceding pattern non-greedy or include the slash in the negated character class in order to avoid capturing the trailing slash in the backreference.



              For example, either:



              RewriteRule ^([^.]+?)/?$ $1.php [L]


              Or,



              RewriteRule ^([^./]+)/?$ $1.php [L]


              But the second rule would only work with URLs that contain a single path segment, whereas the first would work with a URL of the form /foo/bar/login.



              In Summary



              Options -MultiViews

              RewriteEngine On
              RewriteRule ^([^.]+?)/?$ $1.php [L]





              share|improve this answer



























                0












                0








                0







                This behavior is typical of a conflict with MuiltiViews. You should disable MultiViews at the top of your .htaccess file:



                Options -MultiViews


                This explains why /login without the trailing / successfully serves the file - your RewriteRule is entirely bypassed in this scenario.



                With MultiViews enabled, when requesting /login/, mod_negotiation mocks up a rewrite map and issues an internal subrequest for /login.php before mod_rewrite gets to process the URL. The path-info (trailing /) is added back, to become /login.php/. This is passed back for further processing and matches your overly-generic (^(.*)/$) RewriteRule pattern, resulting in an internal rewrite to /login.php.php/ etc. Depending on your config, this could result in a 403 (as it can result in a malformed request), or a 404 (as you appear to be seeing).



                Disabling MultiViews alone (as mentioned above) would solve this problem. However, you should also consider making your RewriteRule pattern more restictive. For example, to avoid matching URLs that already contain a dot (ie. have a file extension):



                RewriteRule ^([^.]+)/$ $1.php [L]


                Simply making the regex more restrictive may even "fix" the error. However, it would be MultiViews making the internal subrequest, not mod_rewrite (your rule would simply be bypassed).




                as /login/ or /login (with or without the trailing /)




                To allow the rule to work with either /login/ or /login (ie. with or without the trailing slash) you would need to make the trailing slash optional, ie. /?. But you would also need to either make the preceding pattern non-greedy or include the slash in the negated character class in order to avoid capturing the trailing slash in the backreference.



                For example, either:



                RewriteRule ^([^.]+?)/?$ $1.php [L]


                Or,



                RewriteRule ^([^./]+)/?$ $1.php [L]


                But the second rule would only work with URLs that contain a single path segment, whereas the first would work with a URL of the form /foo/bar/login.



                In Summary



                Options -MultiViews

                RewriteEngine On
                RewriteRule ^([^.]+?)/?$ $1.php [L]





                share|improve this answer















                This behavior is typical of a conflict with MuiltiViews. You should disable MultiViews at the top of your .htaccess file:



                Options -MultiViews


                This explains why /login without the trailing / successfully serves the file - your RewriteRule is entirely bypassed in this scenario.



                With MultiViews enabled, when requesting /login/, mod_negotiation mocks up a rewrite map and issues an internal subrequest for /login.php before mod_rewrite gets to process the URL. The path-info (trailing /) is added back, to become /login.php/. This is passed back for further processing and matches your overly-generic (^(.*)/$) RewriteRule pattern, resulting in an internal rewrite to /login.php.php/ etc. Depending on your config, this could result in a 403 (as it can result in a malformed request), or a 404 (as you appear to be seeing).



                Disabling MultiViews alone (as mentioned above) would solve this problem. However, you should also consider making your RewriteRule pattern more restictive. For example, to avoid matching URLs that already contain a dot (ie. have a file extension):



                RewriteRule ^([^.]+)/$ $1.php [L]


                Simply making the regex more restrictive may even "fix" the error. However, it would be MultiViews making the internal subrequest, not mod_rewrite (your rule would simply be bypassed).




                as /login/ or /login (with or without the trailing /)




                To allow the rule to work with either /login/ or /login (ie. with or without the trailing slash) you would need to make the trailing slash optional, ie. /?. But you would also need to either make the preceding pattern non-greedy or include the slash in the negated character class in order to avoid capturing the trailing slash in the backreference.



                For example, either:



                RewriteRule ^([^.]+?)/?$ $1.php [L]


                Or,



                RewriteRule ^([^./]+)/?$ $1.php [L]


                But the second rule would only work with URLs that contain a single path segment, whereas the first would work with a URL of the form /foo/bar/login.



                In Summary



                Options -MultiViews

                RewriteEngine On
                RewriteRule ^([^.]+?)/?$ $1.php [L]






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited May 24 at 21:22

























                answered May 24 at 21:07









                DocRootDocRoot

                20519




                20519



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Server Fault!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f419092%2fhtaccess-rewriterule-results-in-404-response%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

                    Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

                    What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company