Reputation:
I have a link in my index.html:
href="my_page.php?cat=14&p=1";
Now, how can I rewrite this so that it looks like this:
/my_page/14/1
I have this so far, but I don't know how to add the ending '/1' to this:
RewriteRule ^kategori/([0-9_]+)$ browse_cat.php?cat_gr=$1 [NC]
Thanks
Upvotes: 1
Views: 179
Reputation: 655549
If you’re actually using URLs like /my_page.php?cat=14&p=1
and want them to be “displayed” as /my_page/14/1
, you will need two rules: One to redirect /my_page.php?cat=14&p=1
externally to /my_page/14/1
and one to rewrite /my_page/14/1
internally to /my_page.php?cat=14&p=1
.
RewriteCond %{THE_REQUEST} ^GET\ /my_page\.php\?
RewriteCond %{QUERY_STRING} ^cat=([0-9]+)&p=([0-9]+)$
RewriteRule ^my_page\.php$ /my_page/%1/%2 [L,R=301]
RewriteRule ^my_page/([0-9]+)/([0-9]+)$ my_page.php?cat=$1&p=$2 [L]
Note that this rule does only work with this specific URL (number and order or parameters). A more general rule will get more complex; using a higher level language like PHP is probably easier in this case.
Furthermore, it would be better if you already use the “right” URL in your HTML documents rather than redirecting/rewriting every request twice.
Upvotes: 0
Reputation: 2992
ReWriteCond {%REQUEST_URI} ^/my_page/([0-9]*)/([0-9]*)
ReWriteRule .* /my_page.php?cat=%1&p=%2 [NC,L]
This would only work if both cat and p at a minimum are specified.
Upvotes: 0
Reputation: 64259
Have no apache installed here at home, but this should do the trick.
RewriteRule ^kategori/([0-9_]+)(?:/(0-9]+))?$ browse_cat.php?cat_gr=$1&p=$2 [NC]
(?: ...)? makes the second part optional. So /kategori/14 as well as /kategori/14/1 should work.
If not, you'll need two rules
RewriteRule ^kategori/([0-9_]+)$ browse_cat.php?cat_gr=$1 [NC]
RewriteRule ^kategori/([0-9_]+)(?:/(0-9]+))?$ browse_cat.php?cat_gr=$1&p=$2 [NC]
Upvotes: 2