dinkode.dk
dinkode.dk

Reputation: 57

PHP URL Rewrite two parameters

Is it possible to have an url like www.domain.com/admin/mail-sent which actually would be www.domain.com/admin/mail.php?category=mail&folder=sent?

I have this set up in my .htaccess:

AddDefaultCharset UTF-8
RewriteEngine On

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

RewriteRule ^([a-zA-Z0-9-]+)$ mail.php?category=$1folder=$2

.. and I don't know if it's right. I've tried with just the folder as the URL which was fine, but I really want the url to be like /mail-sent instead of just /sent.

Is this even possible?

Have a wonderful day :)

Upvotes: 0

Views: 564

Answers (2)

Prix
Prix

Reputation: 19528

Yes, it is:

RewriteRule ^admin/([^-]+)-([^/]+)/?$ mail.php?category=$1&folder=$2 [L,NC]

([^-]+) anything not a dash

([^/]+) anything not a / after the first dash

You can also do it more strictly like:

RewriteRule ^admin/(mail)-(sent)/?$ mail.php?category=$1&folder=$2 [L,NC]

Upvotes: 3

ntaso
ntaso

Reputation: 614

Sure, it's possible. Just rewrite your regex to watch out for -. With parenthesis you can define matching groups:

RewriteRule ^([a-zA-Z0-9]+)\-([a-zA-Z0-9]+)$ mail.php?category=$1folder=$2

$1 refers to first parenthesis, $2 to second.

Upvotes: 0

Related Questions