how to Change a GET method URL to clean URL in php? - php

This is an URL that created by GET method in php to send DATA parameter to archives.html page:
http://127.0.0.1/archives.html?option=com_archive&date=16-2-2014
Is there any way to clean this URL?
I want to do something like this (and I can send parameter too!)
http://127.0.0.1/archives.html/16-2-2014
Can I do that by mod_rewrite?
(I am developing a component on joomla3)

You can use this rule in your root .htaccess:
RewriteEngine On
RewriteRule ^(archives\.html)/([0-9-]+)/?$ /$1/?option=com_archive&date=$2 [L,NC,QSA]

RewriteEngine On
RewriteRule ^([^/]*)$ /archives.html?option=com_archive&date=$1 [L]
This htaccess would do this job for you
by url i assume this is joomla
this doc might help you
http://docs.joomla.org/Enabling_Search_Engine_Friendly_(SEF)_URLs_on_Apache

One way to do this in PHP:
$original_url = 'http://127.0.0.1/archives.html?option=com_archive&date=16-2-2014';
$new_url = explode('?', $original_url);
$clean_url = $new_url[0];

Related

Extract URL slug using PHP. Building a URL shortener

I'm building a URL shortening web app using PHP. I am able to generate shorter URLs successfully. But I'm not able to redirect the users when they visit the shortened URL.
If the user enters https://example.com/aBc1X, I'd like to capture the aBc1X. I'll then query the database to find the original URL and then redirect.
My question is, how can I extract the aBc1X from the above URL?
P.S. I'll use either Apache or Nginx.
Two things to do for you.
First you have to redirect all traffic to one file which will be your router file. You can do this by placing a few rules in .htaccess file. I will put there some generic rules to start with (this one come from Wordpress):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^redirect\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /redirect.php [L]
</IfModule>
They tell that everywhere url points to which isn't file or directory will run file redirect.php. You may want to tweak that settings to your needs.
Then in redirect.php you can capture url by looking inside $_SERVER['REQUEST_URI'].
For url http://example.com/any-url-i-want you would have
$_SERVER['REQUEST_URI'] == '/any-url-i-want.
Now the only thing you need is to find the url in database, and do a redirect.
I guess you can handle string operations at this point, either by using parse_url, regular expressions, or simple string cutting.
You want to use;
$_SERVER['REQUEST_URI'];
That will return what you're looking for.
You can see the documentation here
To parse the url
$url = "https://example.com/aBc1X";
$path = ltrim(parse_url($url, PHP_URL_PATH), '/');
Then $path will be aBc1X as desired. Note that any query following a ? will be omitted in this solution. For more details have a look at the documentation of the parse_url function.

url Change in core php

I am use core php and i want to change url.
This is my actual URL.
http://www.example.com/blogs/blog_single.php?title=Need-of-a-Professional
and i want that type of url:
http://www.example.com/blogs/Need-of-a-Professional
so what i do?
Please help me, How i can achieve this?
Add the following to your .htaccess:
RewriteEngine On
RewriteRule ^blogs/([^/]*)\.html$ /blogs/blog_single.php?title=$1 [L]
You can use online url rewrite tools to generate custom urls such as this one.
This is a get request and title is send in it.
To change it you have to change the method of form.
There will be other changes too as by doing this your previous code will not work properly.
What I have done is on the basis of url path(condition) i have called the controller(means the page which i have to call in result):
Like in this code line on index.php $ctrlpath is the path upto the controller then $page is the page which i have to call one the basis of condition :-
$url_parts = parse_url(preg_replace('/\/{2,}/', '/', $_SERVER['REQUEST_URI']));
$url_path = $url_parts['path'];
/*This I have done in router.php*/
if($url_path == '/saving-calc/'){
$page = 'tools/saving-calc';
}
require_once($ctrlpath.$page.'.php');
Then on your own controller you an append the title of your page in the url.
You try this..
$url = explode('/', $_SERVER['REQUEST_URI']);
//breaking the url and storing it as array based on '/'
array_pop($url);
// last part of the url is removed from array in you case 'blog_single.php?title=Need-of-a-Professional' removed from array
array_push($url,'Need-of-a-Professional');
// pushed at last index
echo implode('/', $url); //converting array into url(string)
modify your .htaccess file with below.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^blog/(\d+)*$ ./blog_single.php?title=$1
You can generate htaccess by putting the dynamic url.
generate from here.
https://www.webconfs.com/web-tools/url-rewriting-tool/
Yes,You will change easily URL manually.
Example:
Click Me

