Apache Rewrite to remove index.php?












0














I am trying to rewrite my URL's to remove index.php? but I'm struggling a little to get it to work. The closest I can get is the answer here: remove question mark from 301 redirect using htaccess when the user enters the old URL



I need to convert the URLs to pretty URLs on the way out, and rewrite them back to the proper URL on the way in. The structure of the URLs is as follows:



https://sub.domain.com/index.php?/folder1/folder2-etc



Using the code from the referenced answer results in a double forward slash:



https://sub.domain.com//folder1/folder2-etc



The rewrite rules I'm using from the referenced answer are:



RewriteEngine On

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

RewriteCond %{THE_REQUEST} s/+?([^s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]

# internal forward from pretty URL to actual one
RewriteRule ^((?!web/)[^/.]+)/?$ /index.php?$1 [L,QSA,NC]


I suspect I know how to solve the first bit, but I'm struggling to understand the second rule for the internal forward.



Additionally, I'm wondering if this is the best way to do this. I'm currently running an Apache backend behind an Nginx reverse proxy. Would I be better doing the rewrite on the Nginx side and the internal forward on Apache?



EDIT:



Complication: I've noticed an additional structure to complicate things. Some URLs appear to have https://sub.domain.com/picture.php?/folder1/folder2-etc



For these, I'd be quite happy to keep 'picture' and just remove the .php? bit.



I'm guessing that for the first bit, Id need to do something like the following:



    RewriteCond %{THE_REQUEST} s/+index.php?/([^s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]

RewriteCond %{THE_REQUEST} s/+picture.php?/([^s&]+) [NC]
RewriteRule ^(.*)$ /picture/%1 [R=301,L]


But have no idea where to start with the opposite.... ie converting pretty urls back to standard. It would help if the following section could be explained to me?



    ^((?!web/)[^/.]+)/?$ /index.php?$1 [L,QSA,NC]









share|improve this question





























    0














    I am trying to rewrite my URL's to remove index.php? but I'm struggling a little to get it to work. The closest I can get is the answer here: remove question mark from 301 redirect using htaccess when the user enters the old URL



    I need to convert the URLs to pretty URLs on the way out, and rewrite them back to the proper URL on the way in. The structure of the URLs is as follows:



    https://sub.domain.com/index.php?/folder1/folder2-etc



    Using the code from the referenced answer results in a double forward slash:



    https://sub.domain.com//folder1/folder2-etc



    The rewrite rules I'm using from the referenced answer are:



    RewriteEngine On

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

    RewriteCond %{THE_REQUEST} s/+?([^s&]+) [NC]
    RewriteRule ^ /%1? [R=301,L]

    # internal forward from pretty URL to actual one
    RewriteRule ^((?!web/)[^/.]+)/?$ /index.php?$1 [L,QSA,NC]


    I suspect I know how to solve the first bit, but I'm struggling to understand the second rule for the internal forward.



    Additionally, I'm wondering if this is the best way to do this. I'm currently running an Apache backend behind an Nginx reverse proxy. Would I be better doing the rewrite on the Nginx side and the internal forward on Apache?



    EDIT:



    Complication: I've noticed an additional structure to complicate things. Some URLs appear to have https://sub.domain.com/picture.php?/folder1/folder2-etc



    For these, I'd be quite happy to keep 'picture' and just remove the .php? bit.



    I'm guessing that for the first bit, Id need to do something like the following:



        RewriteCond %{THE_REQUEST} s/+index.php?/([^s&]+) [NC]
    RewriteRule ^ /%1? [R=301,L]

    RewriteCond %{THE_REQUEST} s/+picture.php?/([^s&]+) [NC]
    RewriteRule ^(.*)$ /picture/%1 [R=301,L]


    But have no idea where to start with the opposite.... ie converting pretty urls back to standard. It would help if the following section could be explained to me?



        ^((?!web/)[^/.]+)/?$ /index.php?$1 [L,QSA,NC]









    share|improve this question



























      0












      0








      0







      I am trying to rewrite my URL's to remove index.php? but I'm struggling a little to get it to work. The closest I can get is the answer here: remove question mark from 301 redirect using htaccess when the user enters the old URL



      I need to convert the URLs to pretty URLs on the way out, and rewrite them back to the proper URL on the way in. The structure of the URLs is as follows:



      https://sub.domain.com/index.php?/folder1/folder2-etc



      Using the code from the referenced answer results in a double forward slash:



      https://sub.domain.com//folder1/folder2-etc



      The rewrite rules I'm using from the referenced answer are:



      RewriteEngine On

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

      RewriteCond %{THE_REQUEST} s/+?([^s&]+) [NC]
      RewriteRule ^ /%1? [R=301,L]

      # internal forward from pretty URL to actual one
      RewriteRule ^((?!web/)[^/.]+)/?$ /index.php?$1 [L,QSA,NC]


      I suspect I know how to solve the first bit, but I'm struggling to understand the second rule for the internal forward.



      Additionally, I'm wondering if this is the best way to do this. I'm currently running an Apache backend behind an Nginx reverse proxy. Would I be better doing the rewrite on the Nginx side and the internal forward on Apache?



      EDIT:



      Complication: I've noticed an additional structure to complicate things. Some URLs appear to have https://sub.domain.com/picture.php?/folder1/folder2-etc



      For these, I'd be quite happy to keep 'picture' and just remove the .php? bit.



      I'm guessing that for the first bit, Id need to do something like the following:



          RewriteCond %{THE_REQUEST} s/+index.php?/([^s&]+) [NC]
      RewriteRule ^ /%1? [R=301,L]

      RewriteCond %{THE_REQUEST} s/+picture.php?/([^s&]+) [NC]
      RewriteRule ^(.*)$ /picture/%1 [R=301,L]


      But have no idea where to start with the opposite.... ie converting pretty urls back to standard. It would help if the following section could be explained to me?



          ^((?!web/)[^/.]+)/?$ /index.php?$1 [L,QSA,NC]









      share|improve this question















      I am trying to rewrite my URL's to remove index.php? but I'm struggling a little to get it to work. The closest I can get is the answer here: remove question mark from 301 redirect using htaccess when the user enters the old URL



      I need to convert the URLs to pretty URLs on the way out, and rewrite them back to the proper URL on the way in. The structure of the URLs is as follows:



      https://sub.domain.com/index.php?/folder1/folder2-etc



      Using the code from the referenced answer results in a double forward slash:



      https://sub.domain.com//folder1/folder2-etc



      The rewrite rules I'm using from the referenced answer are:



      RewriteEngine On

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

      RewriteCond %{THE_REQUEST} s/+?([^s&]+) [NC]
      RewriteRule ^ /%1? [R=301,L]

      # internal forward from pretty URL to actual one
      RewriteRule ^((?!web/)[^/.]+)/?$ /index.php?$1 [L,QSA,NC]


      I suspect I know how to solve the first bit, but I'm struggling to understand the second rule for the internal forward.



      Additionally, I'm wondering if this is the best way to do this. I'm currently running an Apache backend behind an Nginx reverse proxy. Would I be better doing the rewrite on the Nginx side and the internal forward on Apache?



      EDIT:



      Complication: I've noticed an additional structure to complicate things. Some URLs appear to have https://sub.domain.com/picture.php?/folder1/folder2-etc



      For these, I'd be quite happy to keep 'picture' and just remove the .php? bit.



      I'm guessing that for the first bit, Id need to do something like the following:



          RewriteCond %{THE_REQUEST} s/+index.php?/([^s&]+) [NC]
      RewriteRule ^ /%1? [R=301,L]

      RewriteCond %{THE_REQUEST} s/+picture.php?/([^s&]+) [NC]
      RewriteRule ^(.*)$ /picture/%1 [R=301,L]


      But have no idea where to start with the opposite.... ie converting pretty urls back to standard. It would help if the following section could be explained to me?



          ^((?!web/)[^/.]+)/?$ /index.php?$1 [L,QSA,NC]






      regex apache .htaccess mod-rewrite url-rewriting






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 15:43

























      asked Nov 23 '18 at 10:14









      MB263

      103




      103
























          2 Answers
          2






          active

          oldest

          votes


















          0














          RewriteRule ^/*picture/(.*)$ /picture.php?/$1 [L]
          RewriteRule ^/*(?!/*index.php$)(.*)$ /index.php?/$1 [L]


          should do the trick. I wasn't able to test it yet though.



          I only used the [L] last flag to stop applying rules on match. The QSA query string append flag doesn't seem to make sense as you don't seem to use ?key=value&... syntax anyway. Also dunno if you actually need the NC case-insensitive flag...



          Side note:
          I hope your php files don't serve paths with .. in them, as that would allow people to read arbitrary files from disk, e.g. /picture/../../../etc/passwd






          share|improve this answer































            0














            Apologies, but as it turns out, the main reason I can't get anything to work is due to the use of relative URLs and dynamically generated links within the PHP. Not something I can change unfortunately. The not perfect URLs are something I'm going to have to live with. For reference, the app I'm using is Piwigo






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              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%2fstackoverflow.com%2fquestions%2f53444703%2fapache-rewrite-to-remove-index-php%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














              RewriteRule ^/*picture/(.*)$ /picture.php?/$1 [L]
              RewriteRule ^/*(?!/*index.php$)(.*)$ /index.php?/$1 [L]


              should do the trick. I wasn't able to test it yet though.



              I only used the [L] last flag to stop applying rules on match. The QSA query string append flag doesn't seem to make sense as you don't seem to use ?key=value&... syntax anyway. Also dunno if you actually need the NC case-insensitive flag...



              Side note:
              I hope your php files don't serve paths with .. in them, as that would allow people to read arbitrary files from disk, e.g. /picture/../../../etc/passwd






              share|improve this answer




























                0














                RewriteRule ^/*picture/(.*)$ /picture.php?/$1 [L]
                RewriteRule ^/*(?!/*index.php$)(.*)$ /index.php?/$1 [L]


                should do the trick. I wasn't able to test it yet though.



                I only used the [L] last flag to stop applying rules on match. The QSA query string append flag doesn't seem to make sense as you don't seem to use ?key=value&... syntax anyway. Also dunno if you actually need the NC case-insensitive flag...



                Side note:
                I hope your php files don't serve paths with .. in them, as that would allow people to read arbitrary files from disk, e.g. /picture/../../../etc/passwd






                share|improve this answer


























                  0












                  0








                  0






                  RewriteRule ^/*picture/(.*)$ /picture.php?/$1 [L]
                  RewriteRule ^/*(?!/*index.php$)(.*)$ /index.php?/$1 [L]


                  should do the trick. I wasn't able to test it yet though.



                  I only used the [L] last flag to stop applying rules on match. The QSA query string append flag doesn't seem to make sense as you don't seem to use ?key=value&... syntax anyway. Also dunno if you actually need the NC case-insensitive flag...



                  Side note:
                  I hope your php files don't serve paths with .. in them, as that would allow people to read arbitrary files from disk, e.g. /picture/../../../etc/passwd






                  share|improve this answer














                  RewriteRule ^/*picture/(.*)$ /picture.php?/$1 [L]
                  RewriteRule ^/*(?!/*index.php$)(.*)$ /index.php?/$1 [L]


                  should do the trick. I wasn't able to test it yet though.



                  I only used the [L] last flag to stop applying rules on match. The QSA query string append flag doesn't seem to make sense as you don't seem to use ?key=value&... syntax anyway. Also dunno if you actually need the NC case-insensitive flag...



                  Side note:
                  I hope your php files don't serve paths with .. in them, as that would allow people to read arbitrary files from disk, e.g. /picture/../../../etc/passwd







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 25 '18 at 16:31

























                  answered Nov 24 '18 at 22:24









                  Jay

                  1,351511




                  1,351511

























                      0














                      Apologies, but as it turns out, the main reason I can't get anything to work is due to the use of relative URLs and dynamically generated links within the PHP. Not something I can change unfortunately. The not perfect URLs are something I'm going to have to live with. For reference, the app I'm using is Piwigo






                      share|improve this answer


























                        0














                        Apologies, but as it turns out, the main reason I can't get anything to work is due to the use of relative URLs and dynamically generated links within the PHP. Not something I can change unfortunately. The not perfect URLs are something I'm going to have to live with. For reference, the app I'm using is Piwigo






                        share|improve this answer
























                          0












                          0








                          0






                          Apologies, but as it turns out, the main reason I can't get anything to work is due to the use of relative URLs and dynamically generated links within the PHP. Not something I can change unfortunately. The not perfect URLs are something I'm going to have to live with. For reference, the app I'm using is Piwigo






                          share|improve this answer












                          Apologies, but as it turns out, the main reason I can't get anything to work is due to the use of relative URLs and dynamically generated links within the PHP. Not something I can change unfortunately. The not perfect URLs are something I'm going to have to live with. For reference, the app I'm using is Piwigo







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 7 '18 at 14:40









                          MB263

                          103




                          103






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • 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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2fstackoverflow.com%2fquestions%2f53444703%2fapache-rewrite-to-remove-index-php%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

                              What visual should I use to simply compare current year value vs last year in Power BI desktop

                              How to ignore python UserWarning in pytest?

                              Alexandru Averescu