How can I use special character (?) in URL - php

I have developed a website using Raw PHP. I have used special character (? and ~) in URL. Example: http://webdeveloperszone.com/?~=portfolio .
Now, I want to rebuild this website using wordpress or codeigniter. I want to keep all the existing link live, because google has cached those links. That means, I have to create links which supports special character (? and ~).
Can any one tell me how can I do this?

PHP
string urlencode ( string $str );
string urldecode ( string $str );
JavaScript
encodeURI(uri);
decodeURI(encodeURI(uri));

For codeigniter
Have a look on
https://ellislab.com/codeigniter/user-guide/general/urls.html
Enable query string
And change
$config['controller_trigger'] = 'c';
To
$config['controller_trigger'] = '~';

In wordpress just you need to create page.
and then change post name from database as you like.
Create page named portfolio.
save and publish it.
then go to phpmyadmin.
open wp_posts.
get the record for perticular page.
and change post_name to your slug ?~=portfolio.
That's it.

Related

PHP (Change the pageid to the page name.)

I am making the dynamic website in PHP. While code is running good using pageid.
The url is now like www.google.com/pageid=1
Now, i want to change the url for 1 level pages as www.google.com/page1
for level 2 as www.google.com/page1/page2
for level 3 as www.google.com/page1/page2/page3
While unique address is stored in the my table as page1,page2,page3.
How can it be possible to change url at run time. Please give the examples and comments. So that it may helpful to understand.
Also i want to know if it is possible using the .htaccess file. If possible How .htaccess %{REQUESTED_FILE}% will reach out for my unique url's that are stored in the database.
$URL= "www.google.com/page1"
1) split the string with '/'
$data = explode("/",$URL);
$data[1] will have page1 value
2) replace the string page with ""
$pageId = str_replace("page","", $data[1]);
3) whatever is left is the page id
pageId has the value now.
Use that to query the database.
Another way is to use regex to extract the number from page1 and use it where-ever you want to use it.

Display if URL matches database characters

Let me first give you a little background to explain what I'm trying to do. My websites use URL's that look like this: MySite/World/Isthmus_of_Panama
I'm working on a major upgrade (and may eventually upgrade further by switching to a CMS, like Drupal or WordPress), and it sounds like the general consensus is that URL's with hyphens are better than underscores. So I'm changing my URL's to MySite/World/Isthmus-of-Panama. In the meantime, I'm also trying to figure out if I should change my URL's to all lower case, and what about special symbols like accents or parentheses?
And what if someone typed in a URL that looks like MySite/World/Isthmus of Panama ? Wikipedia has a script that automatically converts the spaces to underscores. It will also default to the correct URL if you use the wrong case.
Of course, if I change my URL's, I'll also have to forward visitors from my old URL's. It's getting very confusing.
Then I realized that I could cover all of the bases with a script that accepts any URL that matches the characters in my database, 1) regardless of case, 2) and regardless of whether multiple words are separated by hyphens, underscores, spaces or %20. So imagine the following URL's:
MySite/World/Isthmus-of-Panama
MySite/World/Isthmus of Panama
MySite/World/Isthums%20of%20Panama
MySite/World/isthumus_of_panama
MySite/World/Isthmus-of_PANAMA
Where the database value is Isthmus-of-Panama.
Below is one of my queries, where $MyURL = the database value URL (e.g. Isthmus-of-Panama). Can anyone tell me how to modify it so that all of the above URL's will be accepted, with the page then defaulting to the database value?
Wikipedia has a similar feature. If you go to their article about Crazy Horse, then replace the URL Crazy_Horse with crazy_horse or Crazy Horse, it will default to Crazy_Horse. Thanks.
$sql= "SELECT COUNT(URL) AS num FROM gs_reference
WHERE URL = :MyURL";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':MyURL',$MyURL,PDO::PARAM_STR);
$stmt->execute();
$Total = $stmt->fetch();
switch($Total['num'])
{
case 1:
// DISPLAY A PAGE
break;
case 0:
// 404 NOT FOUND ERROR
break;
default:
// DUPLICATE RESULTS
break;
}
I would convert input, example Isthums%20of%20Panama, to the database value in php.
If the converted value is equal to the input one then don't do a 301 redirect to the url with the converted text else do one
EDIT
I would create in database a column slug (generally called like this) which contain the text normalized (ascii character and -) and create an unique index on it
You could use this function to generate the slug in php: PHP function to make slug (URL string)

How to format url for database query

