2

i'm new to mod_rewrite, and i'm trying to convert my web address from:

website.com/profile.php?user=andy

to the following:

website.com/user/andy

This is my following code:

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

I researched extensively and this does seem to be the correct way to do it, but it doesn't redirect to where i want it to, it just redirects to this:

http://website.com/profile.php?user=andy

which means i must doing something wrong...

Can anyone help me out here? I would appreciate any tips.

1
  • Your code is correct. Did you give the link like <a href="website.com/user/andy">Test</a>. If so, then it will redirect to profile.php Commented Mar 23, 2012 at 8:51

3 Answers 3

1

If you want

  • http://website.com/profile.php?user=andy ->301-> http://website.com/user/andy
  • http://website.com/user/andy means http://website.com/profile.php?user=andy

They are 2 different things, you'll need 2 rules:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^user=([A-Za-z0-9]+)
RewriteRule ^profile.php /user/%1? [R=301,L]

RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?a=b&user=$1 [L]

The first will 301 (moved permanently) redirect to the pretty url. The second will allow your application to understand the pretty url.

Whenever you change the url scheme for a site you should take care of existing links. As such, that first rule is required/a good idea. You should not, however, need the first rule when using your own application. If your own application is generating links to profile.php?user=me - change your application code.

Sign up to request clarification or add additional context in comments.

6 Comments

Hey AD7, thank you so much, i read over some more mod_rewrite, and your way does seem to be the RIGHT way. It is the 301 that physically redirects to a prettier url. I copied and pasted your code to the .htaccess, but it still doesn't direct to website.com/user/andy , rather, it goes to website.com/profile.php?user=andy . If i go to website.com/user/andy, it does work though. AHHH this is so confusing T.T
lol sorry im new. I noticed that you changed the code. Thanks so much for that. It finally redirects to website.com/user/andy but the page says "this page isn't redirecting properly, firefox has detected that the server is redirecting the request for this address in a way that will never complete. I'm not sure what this means o.o
Chrome gives the error: Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.
I think for the second line, maybe the L should be taken out? cuz doesn't the L stop running the rules, such as that the third line won't be reached? But RewriteRule ^profile.php /user/%1? [R=301] doesn't seem to work either :/
tip: use copy and paste. and try the php chat room for "interactive" help, not comments.
|
1

You have to change your URLs when outputting them in your HTML to be in the format you want (/user/andy).

mod_rewrite will rewrite /user/andy to main.php?... not the other way around.

Comments

1

What do you mean by my result?

mod_rewrite won't change existing links in your source code. Navigate to website.com/user/andy and you should see it work.

2 Comments

Hey alex! i meant result as in, when I log in, it will take me to that url and not the one i want (i re-edited, thanks for pointing that out). I tried your suggestion, and website.com/user/andy does seem to work if i go there, but it doesn't physically change on the url bar. How do i get it to physically change the words to website.com/user/andy and not website.com/profile.php?user=andy
Thank you so much for the help alexn and AD7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.