简体   繁体   English

PHP .htaccess mod_rewrite

[英]PHP .htaccess mod_rewrite

So I want to use mod_rewrite to transform: 所以我想用mod_rewrite来转换:

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) ($ user是一个添加到url上的php变量,因此它可以是任何名称,例如andy或rebecca,诸如此类)

and my code for the .htaccess is: 我的.htaccess代码是:

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. 如果有任何使用mod_rewrite的经验的人给我一些指导,我将不胜感激。

You cannot use mod rewrite to transform random_name.com/main.php?user=$user into random_name.com/$user. 您不能使用mod rewrite将random_name.com/main.php?user=$user转换为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 之后,您可以使用mod rewrite进行反向转换,这会使/main.php?user=$user超出/user请求

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM