PHP Permalinks.. how to change? - php

I built an client website with php script purchased recently and the support for script is pathetic, I want the permalinks of the script to be search engine friendly but they're not. They have spaces inbetween which are not at all indexed by search engine..
So, how can I change the permalinks? Thank you all..

If you've got 50 PHP files and an .htaccess file that come with this "script", you'll likely first have to find the programming path that flows through them. If you take a look at the .htaccess file, you should see some ModRewrite lines, which should end with a PHP file name. That's the script that's receiving (and decoding) the permalinks. That file would be a good place to start looking for a hook to rewrite the permalink structure. If you could post the source code (or put it somewhere like pastebin and post the link) for that file, I'd be glad to take a look.
From one of your other comments it sounds like at least part of the script is using the Smarty PHP template engine. If so, if you can find a folder that contains "cache", "templates", and "templates_c" folders (or similar), you can rule that one out as well; that would be the templates used to show the page, and not any of the decode/encode scripts.
EDIT: Looking at your .htaccess file, line 29 looks to the be one that deals with article permalinks, and it points to view.php, and converts what was the permalink into id and title GET variables. Post the source of view.php if you could, and we'll go from there.
EDIT 2 Okay, looking at view.php gained a little more insight. Primarily of which is that there is no decoding function; the Answerscript engine promptly discards the 'title' part of the permalink and only looks up a question by its ID (number following the pipe on the URL query; you can prove this on their demo page by changing the title part of the URL to anything else, and it will still fetch the right page). So, the good news is that there's no decoding function that needs to be updated when you change the encoding function. Unfortunately this does very little to tell us where the encoding function is in the scripts.
The only hint is that the view.php file includes a file called include/functions/import.php, which I'm presuming has function definitions for does_post_exist($PID), update_last_viewed($PID), update_your_viewed($USERID), and update_viewcount_question($PID). Let's see the source of that file to see if there's any other functions in there that would be used for importing. Also, how many files are in the include/functions/ folder? If there's only a few, post all their sources; likely the encoding function is defined in there. If there's a bunch, is there an export.php file in that folder (i.e. the opposite of import.php that was used by view.php)? Post that file's source as it likely has the encoding function.
EDIT 3 There they are: in the main.php file there's a trio of functions: seo_clean_titles, insert_seo_clean_titles, and seo_clean_titles2. insert_seo_clean_titles is a function to be called from within a Smarty template (search all files that have a .tpl extension for {insert name="seo_clean_titles" to see where that's used), and the difference between seo_clean_titles and seo_clean_titles2 is that the first echoes out the result, while the second returns it. However, all three have the line $title = str_replace(" ", "-", $title);, which should be turning all spaces in the title to hyphens. If you're not seeing that result, likely the code is not calling these functions at the right places. You can search through all the .php files and see if anywhere else there's a call to seo_clean_titles or seo_clean_titles2, and make sure the result is being actually used as the final URL.
Edit 4 To add ".html" to the end of all URLs: Here's the line in the template file linking to the question page:
{$ques[i].title|stripslashes}
Change that to:
{$ques[i].title|stripslashes}
and the links will have ".html" on the end. You'll then need to modify view.php to strip the ".html" back off again when parsing out the ID number: Right before $pid = intval($ph);, insert the following:
if (strtolower(substr($pid, -5)) == ".html") $pid = substr($pid,0,-5); // Remove ".html" if it exists
That should do it!

The urlencode function would take care of the spaces in the URL.
For example:
<?php
$base_url = 'http://example.com';
$category = urlencode('some thing');
$item = urlencode('Name of an item');
echo "<a href=\"{$base_url}/{$category}/{$item}/>{$item}</a>";
?>
This would make the link http://example.com/some+thing/Name+of+an+item/ which a search engine should be fine with.
And, assuming you are using URL rewriting (like mod_rewrite), the values should reach your PHP script as they were before the urlencode function. If not, you can revert them back with urldecode.

Related

What is the function of using php for site links?

I am working on a site and the builders have used a mix of php and html for links. For example:
<li>Variable Speed Drives</li>
<li>Corrosion Resistant Baseplates</li>
and
<li>MP Repair</li>
<li>MTA Repair</li>
The php is referenced in another file in this way:
<?php
$pdf_link = "../pdf/";
$external_pdf_link = "../../pdf/";
$video_link = "../video/";
$external_video_link = "../../video/";
?>
My concern is not knowing the function of the php, other than it being a placeholder, and given that the links work both ways, I don't want to break something because I am clueless to its purpose.
In doing my due diligence researching, I ran across this post, which is close, but still no cigar, Add php variable inside echo statement as href link address?. All of the research seems to be about how rather than why. This is the site, and they only used it for the "Downloads" links: http://magnatexpumps.com/
Thank you...
B
There is no right way. They are just different.
Let's forget the PHP for a while. If you have this link in a page:
<a href='about.html'/>About</a>
What will happen? The browser will change the URL of the document. If you are at the root of the site like: "www.example.com", will redirect to "www.example.com/about.html". If you are in a URL like "www.example.com/news/index.html" will redirect you to "www.example.com/new/about". That's why sometimes it is useful to have a variable before, to force a full path URL.
Another case of URL variable interpolation is when you have different systems running in the same url. In this case, you will have to append the system name in order to get to where you want. If you don't know where your application will run if it will run on the doc root, or in a subfolder, use a variable to indicate the base path.

