how to write pretty URLs without htaccess file in php

心不动则不痛 提交于 2019-12-02 13:06:02

If you have an Apache server and AcceptPathInfo is enabled, then you can 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.

Actually you can create a new folder dynamically for each member with a simple index file inside that includes your front controller, but even if it's possible, you probably don't want to do that, and instead want to change your host.

I might have this Single Line of Code inside index.php page if i don't want to use .htaccess.

Pseudo code: Get the ID and Redirect the user

Short Explanation : Concat the id that you got in the url and redirect to your desired location with that id

Real Code

header('Location: http://yousite.com/member/'.$_GET['id'].'/');

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!