how to write pretty URLs without htaccess file in php

混江龙づ霸主 提交于 2019-12-20 07:44:11

问题


Pretty URLs without htaccess

I want to write pretty URL like :
http://yousite.com/member/1/ instead of http://yousite.com/index.php?page=member&id=1
But i did with htaccess file and now i want to do this without htaccess file because sometimes you don't have permission to edit htaccess file on server.


回答1:


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.




回答2:


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.




回答3:


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'].'/');



来源:https://stackoverflow.com/questions/33670644/how-to-write-pretty-urls-without-htaccess-file-in-php

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