how to add pagination in htaccess rewrite rule - php

I'm looking to add pagination with htaccess.
Visible URL:
example.com/test/page2
To:
example.com/test/pagination.php?page=2
Thanks in advance.

Basically you just need to capture the page number and then recycle it into the correctly formatted URL.
RewriteRule ^test/(\d+)/*$ /test/pagination.php?page=$1 [L]
RewriteRule : Type of action
^test/(\d+)/*$ : Regex to run against URL
/test/pagination.php?page=$1 : Replacement URL
[L] : Flags; L == Last
Example:
example.com/test/5 >>> example.com/pagination?page=5

Related

Read parameter using rewriting engine

I have this type of url :
domain.com/articles/title
And i want to rewrite this url in :
domaic.com/articles.php?id=title
I use this rewrite condition:
RewriteEngine On
RewriteRule ^articles/(.*)$ articles.php?id=$1 [QSA,L]
I can show the page articles.php but i try to read value of id's variable with $_GET['id'] but is empty.
Thank in advance for your reply

.HTACCESS - Get the parameter and rewrite

I have a url
website.com/v2/main-service.php?service=foo-bar
and I want to mask it so that the url will become
website.com/v2/foo-bar.html
I have this .htaccess code
RewriteEngine on
RewriteRule ^foo-bar.html$ main-service.php?service=foo-bar
but it's not working. I will appreciate the help. Thank you.
your rule is correct if you remove the "^" before foo-bar.html,
You can add variable and it give something like this :
For this url :
http://website.com/v2/foo-bar.html
The rule :
RewriteRule (v[0-9])+/([a-z0-9-]+).html$ /$1/main-service.php?service=$2
result for php : http://website.com/v2/main-service.php?service=foo-bar
Try this here :
http://htaccess.mwl.be?share=341609bf-eda9-5a5e-92f5-a2cc3f01f921
EDIT : I change rule without index.php

Rewrite URL with multiple parameters php

I have page redirect URL like below with more than one parameters.
http://example.com/search.php?layout=details&Keyword=mobile&view=product&CategoryId=1026&product_id=93477
From that URL i'm taking all parameter values to take a data from database.
So when i load this page i need to show URL most user friendly like below. In that URL should contain only the value of product_id which is i'm using my redirect URL
http://example.com/93477.htm
I have tried to rewrite this URL like below. But in that i couldn't use the parameters "categoryId" and "keyword"
rewrite ^/([a-zA-Z0-9/-]+).htm /search.php?layout=details&view=product&Keyword=$3&CategoryId=$2&product_id=$1 last;
Please anyone help me to done it successfully..
Regards,
VIGNESH KUMAR K
Try this mate ,
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/search\.php\?layout=details&Keyword=([^&]*)&view=product&CategoryId=([^&]*)&product_id=([^&]*) [NC]
RewriteRule ^ /%2/%3/%1? [L,NE,R=302]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /search.php?layout=details&Keyword=$2&view=product&CategoryId=$3&product_id=$1 [L,QSA]
The original URL:
http://example.com/search.php?layout=details&Keyword=mobile&view=product&CategoryId=1026&product_id=93477
The rewritten URL:
http://example.com/93477.htm
Update 1 :
[QSA] has been added in the above code now so that to append the query string in order to retrive
dynamic parameters using PHP $_GET
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa
Update 2:
Edited the .htaccess code to make Keyword and CategoryId Dynamic

Get Page Number from Rewrited URL

i wrote this Rewrite Rule :
RewriteRule ^(products)/(page[0-9]+)?/?$ index.php?action=$1&page=$2 [L]
in following url this will happen :
rewrited url : http://www.silvergroup.ir/products/page2
what actually is : http://www.silvergroup.ir/index.php?action=products&page=page2
but i need the number of page only and whole of page variable is optional, this means following url is allowed :
http://www.silvergroup.ir/products
thank you
Use this rule instead:
RewriteRule ^(products)(?:/page([0-9]+))?/?$ index.php?action=$1&page=$2 [L,NC,QSA]
This will forward
/products to /index.php?action=$1&page=
/products/ to /index.php?action=$1&page=
/products/page2 to /index.php?action=$1&page=2

How to add an optional "param" in a regular expression in .htaccess

I've following 2 rules in my .htaccess file -
1. RewriteRule ^myscript/([A-Za-z0-9]{0,1})\?page=([0-9]?)$ /myscript.php?action=check&var=$1&page=$2 [L]
2. RewriteRule ^myscript/([A-Za-z0-9]{0,1})$ /myscript.php?action=check&var=$1 [L]
so that visitng /myscript/d sends a request as /myscript.php?action=check&var=d
I am trying to add an option page parameter so that visiting /myscript/d?page=5 sends the requests as /myscript.php?action=check&var=d&page=5
to achieve this I tried this
RewriteRule ^myscript/([A-Za-z0-9]{0,1})\?page=([0-9]?)$ /myscript.php?action=check&var=$1&page=$2 [L]
But this rules is being ignored and the request is sent as /myscript.php?action=check&var=d (i.e. 2nd rule from the above is being applied). What am I doing wrong here? What changes do I need to make it get it working?
Thanks for your help.
Use 2nd rule only and add QSA flag:
RewriteRule ^myscript/([A-Za-z0-9]{0,1})$ /myscript.php?action=check&var=$1 [QSA,L]
This will pass existing query parameter to the new URL:
/myscript/d?page=5&say=hello
=>
/myscript.php?action=check&var=d&page=5&say=hello
Apache documentation: http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa

Categories