Page 1 of 1
mod_rewrite troubles
Posted: Fri Aug 14, 2009 10:14 am
by ninethousandfeet
Hello,
I have been reading up on mod_rewrite and have tried multiple variations of the rewrite to make my urls appear more seo/user friendly... and it isn't working.
I do not have a whole lot of pages, so I have no folders (all files are in my one public folder). I am trying to change urls like these:
http://www.mysite.com/profile.php?user_id=99 ... changed to ...
http://www.mysite.com/profile/usernamehere
another example:
http://www.mysite.com/post.php?post_id=1234 ... changed to ...
http://www.mysite.com/post/productnamehere
Most everything that I see on this topic has an additional folder before the file that is being changed, is this the problem?
Using this below, I receive no change in how the url is displayed, any ideas how to fix it? Thank you.
Code: Select all
Options +FollowSymLinks
RewriteEngine On
RewriteRule /post/([0-9]+) /post.php?post_id=$1
Another question, what is the variable $1 supposed to represent? This might also be my problem, but I see $1 in many examples for re
Re: mod_rewrite troubles
Posted: Fri Aug 14, 2009 10:22 am
by jackpf
Try this:
Code: Select all
RewriteRule ^post/([0-9]+)$ post.php?post_id=$1
The $1 represents the reg expression saved. since ([0-9]+) is what you want, and it's the first, it'll be $1.
Re: mod_rewrite troubles
Posted: Fri Aug 14, 2009 10:52 am
by ninethousandfeet
i just tried that and no luck.
i used:
Code: Select all
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^post/([0-9]+)$ postpage.php?post_id=$1
and the url displays:
http://www.mysite.com/postpage.php?post_id=777
any ideas or suggestions?
Re: mod_rewrite troubles
Posted: Fri Aug 14, 2009 11:01 am
by littleg
if your going to use that line then dont you have to specify which directory your going to use it for, as the RewriteEngine is on but the 2nd line is saying its relevant to not all directories.
Re: mod_rewrite troubles
Posted: Fri Aug 14, 2009 12:03 pm
by ninethousandfeet
okay. can you let me know if this is possible and if so, how i can fix my rewrite to allow for it.
let's say that a user searches for products then results are displayed as links.
the user is interested in one of the posts and clicks. right now that user clicks the link and is taken to the page with that posts info, in the url, they see:
http://www.mysite.com/postspage.php?post_id=543.
how can i make it so that when the user clicks the post they are interested in, they see this in the url:
http://www.mysite.com/posts/543
Re: mod_rewrite troubles
Posted: Fri Aug 14, 2009 12:08 pm
by jackpf
Replace the link with the new url.
Re: mod_rewrite troubles
Posted: Fri Aug 14, 2009 12:36 pm
by ninethousandfeet
manually? or using rewrite?
Re: mod_rewrite troubles
Posted: Fri Aug 14, 2009 12:40 pm
by jackpf
Manually. If you want users to see that url then you'll have to point them to it. Or redirect them...but that'd increase server load and page load times.
Re: mod_rewrite troubles
Posted: Fri Aug 14, 2009 12:52 pm
by ninethousandfeet
okay, so you are saying i'm better off pointing them straight there. i'm new to php for the most part so maybe if i explain a little more about what i have, you can help me see how i can make it better.
my search page shows results and shows them as a link like this:
Code: Select all
<a href='postpage.php?post_id=<?php echo $row_getproduct['post_id'];?>'>Post</a>
if clicked the user is taken to postpage.php and the url is a mess and not secure as i've explained.
at what step am i changing the url link? how do you recommend i change it?
Re: mod_rewrite troubles
Posted: Fri Aug 14, 2009 3:06 pm
by jackpf
Just change that link to whatever you want it to look like!!!
Then, you can make a rewrite rule to point the new url to the actual url.

Re: mod_rewrite troubles
Posted: Fri Aug 14, 2009 3:59 pm
by ninethousandfeet
okay, i'm trying to wrap my head around this, but this is completely different from what i keep reading on every site. can you give me an example if you were going to do it given my scenario?
the link that passes the post_id variable currently looks like:
Code: Select all
<a href='postpage.php?post_id=<?php echo $row_getproduct['post_id'];?>'>Post</a>
what i came up with for your suggestion of making up whatever link i want:
link that i want ---
Code: Select all
<a href="post">Posting title in search result</a>
my rewrite ---
Code: Select all
RewriteEngine On
RewriteRule ^post/([0-9]+)/?$ /postpage.php?post_id=$1 [R=301]
problem is that it redirects from the made up link to the url with everything in it ... my original url. i want the one that it is redirecting from now in this scenario.
Re: mod_rewrite troubles
Posted: Fri Aug 14, 2009 4:03 pm
by jackpf
Ok, the link:
Code: Select all
echo '<a href="post/'.$whatever.'">Post</a>';
And the rewrite:
Code: Select all
RewriteEngine On
RewriteRule ^post/([0-9]+)$ post.php?post_id=$1
Re: mod_rewrite troubles
Posted: Fri Aug 14, 2009 4:17 pm
by ninethousandfeet
okay, so what you just provided does redirect that link to the new desired url, but then i get a 404 error.
any ideas?
Re: mod_rewrite troubles
Posted: Fri Aug 14, 2009 4:36 pm
by ninethousandfeet
okay, i finally got it!
thank you for all of your help today.
Re: mod_rewrite troubles
Posted: Fri Aug 14, 2009 6:40 pm
by jackpf
Cool, thank god
Ahh no problem. Good luck!