skip to Main Content

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]

2

Answers


  1. Chosen as BEST 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


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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search