How to access query string parameters after "Cleaning" your PHP REST API?

To give some context, I initially started out with my api looking like this:
http://myserver/api.php?action=projects
In my api.php file I have a simple if statement to detect the action, and then react accordingly:
if (isset($_GET["action"])
{
switch($_GET["action"])
{
case "projects"
dostuff();
break;
case "otherstuff"
dootherstuff();
break;
...
}
}
However, I of course wanted to clean this up, so I could go to this url:
http://myserver/api.php/projects
and get the same outcome. I was able to accomplish this by reading up on a previous SO post:
Using Clean URLs in RESTful API
And ultimately got it working by making an .htaccess file in my root directory and using the rewrite engine like so:
RewriteEngine On
RewriteRule ^action/([^/\.]+)/?$ api.php?action=$1 [L]
This worked great! Except for one problem, because I am using the clean URL, it no longer seems to recognize the GET parameter exists.
I've found I can use
$_SERVER['REQUEST_URI'];
And then parse the URL to get my parameters. I don't see how this could work out though once I start introducing multiple parameters. Is there something I'm missing here, or do I need to look at it a different way and change how I format the parameters instead?
You can use these rules:
RewriteEngine On
RewriteRule ^(?:action|api\.php)/([^/.]+)/?$ api.php?action=$1 [L,NC,QSA]
Change to this:
RewriteEngine On
RewriteRule ^action/([^/\.]+)/?$ api.php?action=$1 [L, QSA]
You need the QSA parameter to keep the querystring intact. So now this is possible:
/action/projects/?my=name
Or you can do this:
RewriteEngine On
RewriteRule ^action/([^/\.]+)/([^/\.]+)/?$ api.php?action=$1&method=$2 [L, QSA]
RewriteRule ^action/([^/\.]+)/?$ api.php?action=$1 [L, QSA]
Now you will have these urls:
/action/projects/add/
/action/projects/

url php multi method get using .htaccess

if i have a url like:
domain.com/profile.php?id=1&lang=en
Can i get a url like this using htaccess?
domain.com/1/en
thanks
This is how i would do it in .htaccess
RewriteEngine On
RewriteRule ^/1/en$ /profile.php?id=1&lang=en

how can I rewrite URL to fancy url?

when my user logs in, I want the get variables that were sent rewrote onto the URL like so:
http://mysite.com/mygetvar1/mygetvar_value1/mygetvar2/mygetvar_value2/
or
mysite.com/mygetvar1=mygetvar_value1/mygetvar2=mygetvar_value2/
How can I do this?
Please help! Thanks!
Codeigniter can offer you like that. Many other PHP frameworks offer that as well.
Try this.
RewriteRule /([^/]*)/([^/]*)/([^/]*)/([^/]*)/ /login.php?$1=$2&$3=$4 [R=301]
RewriteRule /([^/]*)=([^/]*)/([^/]*)=([^/]*)/ /login.php?$1=$2&$3=$4 [R=301]
First you need the mod_rewrite module enable.
After, put this in your config file :
RewriteEngine on
RewriteRule ^ads/(rims|tires|combo)/([0-9]+)/(.+).html /ad.php?noAds=$2 [L,QSA]
This is a example.
Your url will look like : http://www.yourwebsite.com/ads/rims/331/title.html
but you will call the url : http//www.yourwebsite.com/ad.php?noAds=331
For your regex , you should use a site like http://www.rubular.com
You can use the .htaccess file or put in directly in the httpd.conf file

Categories