Get Page Number from Rewrited URL - php

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

Related

how to add pagination in htaccess rewrite rule

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

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

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

URL rewriting in PHP using htaccess file

My URL is
localhost/Interview/php/SEO_URL/01-example/create_user.php
want to change this into
http://localhost/Interview/php/SEO_URL/01-example/CreateUser
want to create a base url like this
localhost/Interview/php/SEO_URL/01-example/
How ?
You can use the following rule in /root/.htaccess :
RewriteEngine on
RewriteRule ^interview/php/SEO_URI/01-example/create_user/?$ /Interview/php/SEO_URL/01-example/create_user.php [NC,L]
This will internally redirect the requestd url :
interview/php/SEO_URI/01-example/create_user
to
/Interview/php/SEO_URL/01-example/create_user.php

How to get values from query string and pass it to new page using .htaccess file

currently I m using following code in my site in .htaccess file :
RewriteRule ^([^/\.]+).php?$ comman.php?cat=$1 [L]
This redirects user to comman.php page say, user requests
http://www.mysite.com/myfolder/page1.php
will redirects to
http://www.mysite.com/myfolder/comman.php?cat=page1
This works fine. My question is how can I achieve following
http://www.mysite.com/myfolder/page1.php?var1=123
to redirect
http://www.mysite.com/myfolder/comman.php?cat=page1&param=123
i.e. whatever value passed to url using get method to add in my new url.
Thanks in Advance.....
You should add the QSA flag:
RewriteRule ^([^/\.]+).php?$ comman.php?cat=$1 [L,QSA]
QSA stands for Query String Append. Anything after the ? in the original URL will be appended to the rewritten URL.
You need to ass QSA to your rule, i.e.
RewriteRule ^(.*)$ /index.php?pageid=$1 [QSA,L]
You can use the QSA flag in your RewriteRule for that.
See the docs.

Categories