TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   mod_rewrite for page title (http://www.talkphp.com/absolute-beginners/5397-mod_rewrite-page-title.html)

MicroHellas 04-05-2010 04:49 PM

mod_rewrite for page title
 
Hello,

Maybe not beginner in PHP but totally novice when it comes to Apache files. So, I'll appreciate your patience. The problem that I want to resolve is:

With PHP functions and parametters, I'm able to pass to the page as title eg a Classified's title. The problem comes when I want to make the url:
Code:

classifieds.php?do=viewitem&itemid=xxx&title=xxx
to look like:
Code:

classifieds/title_here.html
I've tried many possible ways following tutorials but I was unable to make it work:-/ My last try is:
Code:

<IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule ^classifieds/([0-9]+)-[^\/]+\.html$ classifieds.php?do=viewitem&itemid=$1 [L]
</IfModule>

You can check my page at:
http://www.microdemosite.com/dev4/cl...item&itemid=53
(even if I need to change a bit the code to replace spaces with underscores).

Thank you in advance
Maria

delayedinsanity 04-05-2010 05:05 PM

It looks like you're attempting to create the following structure, correct me if I'm wrong;

/classifieds/{$itemid}_{$title}.html

The following should do the trick:

Code:

RewriteEngine On
RewriteRule ^classifieds/([0-9]+)_([^.]+)\.html$ classifieds.php?do=viewitem&itemid=$1&title=$2 [L]

The first capture looks for a set of numbers, which you have accomplished on your own. The second capture after the underscore is looking for any group of characters up until it finds a period. Since it's enclosed in a character range, you don't have to escape it as a control character. Outside of the range it does need to be escaped, as shown here.

Hth

Edit: This allows a lot of freedom in how the title is presented. If you want to ensure that it's only alphabetical, or alphanumerical, replace [^.] with [A-Za-z] or [A-Za-z0-9] or finally [a-z] with NC added at the end [NC,L]

MicroHellas 04-05-2010 05:25 PM

Thank you for your help, but unfortunatelly it dosen't works.
http://www.microdemosite.com/dev4/cl...item&itemid=15

If you visit the page you'll see the title to be as I want to be in the URL.

Maria

delayedinsanity 04-05-2010 06:45 PM

Works fine for me, I tested it locally. If you just need itemid and not title like the url you posted suggests, then you might try this:

Code:

RewriteRule ^classifieds/([0-9]+)_[^.]+\.html$ classifieds.php?do=viewitem&itemid=$1 [L]
That simply passes the itemid, and lets you add anything you want after it.


All times are GMT. The time now is 09:28 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0