简体   繁体   中英

Advanced Htaccess Rewrite Rule For PHP

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM