conmen
conmen

Reputation: 2417

multiple parameters friendly URL rewriteRule

I need help on rewriteRule for multiple parameters as follow:

sitename.com/project.php?t=value1&a=value2

to become

sitename.com/project/value2/value1

but somehow I was unable to resolve the problem and the page shown 500 Internal Server Error

my htaccess file:

DirectoryIndex index.php
Options -Indexes

<Files *htaccess>
Deny from all
</Files>

<files page>
ForceType application/x-httpd-php
</files>

<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

RewriteRule ^cp/edit-agent/([^/\.]+)/?$ cp/edit-agent.php?name=$1 [L]
RewriteRule ^([^/\.]+)/?$ agent.php?name=$1 [L]

#rule to handle example.com/123/sys
RewriteRule ^project/([0-9]+)/([^/\.]+)/?$ project.php?a=$1&t=$2 [L,QSA]
</IfModule>

Please help.

Upvotes: 1

Views: 2753

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

You're rules look ok for the most part, but you have 2 problems. The first and most obvious problem is that you have 2 conditions that are only applied to the first rule:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

is only applied to this rule:

#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

When it also needs to be applied to this rule:

RewriteRule ^([^/\.]+)/?$ agent.php?name=$1 [L]

Conditions only get applied to the immediately following rule, so you need to duplicate them.

The other problem isn't so obvious, but this is probably what's causing the 500 error, is this condition:

RewriteCond %{REQUEST_FILENAME}\.php -f

THe problem is that you have requests like: /project/123/abcd and you have the file /project.php. The %{REQUEST_FILENAME} variable also takes into account PATH INFO, so if you just stick .php to the end, it will actually check: /project.php/123/abcd and PASS the -f check. BUt in the rule itself, you're appending it to the end, thus: project/123/abcd.php. Then the next time around, the same condition passes again, then .php gets appended to the end again: project/123/abcd.php.php, thus infinite loop.

So you need to change your rules to look like:

DirectoryIndex index.php
Options -Indexes -Mutiviews

<Files *htaccess>
Deny from all
</Files>

<files page>
ForceType application/x-httpd-php
</files>

<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

#resolve .php file for extensionless php urls
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.*)$ $1.php [L]

RewriteRule ^cp/edit-agent/([^/\.]+)/?$ cp/edit-agent.php?name=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/\.]+)/?$ agent.php?name=$1 [L]

#rule to handle example.com/123/sys
RewriteRule ^project/([0-9]+)/([^/\.]+)/?$ project.php?a=$1&t=$2 [L,QSA]
</IfModule>

Upvotes: 2

Related Questions