views:

191

answers:

1

I currently have the following code to rewrite search engine friendly url's to something PHP can handle however there are a few issues.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)/(.*) index.php?$1/$2

This Rewrites: domain.com/cat/mens/size/large/

To This: domain.com/index.php?cat/mens/size/large/


It would be ideal if I could rewrite it to something like this using varying amounts of sub directories such as this which is friendlier to the PHP code:

Ideal Rewrite: domain.com/index.php?cat=mens&size=large

Another Example: domain.com/page/value/subpage/subvalue/something/true/

Should Rewrite To: domain.com/index.php?page=value&subpage=subvalue&something=true


Also is there a way to rewrite: domain.com/search/?q=blah+blah

To: domain.com/index.php?search&q=blah+blah

Thanks guys!

+1  A: 

Try this:

RewriteRule /page/(.*)/subpage/(.*)/something/(.*) index.php?page=$1&subpage=$2&something=$3

When the amount of variables in the url isn't predefined it's better to just pass it as a string to the php, and let it handle it (explode, implode, foreach, build an array).

RewriteRule /search/?q=(.*) index.php?search&q=$1

As for what you asked for below:

RewriteRule /cat/(.*)/size/(.*)/?q=(.*)&abc=(.*) index.php?search&cat=$1&size=$2&q=$3&abc=$4

Basically, the first part of RewriteRule is the url given, the second part is the one that goes to the script. Every (somethingInHere) in the first in a variable that you can use in the second one like $1, $2, $3 and $4.

PawelMysior
Matt
With .htaccess or with php handling the string?
PawelMysior
With .htaccess because currently its not passing that part of the string in the rewrite so anything after the last slash the PHP wont see.
Matt
Edited the answer, hope you get it all now.
PawelMysior
What I was trying to achieve was maybe something like this so the number of or names of the directories and query strings was not restricted so work something like this (obv this doesn't work):**RewriteRule (.*)/?(.*) index.php?$1/$2**Thanks!
Matt