10

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?
Kriem
  • 8,666
  • 16
  • 72
  • 120

8 Answers8

12

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.

Philippe Gerber
  • 17,457
  • 6
  • 45
  • 40
  • Ah, beat me to it. I thought about this for 5 minutes and finally came up with the same thing. – Stefan Mai Jun 10 '09 at 12:36
  • 10
    The document returned still has a HTTP 404 status on it, not a 200. This will cause a lot of problems with web crawlers. – chaos Jun 10 '09 at 12:43
  • 5
  • 7
    You cannot POST (i.e., you can, but the data will be lost) to an error document script. – Maciej Łebkowski Jun 10 '09 at 12:55
  • @Maciej Łebkowski: that should not be a problem because clients usually do not POST to you themselves (you say when and to which resource). – Philippe Gerber Apr 24 '11 at 11:48
  • If you use switch-case, send a `200 OK` header before the `switch`, send the `404 Not Found` header in the `default:` case and finally wrap the whole router around with `ob_start()` and `ob_end_flush()`. This way the `200` gets sent to all, but the else case sends a `404`, which overwrites the earlier `200`. – aalaap Feb 03 '14 at 13:59
7

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.

Nigel Alderton
  • 2,265
  • 2
  • 24
  • 55
  • 1
    I actually just implemented something like this and found that the single question mark served well to call the index.php page without exposing it as the handler. it's not *as clean* but it still works without having to configure the web server rewrite rules. – Wes Jan 21 '20 at 23:10
7

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.

Shoan
  • 4,003
  • 1
  • 26
  • 29
  • 1
    I'm aware of this technique. I changed the question asking for a way without this method. It's an alternative nevertheless so you're right. I just don't prefer this one. – Kriem Jun 10 '09 at 12:53
3

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.

Alex S
  • 25,241
  • 18
  • 52
  • 63
  • You are refering to the Option `MultiViews` in apache. This option can have unwanted side effects. https://httpd.apache.org/docs/current/mod/mod_negotiation.html – Synox Mar 20 '15 at 15:10
3

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
instanceof me
  • 38,520
  • 3
  • 31
  • 40
  • 1
    The question, again, becomes, how to do that w/o .htaccess. Some webhosts do offer a custom 404 in their configs, though, so this would be possible. – Piskvor left the building Jun 10 '09 at 12:51
  • Right. I don't see any solution without either being able to choose (or edit) your 404 ErrorDocument or use a .htaccess, because you have to set this up at request-hendling level... Or use the solution you don't like: `index.php/Blog/...` – instanceof me Jun 10 '09 at 13:12
2

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.

Jet
  • 1,171
  • 6
  • 8
1

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

Joe
  • 31
  • 1
0

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'].

chaos
  • 122,029
  • 33
  • 303
  • 309