URI Rewrite with fake subdomains and multiple variables

.htaccessapache-2.2mod-rewrite

Can someone please help with trying to use mod rewrite so foo.domain.com is rewritten to domain.com/p.php?s=foo and foo.domain.com/bar to domain.com/p.php?s=foo&p=bar?

Currently my .htaccess is:

RewriteEngine On

# Remove www
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

# Rewrite subdomain etc.
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/?$ p.php?s=%1&p=$2 [QSA,L,NC]

But I can't work out how to grab the second variable (being optional and after a slash at the end of the URI. I've tried changing the end of the condition to ?/(.*)$, but to no avail and my mod rewrite skills are certainly naff!

Best Answer

That stops the weird redirect but doesn't remove the slash from the URI (I'm thinking from an SEO perspective in that multiple URIs for duplicate content are bad :) ) does it need a redirect? I tried adding the R=301 flag but that didn't work. Any ideas?

(Sorry for adding answer, "add comment" button has literally disappeared!)