Page not reading query string without index.php in the URL

this problem started happening when my server got upgraded to use PHP7, so I suspect it has something to do with that.
My website's URLs build the page content based on the query string. For example:
example.com/?prop=one
example.com/?prop=two
example.com/?prop=three
etc...
But for some reason, when I do the following:
$prop = $_REQUEST["prop"];
or:
$prop = $_GET["prop"];
It does not recognize the specified query string value.
On the other hand, using the following URL format works fine (note the addition of "index.php"):
example.com/index.php?prop=one
example.com/index.php?prop=two
example.com/index.php?prop=three
In other words, I'm able to grab the prop query string value without a problem with the presence of the "index.php" in the URL. I don't want to use index.php in the URL, I'd like the URLs to remain clean. This is basically a single page info-app that changes the content via that query string value, so there should be no need for index.php at the root of the domain.
Any thoughts as to how I would fix this? Does this have something to do with updating to PHP7 on the server?
Well, this is embarrassing.
Basically I realized that the problem was that I had an index.html file at the root, and the server was picking that up by default when the index file wasn't specified. So naturally there was no PHP present, so it gave the impression that the query string value wasn't being read correctly. After renaming the index.html file, everything works as expected.
I'm wondering if the settings on my previous server were set to pick up the index.php file by default instead of index.html.
I'd delete this entire post but I suppose someone else might have the same problem so I'll leave it.

file_get_contents() in php - need to pass static parameters and it's not working

