Advanced Htaccess Rewrite Rule For PHP
I cu开发者_JAVA百科rrently 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 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
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.
精彩评论