How to make query string look better in the URL with PHP? - php

Now, this is how my URL looks:
onlineproject/search-category-products.php?cat_id=3
I want to show somewhat like this:
onlineproject/search-category-products.php/vegetables
Is there any way to achieve it?

Add or modify the ".htaccess" in your server so it looks like
Rewrite On
RewriteRule onlineproject/search-category-products.php/^(.*)$ onlineproject/search-category-products.php/?cat_id=$1 [L]
That way your url would look like
onlineproject/search-category-products.php/vegetables
But the request would be redirected to
onlineproject/search-category-products.php/?cat_id=vegetables
This way it looks prettier, but the mapping with the categories and their ids must be handled by the server.

Related

Clean url in php using htaccess

Clean url in php using htaccess
Here is an example
http://www.example.com/Mobiles/index.php?idd=4
and i want result like this
http://www.example.com/Mobiles
Please help
This is called URL Rewrite. If your page links are dynamic, like extracting data from database which is mostly the case in e-commerce sites, then the best approach is to append the id at the end of URL. This way you can fetch the data from database. Like in your case, your new URL might look like:
http://www.example.com/Mobiles/4
When user will visit this link .htaccess file will internally rewrite this URL to:
http://www.example.com/Mobiles/index.php?id=4
In this way you can then retrieve id from your PHP like this:
$id = $_GET['id'];
or:
extract($_GET);
This extract function will create the variables automatically from the parameters name and you can access it directly with $id variable.
Here is the .htaccess code:
RewriteEngine On
RewriteRule ^Mobiles/(\d+)$ http://www.example.com/Mobiles/index.php?id=$1
In case if you don't need URL like http://www.example.com/Mobiles/4, then use this:
RewriteEngine On
RewriteRule ^Mobiles$ http://www.example.com/Mobiles/index.php?id=4

.htaccess get var like a folder

I'm new here and I need help with "transform" a $_GET['var'] like folder
For example I have a website with languages, the url is like this:
www.mywebsite.com/index.php?lang=es&p=1
www.mywebsite.com/index.php?lang=en&p=1
then I want something like this:
www.mywebsite.com/EN/index.php?p=1
is like "transform" the $_GET['var'] in that.
I don't have a directory with languages... then I need that whenever there is lang = en this change is made in the url
is this possible?
Thanks you!
As long as you know what languages your site can be displayed in, you can use the following:
RewriteRule ^(en|se|fr|du)/(.*)$ $2?lang=$1 [L,QSA]
The QSA flag will append the current query string to the existing query string. The rest should be obvious.

htaccess rewriting issue

Working on a website, and having some problems with .htaccess.
I do have a category link, which is like this:
sharing/index.php?cf=category&id=$1
I did a rewrite on it to:
sharing/cat-([^/]+)$
which works very well, then I added a pagination to the script, and pagination in the link is like this:
sharing/cat-1?next=1
I would like to make it like this:
sharing/cat-1/page/1
I have tried my best, but it didn't work out. Please note cat-1 is for the category ID meaning in another category it may be cat-3.
The error comes from the fact that you haven't escaped the forward-slash.
So you should try this for the RewriteRule
RewriteRule ^sharing\/cat-([^\/]+)\/page\/([0-9]*)$ sharing/index.php?cf=category&id=$1&next=$2
And the link for the pagination, should be like this:
sharing/cat-1/page/1
not the one you mention (sharing/cat-1?next=1)
Remember that if your categories are only numbers, it might be better to change ([^\/]+) by ([0-9]*)
Try the following example, where I'm passing two variables:
RewriteRule ^player/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ players/view.php?player=$1&page=$2 [NC,L]
For extending it, you just need to add ([A-Za-z0-9-]+)/ to the URL, and pass another parameter in the other side.

How to rewrite url using mod_rewrite

My urls currently look like this:
http://domain.com/news/articles.php?id=22&category=investments&title=securing-your-future-making-the-right-investment
How can I use mod_rewrite to make the url look more like this:
http://domain.com/news/articles/investments/securing-your-future-making-the-right-investment
EDIT: need to include the id variable too
#enable mod rewrite
RewriteEngine On
RewriteRule ^/news/articles.php?id=([0-9]+)&category=([a-zA-Z]+)&title=([a-zA-Z]+)$ /news/articles/$2/$1_$3
the ID must exist in the URL so the it looks like:
http://domain.com/news/articles/investments/{ID}_securing-your-future-making-the-right-investment
Good luck.
Add something like this to your .htaccess file:
^/news/articles/([0-9]+)/(.*)$securing-your-future-making-the-right-investment$
/news/articles.php?id=$1&category=$3s&title=$2 [L]
Don't know about the $3, but category doesn't seem to be present in the url you listed so I guess it isnt needed.
This should make it work ;)

htaccess URL rewrite

Having a bit of trouble with htaccess, at the moment I have a rewrite rule that replaces the query string into a folder structure
RewriteRule profile/id/(.*) u_profile.php?id=$1
This spits out /profile/id/1/ which is perfect, although, I'd really like to add another variable in there to also get the users name too.
At the moment I have a $_SESSION['username']; set for everyone who's logged in, I want to really pass it to the query string like so,
/profile/john/id/1/
/profile/sarah/id/1/
etc
I think this is what you're looking for:
RewriteRule profile/(.*)/id/(.*) u_profile.php?person=$1&id=$2
Then you can access the name through $_GET['person'] and compare it to $_SESSION.
To remove the id part, you just can remove /id
It would look like this:
RewriteRule profile/(.*)/(.*) u_profile.php?person=$1&id=$2

Categories