Rewrite url in PHP without htaccess

The website runs on a web hosting service where we do not have access to the htaccess file. The web server is Apache. I want to rewrite the URL. For example: Source URL: www.mydomainname.com/en/piecework/piecework.php?piecework_id=11 Expected URL: piece work .mydomainname.com / en / 11

How to do it?

+1
source share
4 answers

Unfortunately, without access to htaccess or apache, you cannot do this. Get rid of your master or get a new one. Most of the cheap cheapo hosts out there offer mod_rewrite from my experience.

+2
source

I saw people signing a query string to execute (kind of) what you are talking about, i.e.:

domain.com/foo/bar/11 

Actually interpreted as:

 domain.com/foo/bar/11.php?action=foo&query=bar 

However, you need to create this semantics in the structure of the physical directory. This is ugly, naughty control and a lot easier with mod_rewrite.

You probably have mod_rewrite enabled, here is a short tutorial to get you started.

+1
source

This code may be what you are looking for: Use: phpfile.php/en/11 Note: mod_rewrite is not required.

 <?php $DEFAULT_ACTION = 'badAction'; $pathInfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : ''; $pathParts = explode('/', substr($pathInfo, 1)); $action = isset($pathParts[0]) ? $pathParts[0] : $DEFAULT_ACTION; if (function_exists($action)) { $action($pathParts); } else { badAction(); } function badAction($parts = null) { print 'The function specified is invalid.'; } function en($parts) { $name = isset($parts[1]) ? $parts[1] : 'undefined'; $xml = new SimpleXMLElement('<message>Number: ' . $name . ' </message>'); header('Content_Type: text/xml'); print $xml->asXML(); } ?> 

ps. I found the code in "Professional PHP6" ISBN: 978-0-470-39509-7

+1
source

In fact, you can physically create folders / files (therefore, access to this path actually physically exists), and then redirect / process accordingly in those cumbersome ones, but if this is your only choice ....

Another possibility: if your host allows you to at least create a 404 document with an error, and then process everything on it based on REDIRECT_URL.

-one
source

Source: https://habr.com/ru/post/1495783/


All Articles