I'm new here I and need your help.
I started playing with .htaccess and php and I came across a problem.
When doing url rewriting, I pass into the url a string which is the title of an article extracted from the database. The problem is that the string(title) has a lot of characters who in the url are misspelled. EG: localhost/news/php%5%is%out
Here % are the blank spaces. I tried to format every title with preg_replace and I replaced every space with '-' but there are a lot of characters and I wanted to know if there is any way of doing this without preg_replace so any string can be good for a query.
In news.php I get the string from the url and I use it for the query in the database from which I extract the body of the entire article.
RewriteRule ^news/([a-zA-Z0-9._]+)/?$ /news.php?news_title=$1
This is my .htaccess file so in the news.php i get the 'news_title' variable through $_GET and then query the database to find the articol with this title.
So my question is, am I doing this all wrong? Is there any other way of doing this? I started working with htaccess only 2 days ago and I want to make my urls more friendly.
I hope my question is clear.
Thank everyone who helps me.
Just for an example, this is what I use to transform the normal tile in a string that won't be change with symbols(;amp, %, ?, etc) in the url
function generateUrl($url) {
$v1 = preg_replace("/[\s\:\;\,\_\'\`\"\?\!\%\(\)\+\=\#\#\[\]\{\}\/]/", "-", $url );
$v2= preg_replace('/[-]{2,}/', '-', $v1);
$v3 = preg_replace('/^[-]/', '', $v2);
$final = preg_replace('/[-]$/', '', $v3);
return $final;
}
I think the answer for your problem is here URL Friendly Username in PHP?. When you add article to the table, use this function (Slug) to convert article title and store converted title in column "slug". When user enters the address (.....)/article.php?name=some-title, use $slug = $_GET['title'] and find article by a slug. Before you save article you should check whether the article with this slug exists. If exists add to slug some number and then save to db. You can't allow to exists two record with the same slug in the table.

Handle slash ('/') in a URL CodeIgniter

I am creating a site using CodeIgniter. I have an url like http://mysite.com/albums/MJ/Dangerous.html where MJ is the artist name and Dangerous is the name of the album. Both of these are dynamic and fetched from a database.
In the database there are some albums which have a slash ('/') character in them. So, such an URL becomes http://mysite.com/albums/50-cent/Get+Rich+or+Die+Tryin%27%2FThe+Massacre.html. On decoding, it turns out as http://ringtones/albums/50-cent/Get Rich or Die Tryin'/The Massacre.html. Here the artist is 50-cent and the album name is Get Rich or Die Tryin'/The Massacre
My question is, what do I do so that I get the entire album name, i.e. Get Rich or Die Tryin'/The Massacre.html as a single argument with the slash character? Currently CodeIgniter shows an error as "Page not found" when I click on such an URL. All other URL's which doesn't have the / works fine.
Try double URLencoding the album name (i.e. urlencode(urlencode($album))). I was trying to pass a URL once to a CodeIgniter controller and it constantly gave me troubles. So I just double encoded it, and then it popped through on the other side no problem. I then ran an urldecode() on the passed parameter.
Let me know if that helps you.
As per current SEO trend no need to bring the quote in url or to fetch on page.
instead during saving (in slag kind of field) and retrieving you can use this kind of option.
$base_url = "http://ringtones/";
$controller = "albums";
$artist = "50 cent";
$album = "Get Rich Or Die Tryin' / The Massacre";
$link1 = $base_url.$controller.'/'.url_title($artist,'-',TRUE).'/'.url_title($album,'-',TRUE);
echo $link1;
Result:
http://ringtones/albums/50-cent/get-rich-or-die-tryin-the-massacre
encode the value before passing
$value = str_replace('=', '-', str_replace('/', '_', base64_encode($album)));
decode the value after receiving
$value = base64_decode(str_replace('-', '=', str_replace('_', '/', $value)));
You will want to urlencode the album name when the data is retrieved from the model so that blackslash is escaped.
You need to use the HTML escaped version of the characters in your URL...
See below for a reference.
http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php
After HTML encoding, your URL should look something like:
http://ringtones/albums/50-cent/Get Rich or Die Tryin'/The Massacre.html
I hope this helps!
N
Extending the #MikeMurko answer to include how to work with Javascript and PHP.
Simply go for double encoding and decoding;
JS:
var id = encodeURIComponent( encodeURIComponent('mystring/&-34') );
PHP:
$id = urldecode( urldecode('mystring/&-34') );

how to avoid change in url address in rtl languages

hey guys im working on a task to make my story's links like this
http://localhost/mycms/article/test/
i used :
$mtitle = str_replace("\"", "'", $title);
$slug_title = mysql_real_escape_string($mtitle);
and a href link to show story's title
in other php file i used two arrays as a moderator for google tab
$urlin = array(
"'(?<!/)modules.php\?name=News&file=article&title=([a-zA-Z0-9_-]*)'",
"'(?<!/)modules.php\?name=News&file=tags&tag=([a-zA-Z0-9_-]*)'"
);
$urlout = array(
"article/\\1/",
"article/tags/"
);
and it automatically change urls
but when it goes to RTL languages such as arabic , it failed
e.g. :
http://localhost/CMS/article//صفحات
while it should be like this :
http://localhost/CMS/article/صفحات/
i tried different ways to correct this but none of them worked
Arabic is right to left and English is left to right, could this be any part of the problem?
Just guessing.
If you want to break apart the elements use
$urlparts=explode("/",$url);
$urlparts is now an array that has each element.
$urlparts[0]="http:";
$urlparts[1]=""; // (because of //)
$urlparts[2]="localhost";
$urlparts[3]="CMS";
$urlparts[4]="article";
$urlparts[5]=""; // (because of //)
$urlparts[6]="صفحات"; //(persian for pages)
You can put it back together I presume?
I suggest splitting the URL, and then replace each part, then re assemble the string as one process.
I had the same problem as I was using the url below and wanted the _3904 at the end of the url before the slash.
http://9th-class.result.pk/punjab/urdu/short-questions/chapter-4_شاعروں-کے-لطیفے_3904/
But it was not working for me so I have changed the url to
http://9th-class.result.pk/punjab/urdu/short-questions/chapter-4_شاعروں-کے-لطیفے_c3904/
and it worked perfectly.

Categories