Pretty URLs without mod_rewrite, without .htaccess - php

Without a possibility to access .htaccess I find myself in a creative impasse. There is no mod_rewriting for me. Nevertheless, I want to be able to do the nice stuff like:
http://www.example.com/Blog/2009/12/10/
http://www.example.com/Title_Of_This_Page
What are my alternatives?
In respond to the answers:
I'm building with php5
I don't have access to .htaccess
http://www.example.com/index.php/Blog/ is a known technique but I don't prefer it. Is shows the php so to say.
How would I create extensionless PHP-files? Would this do the trick?
How much would using the custom 404 technique hurt performance?

If you've the permissions to set custom error documents for your server you could use this to redirect 404 requests.
E.g. for Apache (http://httpd.apache.org/docs/2.0/mod/core.html#errordocument)
ErrorDocument 404 /index.php
In the index.php you then can proceed your request by using data from the $_SERVER array.

You can also have urls like
http://domain.com/index.php/Blog/Hello_World
out of the box with PHP5. You can then read the URL parameters using
echo $_SERVER['PATH_INFO'];
Remember to validate/filter the PATH_INFO and all other request variables before using them in your application.

I know this question is very old but I didn't see anyone else suggest this possible solution...
You can get very close to what you want just by adding a question mark after the domain part of the URL, ie;
http://www.example.com/?Blog/2009/12/10/
http://www.example.com/?Title_Of_This_Page
Both of the above HTTP requests will now be handled by the same PHP script;
www.example.com/index.php
and in the index.php script, $_SERVER['REQUEST_URI'] for the two pages above will be respectively;
Blog/2009/12/10/
Title_Of_This_Page
so you can handle them however you want.

A quite simple way is to:
declare a 404 ErrorDocument (e.g. PHP) in .htaccess
parse the query using $_SERVER and see if it corresponds to any result
if so replace the HTTP status 404 with status 200 using header() and include index.php

If you omit a trailing slash, Apache will serve the first file [alphabetically] which matches that name, regardless of the extension, at least on the 2 servers I have access to.
I don't know how you might use this to solve your problem, but it may be useful at some point.
For example if
http://www.somesite.com/abc.html and http://www.somesite.com/abc.php both exist and http://www.somesite.com/abc is requested, http://www.somesite.com/abc.html will be served.

The only way is to use custom 404 page. You have no possibility to interpret extensionless files with PHP interpreter without reconfiguring the web server's MIME-types. But you say that you can't edit even .htaccess, so there's no other way.

You can write a URI class which parses the user-friendly URL you defined.

If the MultiViews option is enabled or you can convince whoever holds the keys to enable it, you can make a script called Blog.php that will be passed requests to example.com/Blog/foo and get '/foo' in the $_SERVER['PATH_INFO'].

Related

Use slash with get request

I'm in the process of working on an error system for my site (i.e., if MySQL encounters an error, it sends them to an error page). I'm wondering, is it possible to use a "/" instead of "?err=" for a URL?
What I'd like to do is have people sent to the url "/error/404/" but display on page the content at url "/error?err=404". Is there a way to do this with HTAccess, or something of the sort?
My current way is with lots of files and iframes, and it gets really annoying when you have to update one tiny little thing.
Thanks!
What you are looking for is url rewriting. You can set it up using an .htaccess file, given that your installation of apache has mod_rewrite enabled (if not, check this question).
Here is a nice tutorial on how to do it.
Have a try with this:
RewriteEngine on
RewriteRule ^error/(\d+)$ error?err=$1 [L,QSA]
This should not end in a redirection loop, since this requires a trailing number in the URI.
Note that I removed your leading slashes from both the pattern and the result. .htaccess style files work on relative paths.
In general you should always prefer to place such rules inside your http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and the really slow the server down. They are only available as a last option for users who do not have access to the host configuration, for example when using a cheap hosting provider.

Handle multiple sublink depths with php with little or no mod rewrite dependency

I am trying to write a content management system and have hit a snag while trying to develop seo friendly urls. I am using php to handle urls, however I have a problem when I try to get the REQUEST_URI for more than one depth level. I am trying to avoid using .htaccess to handle this, because I would like the system to be fairly easy to set up on IIS/nginx/etc also and do not want it to be apache dependent any more than is necessary.
I have in my htaccess file
FallbackResource index.php
and then in my php I have a class that handles the REQUEST_URI slug by checking to see if a record exists in the mysql database. This works fine if the request is something like
http://example.com/foo
however throws an internal server error if the request is
http://example.com/foo/bar
This seems to occur even if I have a completely blank index.php, so I suspect the answer must be at the htaccess level. How can I get my system to handle multiple REQUEST_URI depth levels? Do I need to use a mod rewrite regex or is there a less apache dependent solution?
My bad, I needed to change my .htaccess rule from
FallbackResource index.php
to
FallbackResource /index.php
The missing slash was causing the error. -.-

Create SEO permalinks using PHP without .htaccess

Currently, my page URLs look this this:
http://ourdomain.com/articles/?permalink=blah-blah-blah
I want to convert these to:
http://ourdomain.com/articles/blah-blah-blah
How can I accomplish this using PHP but not with .htaccess?
How can i accomplish this using php but not with .htaccess..
You can't. You will need to tell the web server how to deal with URLs that don't physically exist. In Apache, that is done in the central configuration or in a .htaccess file.
If your server already happens to have AccepPathInfo On, you can try having URLs like
http://ourdomain.com/index.php/articles/blah-blah-blah
which will redirect to index.php and have articles/blah-blah-blah in the $_SERVER["PATH_INFO"] variable. This method is known as "poor man's URL rewriting" because you can't get rid of the index.php part in the URL. If the mentioned setting is turned on (I think it is by default), you may be able to do this without using a .htaccess file.
You can achieve this without mod_rewrite if you have access to the server configuration. Assuming you're using Apache, the first thing you would need to do is turn the MultiViews option on on your document root (ie. add Options MultiViews). Now copy your /articles/index.php to /articles.php (so put the script in your document root and rename it), and adapt your script so it reads $_SERVER["PATH_INFO"] to fetch the correct page (this of course relies on having AcceptPathInfo On).
MultiViews will make sure that the articles.php script is called when you provide a /articles/blah-blah URL.
I don't think you can easily do it without altering .htaccess. You'll most definitely need to use mod_rewrite. See the answers here for more info:
Special profile page link like www.domain.com/username
It is possible to do it in PHP, without modifying .htaccess
Just write following code in either index.php or default.php
<?php
if (isset($_GET['permalink'])) {
header('Location: '.urlencode($_GET['permalink']));
}
?>
It works because when you type following URL:
http://ourdomain.com/articles/?permalink=blah-blah-blah
The filename is not specified.
So, the server looks whether "index" or "default" file is present in the specified directory.
Consider file index.php is present, so server will call:
http://ourdomain.com/articles/index.php
with blah-blah-blah in GET variable permalink
The PHP code checks if permalink GET variable is present, and redirects using header() method.
EDIT: added urlencode() to do input validation

php - clean URL

I want to create a web site with pure PHP. I want to hide the url parameters. I.e. I want to make my web site with clean urls. Is there is any way to do this with out using any framework? Is cURL helpful to do this?
See URL rewriting in PHP without .htaccess if you don't want to or can't use .htaccess, else refer to How to: URL rewriting in PHP?.
Just have a look on it...before you start your stuffs
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
First of all: It is not possible with PHP only (at least not the forms of URL I think of when reading clean URL). The web server needs to know how to handle requests and what requests are meant to be passed to your PHP script. Otherwise you will probably just get a 404 response.
Because the default behavior of a web server is to just take the requested URL path and try to map it to an existing file below the document root. If a corresponding file was found, either the file’s content is passed back to the client or – as in case of PHP files – the file’s content is passed to an appropriate interpreter and the returned data is passed back to the client. And if the file was not found, well, it responds with the status code 404. So at some point you need to configure your web server.
But after that, when the request was passed to your PHP script, you sure can use just PHP to establish clean URLs. And I would rather suggest to do that with PHP than with web server utilities. Because your PHP application should know best how to handle a requested URL.
In PHP, all required information are in the $_SERVER variable:
$_SERVER['REQUEST_URI'] holds the requested URL path and query (you can parse that with parse_url), and
$_SERVER['PATH_INFO'] holds the PATH_INFO if you’re using that (see Apache’s AcceptPathInfo directive).
Try to rewrite url using php and rewrite url using .HTACCESS.
For example, original url,
www.domain.com/item.php?product=Cars for sale in amazon
with php
www.domain.com/item.php?product=Cars-for-sale-in-amazon
and with .HTACCESS file
www.domain.com/Cars-for-sale-in-amazon
Nope, no curl or framework doing this. Nor php at all.
It is web server who deal with urls.
So, if you want fake urls, you have to set up your web server to redirect certain urls to certain scripts.
The most common way is to use Apache web server with mod_rewrite module
From what I have read and understand of it, there's 2 ways you can do this:
The first being mod_rerite where everything seems to re fone through rewrite rules through the .htaccess file fairly simple to do but can put big load on webserver with large sites
Use PHP to control the rerites this does use .htaccess but only to redirect everything back to the index.php where a dispatcher reroutes paths as necessary. There's a fantastic tutorial of this at phpvideotutorials.com the tutorial is called the tumblelog.

How can a URL like http://localhost/index.php/articles/edit/1/my-first-article work without an .htaccess?

I don't get this:
http://localhost/index.php/articles/edit/1/my-first-article
This URL is mentioned as an example in the Kohana framework documentation. I poked around in the files of my installation, and there is no .htaccess besides my own one that has nothing to do with that.
So, how can it be that an index.php is called but then, as parameters, the stuff looks like added directories to the URL? That doesn't look "real".
Or is this just how native PHP/Apache/HTTP stuff actually works? As I understand it, / is always telling "hey, a directory!". Makes really zero sense to me... how's that possible? Or do they have somewhere an .htaccess that I just can't see / find?
From the Apache docs:
AcceptPathInfo Directive
This directive controls whether
requests that contain trailing
pathname information that follows an
actual filename (or non-existent file
in an existing directory) will be
accepted or rejected. The trailing
pathname information can be made
available to scripts in the PATH_INFO
environment variable.
For example, assume the location
/test/ points to a directory that
contains only the single file
here.html. Then requests for
/test/here.html/more and
/test/nothere.html/more both collect
/more as PATH_INFO.
So I assume this setting is enabled somewhere like in httpd.conf. Note that this, like mod_rewrite, can be enabled/configured in a number of places - see here.
In PHP you can get the data after the filename with the $_SERVER["PATH_INFO"] variable. This allows you to basically GET information without having to use GET variables, which means Google and co will think you're using static pages. This is basically an alternative to mod_rewrite which is often enabled while mod_rewrite is more often not enabled.
This may be obvious to you, but it wasn't immediately to me, this doesn't work correctly on index pages unless you use the filename. For instance, http://example.com/test/my/get/params will not work, while http://example.com/test/index.php/my/get/params will.
AcceptPathInfo is turned on for your server. :)
I'm not using Kohana, so I don't know if my method will be of any use, but when a server doesn't support .htaccess files (or rewrite rules) my 'framework' generates URI's like this:
http://www.domain.com/?/articles/edit/1/my-first-article (notice the ?)
It's a similar method used by the Frog framework, just parse $_SERVER['REQUEST_URI'] (or $_SERVER['HTTP-X-REWRITE-URL'] on windows servers) and explode on the '/'.
This method is completely rewrite independent and still generates more-or-less SEO friendly URI's
Hope it's of any use to you.
Probably not the case here, and certainly not recommended, and probably not the right answer...
BUT ...
I've seen people use 404 pages to parse the request and then include the right page with that information.
See PATH_INFO in CGI Environment Variables.

Categories