Removing query string from url in php - php

Lest say i have a link and when some one click on that link it redirect the person to the following page
http://localhost/install.php?act=go
now i just want to remove everything after ? so that person doesn't see the actual query string behind it or is there any way to encode the characters and make it look different in browser

You can add a mod_rewrite rule to your htaccess something like
RewriteRule ^install/([A-Za-z0-9-_]+)$ /install.php?act=$1
that way /install/go takes you to the destinantion

Related

How to differentiate between a url with and without parameters using .htaccess

I don't even know how I could title this question but here is the problem:
I use seo friendly urls on this project where there are services. Service links look like; .com/services/service-title-6 and they get translated to; .com/services.php?s=service-title-6. On the other hand, pages don't have the .php extension at the end of their urls like: .com/services or .com/about-us.
The actual problem comes when I'm handling the switch between multiple display languages for this website. I just used a get parameter called lang and I can pass it to any page since I have a php file I call on every page to handle this along with other stuff. On my navigation bar, the language change button links are created dynamically so that the user doesn't have to leave the current page to change the website's language. So if you are viewing the about page, the link is: .com/about-us?lang=en to switch to English. This is generated in php like $current_url . "?lang=en".
However, when I'm viewing a page with parameters such as a service, the link gets translated by .htaccess to something like: .com/services.php?s=service-title-6?lang=en. As you can tell, the 2nd question mark will get ignored as a parameter and not be passed. I would need to use an ampersand instead of the question mark. But when I'm just doing this on a regular page, I can't use ampersand by itself as it will be translated to: .com/about-us.php&lang=en.
Sorry for the long question but any help and ideas to handle this problem would be appreciated. Thanks.
I fixed my problem by appending the next query string to my url. I wasn't aware of the QSA flag of mod_rewrite.
Here is my original rule for the services page:
RewriteRule ^services/(.*?)/?$ services.php?s=$1 [L]
I replaced it with:
RewriteRule ^services/(.*?)/?$ services.php?s=$1&lang=$2 [L,QSA]
I don't know if it's 100% reliable but it seems to work perfectly for what I need, even after doing a few tests.
(Thanks to Joseph Sible-Reinstate Monica for directing me to a helpful question.)

htaccess - Friendly url with redirection

I have been looking for several solutions on the web but as I have always had problems interpreting the htaccess, I have not achieved what I wanted.
I have a website with the following type of link:
https://sub.example.com/search.php?gosearch=new+music+techno
https://sub.example.com/search.php?gosearch=nice-music-rock
And I would like to obtain a link in this way:
https://sub.example.com/search/new-music-techno
I also have a problem, when in my form I press enter, this automatically adds the "+" symbol to all the spaces and I do not know with what method the change would be made automatically to a "-", if with PHP, with JQuery or with .htacess.
In parallel I also have this type of links:
https://sub.example.com/topmusic.php
And I would like to obtain this type of link:
https://sub.example.com/topmusic/
Is it possible to implement everything with htacess?
Do I need to make several conditions or just one?
Is it possible that old urls are redirected automatically to the new form once the user clicks on them?
You can use like this
RewriteRule ^search/(.*)$ search.php?gosearch=$1 [NC,L,QSA]
And on search.php you need to replace the code. htaccess will send the whole string next to "search/" on search.php file under gosearch $_GET parameter.

Url Rewrite via.htaccess

I have a problem with Url Rewrite in my multilingual site, or better, maybe I don't understand it very well.
My original link is something like
www.mydomain.com/index.php?lang=en&s=2
I write this rule:
RewriteEngine On RewriteRule ^([^/]*)/([^/]*)\.html$ index.php?lang=$1&s=$2 [L]
and it's work. In fact if I write in the address bar
www.mydomain.com/en/2.html (smart url)
I go to the correct page with a smarter url on the address bar.
But the problem is that the link on my code is still:
link
and when I click it, the URL on the address bar is the original, not the smart one.
I thought that the rule on htaccess would have automatically "turned" (in the address bar) all my links in something smarter without changing the original php code...but maybe I'm wrong.
If I'm wrong how should I write my links in order to have smart urls but passing variables to identify languages and pages? I'm confused.
Thank you.
After rewriting URL in your .htaccess you need also to change the URL in your page for example in given link link you need to remove question mark
<a href="index.php/<?php echo $lang?>/2">

conditional .htaccess redirect - redirect to two different pages depending on the url

My website functions a little like 9gag or damnlol, etc.
when the user visits an item page, for example: site.com/104-funny-thing, the rewrite rule redirects to view.php?id=104-funny-thing. Here the URL is stripped away of everything after the first '-'. So we are left with the id only, in this case: 104, and the query gets the item.
Here is the Rule:
RewriteRule ^([A-Za-z0-9-]+)$ view.php?id=$1
What I want is that the same URL format could be used to open up user profiles as well. For example: site.com/salman, this URL should open up profile.php?username=salman.
I thought of doing this via PHP. Where I can use the same file, view.php, for both purposes. Once the parsing of the URL is done, if I get a numeric value, I would include the item.php page and query and show data accordingly. If it's not numerical, then I ca include the profile.php page and show the profile instead. This should theoretically work flawlessly, but what I wanted to try was that if there was a way to redirect using the .htaccess directly, I would prefer to use that option, as the previous option requires extra work to sort out some issues that the solution causes.
Someone good at htaccess (regex code?) might be able to offer a solution. Here is some further explanation:
Usernames can contain numbers, but not only numbers
Username URLs cannot contain '-'
Item IDs cannot contain alphabets
URLs 104-funny, 104- and 104, all work for view.php
Thank you for your help in advanced!
#article urls
RewriteRule ^([0-9]+)(-.*)?$ view.php?id=$1
#profile urls.
RewriteRule ^([A-Za-z0-9]+)$ profile.php?username=$1

Redirect Dynamic URL with one parameter

I have a url as mydomain.com/store.php?id=3.
I would like to convert(redirect) to static URL on the base of id.
Replace the ID with the store name (from database) and add some hard coated keyword at the end of url.
URL should be look like this one mydomain.com/storename-keyword1-keyword2.html
Please guide me that what is the best way to do this? I have used a lot of my time to handle this issue but no result.
Thanks
Are you trying to do something like this? How can I make my urls friendly from this example?
That is, you are still serving the content using the script store.php but you want the browser's location bar to have a clean URL that looks like /storename/word/word.html? You're obviously going to need to change some of the rewrite matches and targets to suit the URLs that you want. Something like?
RewriteRule ^storename-([^\-]+)-(.+).html /store.php?id=$1&something=$2&rewritten=true [L]

Categories