Rewrite URL with multiple parameters php - 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

Related

I want to make my long reference link dynamic with htaccess

First of all, I have a link that takes 2 parameters;
http://localhost/project/home.php?SK=2&referance=1
?SK=2 is my first parameter and &referance=1 is my second parameter. The SK=2 parameter draws my files in the php file, and the referance=1 parameter prints the value from the get method to my registration page. What I want is to put this link in below format with htaccess
http://localhost/project/register?referance=1
The link I made before brings my register page, but now I want it to bring the reference code, but unfortunately it doesn't.
Htaccess code I used before;
RewriteRule ^register$ home.php?SK=2 [NC,L]
RewriteRule ^register$ home.php?SK=2 [NC,L]
You just need to add the QSA (Query String Append) flag to your existing rule.
For example:
RewriteRule ^register$ home.php?SK=2 [QSA,NC,L]
This will now rewrite /register to /home.php?SK=2 and /register?referance=1 to /home.php?SH=2&referance=1.
Reference:
http://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa

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

How to convert custom url wordpress to friendly url

I have one problem, Once page in WordPress need a parameter in $_GET.
I got URL,
example: homepage.com/something/?param1=someString,
I would like to get homepage.com/something/someString <--- someString would like to get in table $_GET. I tried by htaccess but it's not working.
RewriteCond %{REQUEST_URI} ^something/ [NC]
RewriteRule ^/something/([a-z]+)/?$ /something/?param1=$1 [L]
Additional when I tried homepage.com/something/someInteger It's going to the correct page, "something" but when I tried homepage.com/something/someString <---
It's going to 404 page, why?
Somebody any ideas?

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.

How to get the third string from URL using php

I am using below code in htaccess
RewriteRule ^/?service/abc/([\w-]+)/?$ xyz.php?name=$1 [L,QSA,NC]
RewriteRule ^/?service/pqr/([\w-]+)/?$ xyz.php?name=$1 [L,QSA,NC]
to display my URL something like:
http://example.com/service/abc/review
http://example.com/service/pqr/review
which is working for me. Now my issue is, I have to get abc or pqr a string from URL and display
I am trying to get the name like
$getURL=explode('/', $_SERVER["REQUEST_URI"]);// getting service/abc/review
echo $a = next($getURL); //it's displaying only service
Or is there any best way?
Woult you help me out ?
Use one rule, allow for either abc or pqr in that position, and append it as an additional GET parameter:
RewriteRule ^service/(abc|pqr)/([\w-]+)/?$ xyz.php?service=$1&name=$2 [L,QSA,NC]
And use $_GET['service'] to access the value then.
you can edit the htaccess rules in this way
RewriteRule ^/?service/(.*)/(.*)/?$ xyz.php?endpoint=$1&name=$2 [L,QSA,NC]
and retrieve them in xyz.php file
var_dump($_GET);

Categories