how to write pretty URLs without htaccess file in php

时间:2015-11-12 11:41:44

标签: php .htaccess url url-rewriting pretty-urls

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 个答案:

答案 0 :(得分: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.

答案 1 :(得分:0)

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.

答案 2 :(得分:0)

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