开发者

.htaccess Rewrite for clean URLs w/ 2 get variables

My current .htacess looks like this:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9-z\-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-z\-]+)/$ index.php?page=$1

which allows me to take a a link such as

http://www/myserver.com/somepage 

and rewrite it into

http://www.myserver.com/?pag开发者_如何学Pythone=somepage.

but how would i make it go deeper? I want to be able to do:

http://www.myserver.com/somepage/subpage

and turn it into

http://www.myserver.com/?page=somepage&subpage=subpage

Thanks


Add the following rules with additional capturing group for the second parameter

RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)$ index.php?page=$1&subpage=$2
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)/$ index.php?page=$1&subpage=$2


If you have the potential to catch links that do not have subpages, then you'd want to catch the subpages with the first rule. Then you can catch the rest and send to page.

RewriteEngine On
# catch potential subpages first
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)/?$ index.php?page=$1&subpage=$2 [L]
#combines your original rule to allow for optional end slash
RewriteRule ^([a-zA-Z0-9-z\-]+)/?$ index.php?page=$1 [L]

You could combine them into one rule, but it would potentially generate queries with an empty subpage. I prefer the cleaner internal queries, but for arguments sake:

RewriteEngine On
# One rule to rule them all
RewriteRule ^([a-zA-Z0-9-z\-]+)(/([a-zA-Z0-9-z\-]+))?/?$ index.php?page=$1&subpage=$3 [L]

Edit: Throwing in this link to a rewrite tester. Very useful. Although, when I tested my rules, it brought up an issue with your original [] grouping. What is the -z- at the end for?


RewriteEngine On
RewriteRule ^([a-zA-Z0-9-z\-\_]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)$ index.php?page=$1&id=$2
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)/$ index.php?page=$1&id=$2
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)$ index.php?page=$1&id=$2&value=$3
RewriteRule ^([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)/([a-zA-Z0-9-z\-\_]+)/$ index.php?page=$1&id=$2&value=$3
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