Jump to content

mod_rewrite help


limitphp

Recommended Posts

I'm trying to do a url rewrite.

I want to transform this:

mysite.com/artist.php?artistID=2&artistName=The Strokes

 

into this:

mysite.com/artistName/The-Strokes/

 

 

Will modifying the htaccess file do this?

 

Would I have to make some type of rewrite rule?

RewriteRule ^artistName/([a-zA-Z0-9 ]+)/$ /artist.php?artistID=$1

 

 

Link to comment
Share on other sites

Ok, I put that rewrite in the .htaccess file.

RewriteRule ^artistName/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/$ /artist.php?artistID=$1&artistName=$2

And I clicked on the link:

http://localhost/greckle/artist.php?artistID=1&artistName=The%20Strokes

 

and it took me to the artist.php page, but nothing happened.

The link stayed the same and when I tried to grab the artistName:

$artistname = $_GET['artistname'];

 

it didn't work.  As in, when I displayed it:

echo $artistName

 

nothing appeared.

Link to comment
Share on other sites

I'm starting to understand.....in my website, I should write the url like this:

mysite.com/artistName/The-Strokes/

 

not with the querystrings.....

 

: )

 

I had it backwards......I kept thinking it was going to take my url from my website with the querystrings and rewrite it without........

No....

 

it takes the nice looking one and converts it to the appropriate one (secretly).....

: )

 

I'm starting to get this....

 

Link to comment
Share on other sites

Ok, if I rewrite a url like this:

mysite.com/artist/The-Strokes

 

and my rewrite rule is this:

RewriteRule ^artist/([a-zA-Z0-9-]+)/$ /artist.php?artistName=$1

 

in the database, the name is "The Strokes", not "The-Strokes" with a dash.

 

Will it match up?

 

 

 

 

 

 

 

Link to comment
Share on other sites

Hey limitphp,

 

No it will not match up, a quick fix would be to run the artistName through a "cleaning" function.

 

Basically remove - and replace it with a space.

 

$artistName = str_replace("-"," ", $artistName);

(use the above code before you search artistName in your database).

 

Normally though you should have whats known as a hash for each artist and save it in your database. So for example "The Strokes" would have a hash like "The-Strokes" and this is what you would use to check to see if theres a match.

 

Link to comment
Share on other sites

Hey limitphp,

 

No it will not match up, a quick fix would be to run the artistName through a "cleaning" function.

 

Basically remove - and replace it with a space.

 

$artistName = str_replace("-"," ", $artistName);

(use the above code before you search artistName in your database).

 

Normally though you should have whats known as a hash for each artist and save it in your database. So for example "The Strokes" would have a hash like "The-Strokes" and this is what you would use to check to see if theres a match.

 

 

A hash for each artist?

So, in my artist table, which contains artistID, artistName, etc....

add artistNameHash

 

and it will be exactly like the artistName only it will have dashes if there are spaces?

 

And if there aren't any spaces, it will be exactly the same as the artistName?

Link to comment
Share on other sites

Ok, I added the hash entry to the artist table.

 

the link now looks like:

mysite.com/artist/The-Strokes/

 

and the rewrite looks like:

RewriteEngine on

 

RewriteRule ^artist/[a-zA-Z0-9-]+/$ /artist.php?artistName=$1

 

but when i click on the link I still get a 404 not found error.

As if, the page doesn't exist.

 

Does anyone know what could be wrong?

 

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.