how to write pretty URLs without htaccess file in

2020-05-09 18:43发布

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.

3条回答
家丑人穷心不美
2楼-- · 2020-05-09 19:01

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.

查看更多
▲ chillily
3楼-- · 2020-05-09 19:06

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.

查看更多
一纸荒年 Trace。
4楼-- · 2020-05-09 19:06

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

查看更多
登录 后发表回答