简体   繁体   中英

PHP .htaccess mod_rewrite

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.

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

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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