kaiman Posted January 14, 2011 Share Posted January 14, 2011 I am trying to clean URLs in my custom made blog using mode_rewrite but am a little confused by how to use the regular expressions. I am trying to convert all pages with: http://www.domain.com/blog/article/?id=1 Which is actually: http://www.domain.com/blog/article/index.php?id=1 (I just shortened the URL like the one above in my link) To: http://www.domain.com/blog/article/id/1/ How would I go about doing this? I am new to mod_rewrite and regular expressions so you'll have to forgive me. So far I have the following: RewriteEngine On RewriteRule ^article/([0-9]+)/?$ ?id=$1 [NC,L] Is this correct? Thanks for any help or suggestions, kaiman Quote Link to comment https://forums.phpfreaks.com/topic/224451-first-time-mod_rewrite-help/ Share on other sites More sharing options...
kaiman Posted January 14, 2011 Author Share Posted January 14, 2011 Okay so I've changed all my links from: http://www.domain.com/blog/article/?id=$id To: http://www.domain.com/blog/article/index.php?id=$id And think I need to use the following mod_rewrite but I'm unsure. Can someone take a look at this and tell me whether it looks correct? RewriteRule ^/id/([0-9]+)/?$ ?id=$1 [NC,L] Should output this address: http://mydomain.com/blog/article/id/1/ (for example) Also, I have a sql query using $_GET through the url to display the posts according to id. What do I need to change to get it to work with this rewrite? Here is the code: // get url variables $post_id = mysql_real_escape_string($_GET['id']); Any help is appreciated. Please let me know if I'm not being clear. Thanks, kaiman Quote Link to comment https://forums.phpfreaks.com/topic/224451-first-time-mod_rewrite-help/#findComment-1159539 Share on other sites More sharing options...
requinix Posted January 15, 2011 Share Posted January 15, 2011 You got your URL backwards. Your code should generate URLs like /blog/article/id/1 You then configure your .htaccess to rewrite from /blog/article/id/# to /blog/article/index.php?id=#. Any reason you want the /id/ in the address? You don't need it, you know... Quote Link to comment https://forums.phpfreaks.com/topic/224451-first-time-mod_rewrite-help/#findComment-1159575 Share on other sites More sharing options...
kaiman Posted January 15, 2011 Author Share Posted January 15, 2011 @ requinix Not sure I am following you here. If I have a URL that currently looks like this: http://www.domain.com/blog/article/index.php?id=$id And want to change it to this: http://www.domain.com/blog/article/id/{id number here}/ Wouldn't I use something like this to do it and stick the .htaccess file in the /article/ directory? Or do I have it backwards? Can you provide an example? RewriteEngine On RewriteRule ^id/([0-9]+)/?$ index.php?id=$1 [NC,L] Thanks, kaiman Quote Link to comment https://forums.phpfreaks.com/topic/224451-first-time-mod_rewrite-help/#findComment-1159827 Share on other sites More sharing options...
strago Posted January 22, 2011 Share Posted January 22, 2011 Options +FollowSymLinks +Indexes RewriteEngine on RewriteBase / RewriteRule ^blog/article/id/([^.]+)/$ blog/article/index.php?id=$1 [L] For an even shorter URL... RewriteRule ^article/([^.]+)/$ blog/article/index.php?id=$1 [L] domain.com/article/#/ Quote Link to comment https://forums.phpfreaks.com/topic/224451-first-time-mod_rewrite-help/#findComment-1163445 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.