Eric
Eric

Reputation: 4301

htaccess rewriterule for grabbing everything past hostname

I want an htaccess rewrite rule to grab everything past the host name and use it as an argument. For example, I want http://example.com/one to be handled by http://example.com/category.php?cat=one

I think it should be simple, but I can't quite find the combination. Thanks

Upvotes: 0

Views: 369

Answers (2)

Eric
Eric

Reputation: 4301

This is what I came up with:

RewriteRule ^/?([^\./]*)[:;,\.]*$ category.php?cat=$1 [L,NS]

Anybody see any way to improve it?

Upvotes: 1

Tim Stone
Tim Stone

Reputation: 19189

Try something like this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ category.php?cat=$0 [B]

If you want to pass the query string along too, be sure to change the B flag to B,QSA, but be aware that someone could then pass the GET argument cat, which would override what you set in the rewrite. There are some workarounds to that situation if you need them, but otherwise that should do it for you.

Upvotes: 0

Related Questions