Amjad
Amjad

Reputation: 2100

htaccess code break down and some explanations

I've the following .htaccess file code

RewriteEngine On
RewriteBase /claimspro/

##-----Establish a custome 404 File not Found Page---
ErrorDocument 404 /claimspro/filenotfound.php

##-----Prevent Directory File listing in all folder---
IndexIgnore *

RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php\?caseid=([0-9]+)&?([^\ ]*)
RewriteRule ^ %1/caseid/%2/?%3 [L,R]
RewriteRule ^([A-Za-z0-9]+)/caseid/([0-9]+)/ $1.php?caseid=$2 [L,QSA]

RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php
RewriteRule ^ %1/?%2 [L,R]
RewriteRule ^([A-Za-z0-9]+)/ $1.php [L,QSA]

The code above work the way I wanted it to be worked. It does change the url from this localhost/claimspro/afile.php?caseid=11 to localhost/claimspro/afile/caseid/11/

But when I add the another parameter i.e. /caseid/11/picid/12/ through the following way it behave weirdly.

Can I ask for some explanations of the code so can manage to get some result working in my favour.

RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php\?caseid=([0-9]+)&?([^\ ]*)
RewriteRule ^ %1/caseid/%2/?%3 [L,R]

First if may I ask what is this at the end of the first line (above) &?([^\ ]*) how would I have to modify this to add another parameter such as RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php\?caseid=([0-9]+)\&picid=([0-9]+)\&?([^\ ]*). What do I have to do here please if you could explain what I'm doing wrong here.

Secondly this line RewriteRule ^ %1/caseid/%2/?%3 [L,R] what is ?%3 [L,R] this %3 is representing what?

basically I want to add one more parameter in my htaccess file to cater the following

localhost/claimspro/anotherfile.php?caseid=11&picid=22

What additions should I make to my code?

Any idea?

Upvotes: 2

Views: 45

Answers (2)

Justin Iurman
Justin Iurman

Reputation: 19026

Let me answer your 2 questions.

Answer to first question: the pattern &?([^\ ]*) is matching the rest of the query string (if present).

Answer to second question: ?%3 [L,R] is appending the captured data (from first question) as a query string (the R flag is used to redirect with a 302 by default).

If i had to do it, i would have written it that way

Options -Indexes
ErrorDocument 404 /claimspro/filenotfound.php

RewriteEngine On
RewriteBase /claimspro/

RewriteCond %{THE_REQUEST} /([^/]+)\.php\?caseid=([0-9]+)&picid=([0-9]+)\s [NC]
RewriteRule . %1/caseid/%2/picid/%3/? [R=301,L]

RewriteCond %{THE_REQUEST} /([^/]+)\.php\?caseid=([0-9]+)\s [NC]
RewriteRule . %1/caseid/%2/? [R=301,L]

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

RewriteRule ^([^/]+)/caseid/([0-9]+)/picid/([0-9]+)/$ $1.php?caseid=$2&picid=$3 [L]
RewriteRule ^([^/]+)/caseid/([0-9]+)/$ $1.php?caseid=$2 [L]
RewriteRule ^([^/]+)/$ $1.php [L]

Upvotes: 2

Sharif
Sharif

Reputation: 728

I can't explain the code because i'm not an expert in mode rewriting you can change your code withe the following code and it would work

RewriteEngine On
RewriteBase /claimspro/

#-----Establish a custome 404 File not Found Page---
ErrorDocument 404 /claimspro/filenotfound.php

#-----Prevent Directory File listing in all folder---
IndexIgnore *

RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php\?caseid=([0-9]+)\&picid=([0-9]+)&?([^\ ]*)
RewriteRule ^ %1/caseid/%2/picid/%3/?%4 [L,R]
RewriteRule ^([A-Za-z0-9]+)/caseid/([0-9]+)/picid/([0-9]+)/ $1.php?caseid=$2&picid=$3 [L,QSA]

RewriteCond %{THE_REQUEST} ([A-Za-z0-9]+)\.php
RewriteRule ^ %1/?%2 [L,R]
RewriteRule ^([A-Za-z0-9]+)/ $1.php [L,QSA]

Let me know if it works

Regards

Upvotes: 1

Related Questions