Redirect “mydomain.com/user” to “mydomain.com/name.php?id=user”

笑着哭i 提交于 2019-12-24 09:17:37

问题


I think I can use the .htaccess file for this, but I've looked it up and not found anything useful. What I want to do is have my site redirect to a php page when a user types their username in the URL like:

example.com/username

And have it be the same as a PHP page like:

example.com/name.php?id=username

I'd like it to display as example.com/username even after it redirects, but it is not necessary. Any ideas?


回答1:


You can use mod_rewrite to transparently rewrite your URLs on the server.

Assuming that you'd only have usernames following your domain, something like this would do what you want:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ name.php?id=$0



回答2:


RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) users.php?user=$1 [L]

I think that will work.

The Apache mod_rewrite guide is here http://httpd.apache.org/docs/2.0/misc/rewriteguide.html




回答3:


You probably want Apache's mod_rewrite.



来源:https://stackoverflow.com/questions/3508171/redirect-mydomain-com-user-to-mydomain-com-name-phpid-user

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