PHP .htaccess mod_rewrite

旧巷老猫 提交于 2020-01-06 07:57:27

问题


So I want to use mod_rewrite to transform:

random_name.com/main.php?user=$user

into

random_name.com/$user

(the $user is a php variable added onto the url, so it can be any name such as andy or rebecca, names like that)

and my code for the .htaccess is:

RewriteEngine on
RewriteRule ^/([A-Za-z0-9-]+)/?$ main.php?id=$1 [NC,L] 

But this doesn't seem to work for some reason. I've read up on the tutorials but they're really complicated and it seems like this would do the trick, but it doesn't. I'll appreciate it if anyone who has experience with mod_rewrite would give me a few pointers.


回答1:


Try this:

RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)$ main.php?user=$1 [NC,L] 



回答2:


You cannot use mod rewrite to transform random_name.com/main.php?user=$user into random_name.com/$user.
You have to do it manually in all the links on your site.

After that you may use mod rewrite for the reverse transformation, which will make /main.php?user=$user out of /user request



来源:https://stackoverflow.com/questions/9833654/php-htaccess-mod-rewrite

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