url Change in core php - 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

Related

How to change to directory when I click on link in PHP?

I want to run the hello.php from input/index.php to input/output/hello.php when the link on index.php was clicked. I have googled it but cannot find the solution? Any solution for this?
Just put a / in the URL.
<a href="output/hello.php">
Sometimes, i have a problem when just using (with your example) "output/hello.php" so i use "./output/hello.php", maybe it could work for you.
What you are asking I think is more about URLs, you have to create a file called:
.htaccess
Just like that, with the dot at the beginning. You can use this code inside of it:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php?u=$1
Now, all the requests are going to go to the index.php file, ALL!! no matter if you write /lalala, is still going to go to index.php, so you have to parse the URL, that way, you can know what are you receiving and be able to execute any method that you want.
Example:
$url = 'http://' . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
$parsedUrl = parse_url($url);
$parsedUrl['path'] // Is the var that has the info after yourdomain.com/
Once you have that, you can separate that with explode('/', $parsedUrl['path']) and analize the data.
So you can write /input/output/hello.php and you can receive the data in index.php

how to Change a GET method URL to clean URL in 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];

PHP Front Controller to get the Contents using HTACCESS file

I was trying to get make pretty URL. I have added a front end controller which gets the URL
and here is the Code for HTACCESS file and controller
RewriteEngine on
RewriteCond $1 !^(index\.php|search\.php|lib|css|ajax|includes|js|classes|parts|pages|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Controller file
$URL = $_SERVER["REQUEST_URI"];
$all_parts = explode("/", $URL);
My URL is something like
www.example.com/pretty-ulr/10.html
I get the page id which is page.php?p_id=10 (This is a simple example. I am using more complex example. )
But I am not sure how to get the contents here or redirect t0 page.php?p_id=10 to get show this page contents. When I redirect using header(), It does not get there
May be something small in .htaccess file, but I am not sure. Any idea please how to get the page contents.
I recommend using something like this in .htaccess:
RedirectMatch ^/$ /index.php
Then in php do:
$url=parse_url($_SERVER['REQUEST_URI']);
$urls=explode('/',rawurldecode(trim($url["path"],'/')));
new Controller($urls);
The class Controller would parse the urls and get the right view.

Creating dynamic URLs in htaccess

I'm trying to write an .htaccess file that will make my URLs more attractive to search engines. I know basically how to do this, but I'm wondering how I could do this dynamically.
My URL generally looks like:
view.php?mode=prod&id=1234
What I'd like to do is take the id from the url, do a database query, then put the title returned from the DB into the url. something like:
/products/This-is-the-product-title
I know that some people have accomplished this with phpbb forum URLs and topics, and i've tried to track the code down to where it replaces the actual URL with the new title string URL, but no luck.
I know I can rewrite the URL with just the id like:
RewriteRule ^view\.php?mode=prod&id=([0-9]+) /products/$1/
Is there a way in PHP to overwrite the URL displayed?
At the moment you're wondering how to convert your ugly URL (e.g. /view.php?mode=prod&id=1234) into a pretty URL (e.g. /products/product-title). Start looking at this the other way around.
What you want is someone typing /products/product-title to actually take them to the page that can be accessed by /view.php?mode=prod&id=1234.
i.e. your rule could be as follows:
RewriteRule ^products/([A-Za-z0-9-])/?$ /view.php?mode=prod&title=$1
Then in view.php do a lookup based on the title to find the id. Then carry on as normal.
One way to do it, would be just like most mvc frameworks. You can redirect all your pages to the same index.php file, and you use your script to determine which page to load.
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
and your php file will have a script like this one:
// get the url
$uri = (isset($_SERVER['REQUEST_URI']))?$_SERVER['REQUEST_URI']: false;
$query = (isset($_SERVER['QUERY_STRING']))?$_SERVER['QUERY_STRING']: '';
$url = str_replace($query,'',$uri); // you can edit this part to do something with the query
$arr = explode('/',$url);
array_shift($arr);
// get the correct page to display
$controller =!empty($arr[0])?$arr[0]:'home'; // $arr[0] could be product/
$action = isset($arr[1]) && !empty($arr[1])?$arr[1]:'index'; // $arr[1] can be product-title
}
of course you will have to work this code to fashion your application
I hope this helps
One way would be to output a Location: header to force a redirect to the chosen URL.

how do I create the following .htaccess file

I have written the following code in my .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteRule page/(.*)/ index.php?page=$1&%{QUERY_STRING}
RewriteRule page/(.*) index.php?page=$1&%{QUERY_STRING}
The url "xyz.in/index.php?page=home" will look like this in the address bar of browser "xyz.in/page/home"
If I want to pass a variable through URL than I will have to write as "xyz.in/page/home?value=1" or "xyz.in/page/home?value=1&value2=56&flag=true"
The initial part of url (xyz.in/page/home) is clean(search engine friendly), but if I pass some more variables in the url then it doesn't look nice.
I want to make this url like
"xyz.in/page/home/value/4/value2/56" and so on.
The variables value and value2 are not static they are just used for example over here. Name can be anything.
Is it possible to do this ?
Please help me form the ".htaccess" file
(any corrections related to title or language or tags used in this question are welcome)
Thanks
The easiest would be to parse the URL path with PHP. Then you would just need this rule to rewrite the requests to your PHP file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^index\.php$ index.php [L]
The condition will ensure that only requests to non-existing files are rewritten.
Your PHP script could than look like this:
$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', ltrim($_SERVER['REQUEST_URI_PATH']));
for ($i=0, $n=count($segments); $i<$n; $i+=2) {
$_GET[rawurldecode($segments[$i])] = rawurldecode($segments[$i+1]);
}

Categories