I have a simple file I have created:
<?php echo file_get_contents("http://occupancyadvantage.com/shortquiz.aspx?sid=234"); ?>
I can only get the file to render properly when I remove the parameters (well, when you say properly, I doubt you would consider that (http://occupancyadvantage.com/processfree.php -- the %# page.... is showing up) But I could care less about that.
I'm just trying to get a php file to load this aspx file with the ?sid=243 parameter on the end of it.
I don't have access to curl - I have a few weeks without the tech guy here and I'm not allowed to make that kind of an install w/o him... I do have access to the ini file here is what it does have. If something isn't on, I don't know what it is: http://occupancyadvantage.com/phpinfo.php
I've been killing myself with this and I just can't seem to figure it out. Besides punching babies I figured this place was the best option.
If you could please, try to spell out your responses the best you can, as being vague will probably just end up with me researching and asking more questions. I appreciate it.
This note from PHP's documentation on file_get_contents() might help:
If you're opening a URI with special characters, such as spaces, you
need to encode the URI with urlencode().
So that would be:
<?php echo file_get_contents(urlencode("shortquiz.aspx?sid=234")); ?>
If that doesn't work, maybe use the full URL? As in:
<?php echo file_get_contents(urlencode("http://your-site-here/shortquiz.aspx?sid=234")); ?>
You can't file_get_contents with parameters if it's a local file. Just like you can't actually open the file with the parameters in a text file, it doesn't make sense to F_g_c.
You can, if the aspx is http-able, use f_g_c(http:/...).
Otherwise, you'll have to use wget (there are nastier ways to do it such as exec, but don't use them unless you don't need security at all).
You are trying to open the file named shortquiz.aspx?sid=234 in the current working directory. I'm guessing there is no such file in the current working directory.
If you want to fetch the url that ends with shortquiz.aspx?sid=234, you can try this:
file_get_contents('http://example.org/shortquiz.aspx?sid=234');
Of course, replace "example.org" with the full host and path for this URL.
I ended up using an iFrame to do this. I don't know what I was thinking. Thank you for all of your time.

PHP dynamic URL writing to accommodate dynamic menu and paths

I'm trying to figure out what the best way to go about rewriting urls to accommodate not only a directory structure, but appended actions for a menu for a site I'm building in PHP.
I have one page that dynamically loads content based on whatever "page" is being loaded. Includes are pulled based on what the page is. Right now that's tied to directories in my includes folder. So folder/page will include includes/folder/page.php.
If I have a structure like "root/page/action/param" that works fine as long as I say "whatever is in this place in the url is this." But I ran into trouble when I started adding folders that hold different levels of pages. If I explode page out to "folder/subfolder/page" then using placement to determine what is what goes out the window. I was thinking about using something like a hyphen as a delimiter to go "root/folder-subfolder1-subfolder2-page/action/param" so I can identify the page easily but I don't really like that option.
I also don't really like tying my includes directory structure to my urls but the only other way I would think to go about avoiding that would be to have an in-memory object to handle what exactly goes where. But I want to avoid that as I want the menu to be dynamic, based on an XML file that gets parsed and urls generated accordingly. I don't want the overhead of parsing the xml and not only generating URLs but also dynamically generating an in-memory object that has seemingly arbitrary rules.
Any suggestions?
PHPonTrax uses an implementation that I think would allow for this type of routing that you want, and it's open source so you could look at the code for some inspiration. (Note: I am not making any judgement as to the framework, I just have seen the code and think it might work for you).
Basically, you store your routes in an array and you provide some placeholder's as well so that you can have multiple possible controllers/actions/ids after a given path.
So if $routes[] is our array to hold everything, we could add:
$routes[0]['path'] = "foo/bar/:controller/:action";
$routes[0]['params'] = null;
$routes[1]['path'] = "baz/:action";
$routes[1]['params'] = array("controller"=>"blah");
$routes[2]['path'] = "catalog/:id";
$routes[2]['params'] = array("controller"=>"products", "action"=>"view");
$routes[3]['path'] = ":controller/:action/:id";
$routes[3]['params'] = null;
":controller", ":action", and ":id" are unique tokens that indicate what you are allowing a token to represent in the path. So "/baz/edit", "/baz/delete", "/baz/create", "/baz/funkify", etc are all valid provided controller "blah" has edit, delete, create, and funkify methods. If the path is "/catalog/1234", then '1234' is the id and it's a valid path if and only if you can find a product with id=1234. If your request is to "/products/dothis/12345", routes 0-2 don't match, but route 3 does, so you can then look for a controller file named "products", and provided it has a method named "dothis", then you can call "dothis(12345)". Obviously, if there's no "dothis" method, or '12345' can't be found, it's an invalid path and you can raise an exception. Note you need to provide a default route, and in this case it's $routes[3].
In your file structure, you can have:
/app
/controllers
/views
/etc, etc
Possible drawback is that in your rewrite, you send every request to one php script.
If this interests you: PHPonTrax. You will want to look at the router class and action_controller class (see the process_route() and recognize_route() methods).
Use Absolute Paths instead of relative in your menu html & scripts and you shouldn't have problems as it will always point to correct source.
Absolute Path Example (notice the /):
Home
Services
My Page
My Page 2

How do you create domain.com/?stringhere without a .php extension

I want to create a link like the following:
http://www.myurl.com/?IDHERE
What i want to be able to do is be able to goto the above link, and then pull whats after the ? (in this case IDHERE) and be able to use that information to perform a MySQL lookup and return a page.
Can anyone point me into the right direction? please know this is using PHP not ASP
The issue here is not with your scripting language, but with your web server setup. I'll refer to these by their Apache names, but the features should be available in most web servers.
There are three features you might want to use:
1) content negotiation (mod_negotiation), which allows your web server to try a specified list of extensions in a specified order, for example: http://example.com/foo might be http://www.example.com/foo.html or http://example.com/foo.php
2) DirectoryIndex, which tells the web server that when a client asks for http://example.com it should look for a specified list of files in order, so it might server up http://example.com/index.html or http:/example.com/index.php
3) mod_rewrite, which allows you to basically rewrite the URL format received by the server. This allows you to do things like translate http://example.com/foo/bar/baz to http://example.com/foo/bar.php?page=baz
The rest is done by the backend script code as normal.
Create a default PHP file in that directory that will get loaded when no file name is specified (e.g. index.php). In your PHP script you can get the part after the question mark from the variable $_SERVER['QUERY_STRING'].
Do the following in your site's main index.php:
list($id) = array_keys($_GET);
// right now, $id represents the ID you're looking for.
// Do whatever you want with it.
In the link, form or whatever - index.php?id=someid
In your index.php file:
$_GET['id'] = $id
Now you can use it:
e.g.
echo $id;
Since it's your default page, it will work without the extension.
list($id) = array_keys($_GET);
// right now, $id represents the ID you're looking for.
// Do whatever you want with it.
this was exactly what i was looking for, though now i just need to create something to notify if nothing is there or not. Thank you all for your responses.
I would solve it by using .htaccess file if possible.
create a .htaccess file in the main directory with the content:
RewriteEngine on
RewriteRule cat/(.*)/(.*)/(.*)/$ /$1/$2.php?$3
that should translate "example.com/foo/bar/baz" to "example.com/foo/bar.php?page=baz"

Categories