Forum Moderators: phranque

Message Too Old, No Replies

need help with mode rewrite rule

php .htaccess and mode rewrite

         

vgabrovski

7:16 am on Sep 14, 2010 (gmt 0)

10+ Year Member



Hi guys

I desperately need help with the following:

A. I have this working fine

<a href="news_<?=$id?>.html" class="newscontent" target="<?=$target?>"><?=$title?></a>

it produces link "http://www.mysite.com/news_1.html" instead of "http://www.mysite.com/news_content.php?oid=$1"

then using .htaccess

"RewriteRule ^news_([0-9]+)\.html$" "http://www.mysite.com/company_news_content.php?oid=$1"


B. I want to make it work displaying the <?=$title?> in the link. So if the title in the MySql is something like "Obama is America's new PRESIDENT"

<a href="<?=$title?>_<?=$id?>.html" class="newscontent" target="<?=$target?>"><?=$title?></a>

it gives me link "http://www.mysite.com/Obama%20is%20America's%20new%20PRESIDENT_1.html"

now how should design the rewrite rule so it goes

"RewriteRule ^(?)_([0-9]+)\.html$" "http://www.mysite.com/company_news_content.php?oid=$1"

phranque

7:52 am on Sep 14, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], vgabrovski!

it depends on what is allowed in the title.
for example, if you know there will be no underscores in the title you can try something like this for a pattern:
^[^_]*_([0-9]+)\.html$
in other words, first you have to decide what makes a valid title string.

i should warn you that you are exposing yourself to a possible canonicalization problem with your plan, since http://www.example.com/news_1.html will still serve the same content as will http://www.example.com/any%20string_1.html unless you check in company_news_content.php that the title in the requested path matches the oid and otherwise return a 301 redirect to the canonical url or a 404 Not Found response.

vgabrovski

9:03 am on Sep 14, 2010 (gmt 0)

10+ Year Member



Thank you so much phrangue it worked perfectly :)