| |||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | #1 (permalink) |
| Account Closed | Help with MOD_REWRITE PLZ! Hi, currently building something for a site i am going to relaunch ![]() I want index.php?id=3 rewritten so it makes it show the name like this name-nextword-lastword_1.html and so on. You get the point, how would i do that? If you need to see the code i use i can supply it to honest people. |
| |
| | #2 (permalink) |
| Traveller | Do you have 3 = name-nextword-lastword stored anywhere e.g. a database? Or is the name-nextword-lastword simply hardcoded in the href? Here is an example to turn /3/ into index.php?id=3 Options +FollowSymLinks Options +Indexes RewriteEngine On RewriteBase / RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^/([^/]+)/?$ index.php?id=$1 [NC,L] If the name-nextword-lastword is simply hardcoded in the href, you could do: /3/name-nextword-lastword/ Options +FollowSymLinks Options +Indexes RewriteEngine On RewriteBase / RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^/([^/]+)/([^/]+)/?$ index.php?id=$1 [NC,L] disclaimer: I'm no mod_rewrite master
__________________ Internet.geek.nz NameCooler.com Unlimited Domain Name Web Hosting Travel Money Rates |
| |
| | #4 (permalink) |
| Traveller | The good news: You're in good shape if you've got the titles/id's in the DB! The bad news: I think you're going to need some additional php to: get the title from the url modify it so that it looks like the title in the db query the db to get the ID e.g. url: /name-nextword-lastword/ rewrite: RewriteRule ^/([^/]+)/?$ index.php?name=$1 [NC,L] index.php (pseudo code) $dbname = str_replace('-', ' ', $_GET['name']); (replace the underscores with spaces) select id from database where title = $dbname Oh erm, just to clarify, your example url: name-nextword-lastword_1.html "1" isnt the ID is it?
__________________ Internet.geek.nz NameCooler.com Unlimited Domain Name Web Hosting Travel Money Rates |
| |
| | #6 (permalink) |
| Traveller | Sorry, I'm at work, can only reply when the boss is not looking!
__________________ Internet.geek.nz NameCooler.com Unlimited Domain Name Web Hosting Travel Money Rates |
| |
| | #9 (permalink) |
| Traveller | Yeah that should work great as long as that number at the end is the ID! (I was wondering if I had overlooked the obvious there....) Nice regexp skills Dan! ![]() edit: just noticed your title in your postbit, love the subliminal advertising message!
__________________ Internet.geek.nz NameCooler.com Unlimited Domain Name Web Hosting Travel Money Rates |
| |
| | #11 (permalink) |
| Traveller | ^[_a-zA-Z0-9]*-[_a-zA-Z0-9]*-[_a-zA-Z0-9]*-([0-9]*)?\.html Actually, there is an underscore at the end, so should it be: ^[_a-zA-Z0-9]*-[_a-zA-Z0-9]*-[_a-zA-Z0-9]*_([0-9]*)?\.html or if underscore is a special char in .htaccess, it would be: ^[_a-zA-Z0-9]*-[_a-zA-Z0-9]*-[_a-zA-Z0-9]*\_([0-9]*)?\.html JuggernautH, it should work fine as long as there is only ever 3 words including review. Are you asking what if you have any number of hypen delimited words before the review_{id}.html?
__________________ Internet.geek.nz NameCooler.com Unlimited Domain Name Web Hosting Travel Money Rates |
| |
| | #13 (permalink) |
| Traveller | hmm, should be easy actually! I guess Dan will be able to correct me if I'm wrong, but in that case, all we care about is the _{number}.html So, here goes: ^[\-_a-zA-Z0-9]*\_([0-9]*)?\.html There's definitely a better regex than that though, here's another attempt: ^/([^/\_]+)\_([0-9]*)?\.html Guess I should actually fire up apache and test!
__________________ Internet.geek.nz NameCooler.com Unlimited Domain Name Web Hosting Travel Money Rates |
| |
| | #14 (permalink) |
| Account Closed | Could you write me up the whole code for it? Kind of novice on this stuff myself :P Tryed for a while myself and i managed to get it working Thanks for all help!Code: RewriteRule ^review_([0-9]+).html reviews.php?id=$1 [L,NC] Dan & RCRiver: Go to www.hideurl.info/reviews.html to check the script live ![]() If you want i can review one each of your sites for free and put it there Last edited by JuggernautH; 05-17-2007 at 05:00 PM. |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |