skip to Main Content

This is my rewrite rule in .htaccess:

RewriteRule ^quiz/([A-Za-z0-9-]+)/([0-9-]+)/?$ quiz/index.php?inv=$1&d=$2 [NC,L]

It works perfectly fine for these URLs, which have a mix of cases, with and without hyphens, and all with a date for the final segment:

http://localhost/quiz/C-PFM/2021-04-30/

http://localhost/quiz/cpf-o/2021-04-30/

http://localhost/quiz/ipa/2021-04-30/

http://localhost/quiz/ipacsc/2021-04-30/

http://localhost/quiz/uob-sp/2021-04-30/

http://localhost/quiz/uobsp/2021-04-30/

http://localhost/quiz/Hol-V/2021-04-30/

http://localhost/quiz/HOLVA/2021-04-30/

That is, $_GET is correctly returning e.g.

inv=HOLVA; d=2021-04-30

As expected.

However, the same rule fails (returns a 404) for each of these examples, which also have a mix of case, and some with hyphen, some without:

http://localhost/quiz/CPFM/2021-04-30/

http://localhost/quiz/cpfo/2021-04-30/

http://localhost/quiz/ipac-sc/2021-04-30/

http://localhost/quiz/GLD10US/2021-04-30/

http://localhost/quiz/HolV/2021-04-30/

http://localhost/quiz/OCBCC/2021-04-30/

http://localhost/quiz/PP/2021-04-30/

Some of the failing ones could be made to work by adding a hyphen in random places. I did actually try to escape the hyphen in my rule, like this, but it didn’t seem to make a difference:

RewriteRule ^quiz/([A-Za-z0-9-]+)/([0-9-]+)/?$ quiz/index.php?inv=$1&d=$2    [NC,L]

I also tried removing NC or the whole [NC,L] and it made no difference.

I also tried both + and * after the first [ ] block, but that didn’t make a difference.

The .htaccess file has no other relevant rules that could be conflicting.

Why are the non-working ones not working?

TIA

2

Answers


  1. With your shown samples/attempts, could you please try following. Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(quiz)/([w-]+)/(d{4}(?:-d{2}){2})/?$ $1/index.php?inv=$1&id=$2 [NC,L]
    
    Login or Signup to reply.
  2. You may also try a negated character class like [^/] that matches anything but /.:

    Options -MultiViews
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(quiz)/([^/]+)/([^/]+)/?$ $1/index.php?inv=$1&id=$2 [NC,L,QSA]
    

    This assumes that there is no .htaccess inside quiz/ folder. You can try this rule inside quiz/.htaccess:

    Options -MultiViews
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([^/]+)/?$ index.php?inv=$1&id=$2 [L,QSA]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search