question mark causes failure

New to mod_rewrite? This is a good place to start.

question mark causes failure

Postby bencoder » Wed Sep 16, 2009 7:53 am

So I have a rule:

Code: Select all
RewriteRule ^(.*)/(.*)?/?$ /index.php?page=$1&action=$2   [L,QSA]


This works fine, but when I want to make that first slash optional, I add a question mark after it:

Code: Select all
RewriteRule ^(.*)/?(.*)?/?$ /index.php?page=$1&action=$2   [L,QSA]


but this causes an error (500).

Could somebody explain what I'm doing wrong and why it's wrong?

thanks in advance,

Ben
bencoder
 
Posts: 5
Joined: Mon Jan 05, 2009 7:21 am

Postby richardk » Wed Sep 16, 2009 2:40 pm

Without the slash the rule effectively becomes ^(.*)$ and it matches the internal request to index.php and a loop occurs.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)(?:/([^/]+))?/?$ /index.php?page=$1&action=$2 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 31 guests

cron