views:

366

answers:

5

website is running on a web hosting where we don't have access on htaccess file. i want to do URL rewriting for user friendly URL. e.g. Original URL www.domain-name.com/file?q=name

expected URL www.domain-name.com/file/name

A: 

Your best bet will be to have URLs such as this:

www.domain-name.com/index.php/file/name

You'll to rewrite your PHP code though.

Alix Axel
A: 

You'd need to do something like the following:

<?php
include('file/'.str_replace('/', '', $_GET['q']));
?>

Then you'd access http://example.com/script.php?q=name what would include http://example.com/file/name.

Jerry
if u use this solution sanitize the get variable
Haim Evgi
this doesn't help the url rewriting, as the user requested to use www.domain-name.com/file/name as url, you are telling him the reverse way
Pentium10
Oh. Looks like I got him wrong.
Jerry
+1  A: 

as Alix Axel suggested you can use

www.domain-name.com/index.php/file/name

then you will use $_SERVER['REQUEST_URI'] to process the url

Pentium10
Better use `$_SERVER['PATH_INFO']` instead of `$_SERVER['REQUEST_URI']` in this case.
Mathias Bynens
This workaround looks good. one problem when i am using www.domain-name.com/index.php/filename it looks like current directory changesand all css formating lost and relative navigation is also not working.any suggestions.
Vivart
add / base reference for your each file referencelike`<a href="/support.html"> </a>`also link your CSS by using base reference `src="/media/css.css"`
Pentium10
+3  A: 

As other people said, just use links like /index.php/nice/looking/url.
The "index.php" in the middle of the URL might look a little strange, but I don't think it's possible to have it look better without .htaccess

Else, you could ask your hoster to redirect any URL to /index.php so that you can handle URL rewriting without having /index.php in your URL.

Then you can just use a regex match to detect what file to include.
preg_match('@[/]{1}([a-zA-Z0-9]+)@', $_SERVER["PATH_INFO"], $matches) ($matches will contain all "parts" of the url in an array)

Be careful with including the files, use a whitelist so you're sure nobody would be able to load internal files.

Daan
+1  A: 

If you have an Apache server and AcceptPathInfo is enabled, then you can use the URL you wrote. A request of /file/name will then be automatically rewritten to /file with the PATH_INFO value of /name if /file is a regular file.

Gumbo