php url shortening

冷暖自知 提交于 2019-12-25 05:11:39

问题


Ok i guess this is a bit of a general question really as opposed to a problem.

I am building a system where users can share their profile on other websites so i am wondering is it possible to shorten the actual url which would provide a link to their profile which would be something like this, www.somedomain.com/users/profile.php?user=myusername to simply cut out the users folder and the profile page and so something like this: www.somedomain.com/myusername

I have seen lots of url shortening scripts but they don't seem to do this, any suggestions or advice would be appreciated.

Thanks


回答1:


What you're looking for is called URL rewriting and can be done using Apache's mod_rewrite. You would place a file called .htaccess in your root web directory and it would contain a snippet like this:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule  /(.*) /users/profile.php?user=$1



回答2:


this is called "url rewriting" - there are different approaches to do this, for example using apaches mod_rewrite. another way would be to manually parse $_SERVER['REQUEST_URI'] - this would make your site work even if mod_rewrite isnt enabled, but is some more work.



来源:https://stackoverflow.com/questions/10557955/php-url-shortening

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