Jump to content

first time mod_rewrite help


kaiman

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

@ 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

Link to comment
Share on other sites

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/#/

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.