Display category and item name in url? .htaccess within...

Discuss practical ways rearrange URLs using mod_rewrite.

Display category and item name in url? .htaccess within...

Postby Bigtime » Tue Oct 06, 2009 12:12 pm

Hello,

Here is a sample page I would like rewritten:

http://www.movie-critique.com/review/review-item/2.php

I'd like the review-item to be replaced with the actual category name which is Action & Adventure. I would like the 2 to be replaced with the item name which is National Treasure. What changes need to be made to my current .htaccess file?

Here is the contents of my .htaccess file:

Code: Select all
IndexIgnore */*
Options +FollowSymlinks

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [S=4]

RewriteRule ^review-item/([^.]+)\.php$ index2.php?item_id=$1  [QSA,L]
RewriteRule ^review-category/([^.]+)\.php$ review_categories_yahoo_cats2.php?category=$1  [QSA,L]
RewriteRule ^reviewer/([^.]+)\.php$ reviewer_about.php?username=$1  [QSA,L]
RewriteRule ^comments/review_comments/([^/]+)/([^.]+)\.php$ comments/review_comments.php?item_id=$1&review_id=$2  [QSA,L]

RewriteRule ^([^.]+)\.html$ $1.php [QSA,L]



Thanks in advance and if required, I'm happy to pay for the help.

Tim
Bigtime
 
Posts: 4
Joined: Mon Nov 24, 2008 11:50 pm

Postby richardk » Tue Oct 06, 2009 12:40 pm

You can't do it just be editing the .htaccess file. FAQ: How to change a numeric ID into a name/title.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Bigtime » Tue Oct 06, 2009 1:17 pm

Ok, so if I leave the 2.php at the end how would I rewrite the following to make the url look like this?

http://www.movie-critique.com/review/na ... sure/2.php

Code: Select all
RewriteRule ^review-item/([^.]+)\.php$ index2.php?item_id=$1  [QSA,L]


Thank you,

Tim
Bigtime
 
Posts: 4
Joined: Mon Nov 24, 2008 11:50 pm

Postby richardk » Tue Oct 06, 2009 1:33 pm

You would just add /[^/]+ to match a slash and something after the slash
Code: Select all
RewriteRule ^review-item/[^/]+/([0-9]+)\.php$ index2.php?item_id=$1  [QSA,L]


If you wanted to pass it to your script (to make sure it is correct) you would use
Code: Select all
RewriteRule ^review-item/([^/]+)/([0-9]+)\.php$ index2.php?item_name=$1&item_id=$2  [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Bigtime » Tue Oct 06, 2009 2:08 pm

Excellent! I've been able to get it working the way I wanted.

Couple questions, is it better to change names like M & M to m-m on the php side or through mod_rewrite code?

Do you see any potential problems or ways of making my completed code better? I just found the snippet at the very end to remove the PHPSESSID from the end of the url. I'm hoping that's sufficient.

Code: Select all
IndexIgnore */*
Options +FollowSymlinks

RewriteEngine On


RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [S=4]

RewriteRule ^[^/]+/([0-9]+)-[^/]+\.php$ index2.php?item_id=$1  [QSA,L]
RewriteRule ^([0-9]+)-[^/]+\.php$ review_categories_yahoo_cats2.php?category=$1  [QSA,L]
RewriteRule ^review-categories\.php$ review_categories_yahoo_cats2.php  [QSA,L]
RewriteRule ^comments/review_comments/([^/]+)/([^.]+)\.php$ comments/review_comments.php?item_id=$1&review_id=$2  [QSA,L]
RewriteRule ^reviewer/([^.]+)\.php$ reviewer_about.php?username=$1  [QSA,L]



RewriteRule ^([^.]+)\.html$ $1.php [QSA,L]



# Skip the next two rewriterules if NOT a spider
RewriteCond %{HTTP_USER_AGENT}!(msnbot¦slurp¦googlebot) [NC]
RewriteRule .* - [S=2]
#
# case: leading and trailing parameters
RewriteCond %{QUERY_STRING} ^(.+)&PHPSESSID=[0-9a-z]+&(.+)$ [NC]
RewriteRule (.*) $1?%1&%2 [R=301,L]
#
# case: leading-only, trailing-only or no additional parameters
RewriteCond %{QUERY_STRING} ^(.+)&PHPSESSID=[0-9a-z]+$¦^PHPSESSID=[0-9a-z]+&?(.*)$ [NC]
RewriteRule (.*) $1?%1 [R=301,L]


Thanks for your help!!!!!!!

Tim
Bigtime
 
Posts: 4
Joined: Mon Nov 24, 2008 11:50 pm

Postby richardk » Wed Oct 07, 2009 11:45 am

I just found the snippet at the very end to remove the PHPSESSID from the end of the url.

Just for search engines, just for users or for both?

There a a few changes i would make:
Code: Select all
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [S=4]

Are those four rule likely to match existing files or directories? You could probably remove it.

I would do the redirects first, as after a redirect the mod_rewrite will be processed again.

The ¦s should be |s. Did you get it from webmasterworld?

There is a missing space after %{HTTP_USER_AGENT}.

You can try (or parts of)
Code: Select all
IndexIgnore */*
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_USER_AGENT} (msnbot|slurp|googlebot) [NC]
RewriteCond %{QUERY_STRING} ^(.+&)PHPSESSID=[0-9a-z]+&(.+)$ [NC,OR]
RewriteCond %{QUERY_STRING} ^(.+)&PHPSESSID=[0-9a-z]+$ [NC,OR]
RewriteCond %{QUERY_STRING} ^PHPSESSID=[0-9a-z]+&(.+)$ [NC]
RewriteRule ^(.*)$ /$1?%2%3 [R=301,L]

RewriteRule ^[^/]+/([0-9]+)-[^/]+\.php$ /index2.php?item_id=$1 [QSA,L]
RewriteRule ^([0-9]+)-[^/]+\.php$ /review_categories_yahoo_cats2.php?category=$1 [QSA,L]
RewriteRule ^review-categories\.php$ /review_categories_yahoo_cats2.php [QSA,L]
RewriteRule ^comments/review_comments/([^/]+)/([^.]+)\.php$ /comments/review_comments.php?item_id=$1&review_id=$2 [QSA,L]
RewriteRule ^reviewer/([^.]+)\.php$ /reviewer_about.php?username=$1 [QSA,L]

RewriteRule ^([^.]+)\.html$ /$1.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Exabot [Bot] and 1 guest

cron