Reputation: 387
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/?page=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
Upvotes: 0
Views: 6008
Reputation: 3
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
Upvotes: 0
Reputation: 1532
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?
Upvotes: 0
Reputation: 2332
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
Upvotes: 2