Forum Moderators: phranque

Message Too Old, No Replies

htaccess mod rewrite and search engines?

search engines, htaccess, rewrite

         

JayDev

7:05 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



Hello Everyone,

Htaccess files allow rewriting pages into whatever one wants. However, something is confusing to me.

Here is my htaccess

Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.html$ /my-page\.php?id=$1 [nc]

which makes my-page.php?id=my-page-title

become

my-page-title.html

My question is how will the search engines index the site. I am assuming the SEs will index as php? or html? I was just curious to know.

If the engines do index in php, then what is the point of the .htaccess file with regard to creating html extensions.

Any remarks or clarification on this topic would be appreciated.

Thank you

hannamyluv

8:05 pm on Dec 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



they will index as html. What they see is what they index.

jdMorgan

8:23 pm on Dec 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



which makes which makes my-page.php?id=my-page-title

become

my-page-title.html

Actually, it does the reverse. It changes any request for my-page-title.html into a call to your script
at my-page.php?id=my-page-title

> If the engines do index in php, then what is the point of the .htaccess file with regard to creating html extensions.

The mod_rewrite code is half of the job. Change your php pages (using preg_replace) to output html page links. This is what the visitors and search engines will see. Then when they request an html page, your htaccess code will change the request back into a call to your script.

Jim

JayDev

9:10 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



Actually, it does the reverse. It changes any request for my-page-title.html into a call to your script
at my-page.php?id=my-page-title

Thank you jdMorgan. What do I need to do then since what I am trying to do is make my php pages read as html pages. It seems like I accomplished the reverse of what I want to happen according to your post.

Also, when you say I should use the preg_replace function, could you tell me what you mean exactly and what it would help me accomplish? Do you have an example of preg_replace and its impact?

Do you have links to introduce the concept and use of preg_replace and what its purpose is?

Thank you for your feedback.

jdMorgan

5:39 am on Dec 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What you need to do:

  • On your pages, link to "my-page-title.html", not to "my-page.php?id=my-page-title". The PHP string-replacement function preg_replace is often useful if you use a database to build your pages. This is a PHP subject, and would best be discussed in the PHP forum.

  • Having changed all your on-page links to the "my-page-title.html" form, search engines and humans will use those URLs when requesting pages from your site. But those URLs don't exist as files on your server. So, you need some mechanism to "connect" requests for those URLs back to your script in the "my-page.php?id=my-page-title" form, which is exactly what your mod_rewrite code will do.

    [mod_rewrite works *after* a (URL) request is received by the server, but *before* any content (file or script) is served. That is critical to understanding this sequence.]

    All you are missing is the first step, and there's nothing wrong with what you coded. You just need to change the links on your PHP pages, too. (This technique is quite common: Note that all pages at WebmasterWorld are dynamically-generated, but look at those nice static URLs in your browser address bar!)

    Jim

  • JayDev

    3:58 pm on Dec 15, 2004 (gmt 0)

    10+ Year Member



    What about the urls? Although they are html extensions, the pages will still include the php code. Does it make a difference with regard to search engines? The pages will be html extensions along with html links within the pages, but what about all the php language and code within the page?

    Is it better to code in order to generate the html pages through the help of a conversion software, if any?
    Do you know of any program which helps you generate html pages from your MySQL db?

    Thanks

    bcolflesh

    4:01 pm on Dec 15, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    ...but what about all the php language and code within the page?

    PHP is interpreted on the server - the client gets the HTML output (unless the server is misconfigured).

    JayDev

    5:47 pm on Dec 15, 2004 (gmt 0)

    10+ Year Member



    Thanks for the input. Excuse if sometimes I don't make sense but I get information overload as I learn php once in a while:)

    I checked my pages and the php code does not appear in the page. My bad.

    Still, how can I learn about preg_replace as I need to change my links from php to html?

    Thank you.

    JayDev

    6:13 pm on Dec 15, 2004 (gmt 0)

    10+ Year Member



    ok I am trying to get this preg_replace right:

    if I enter this code in the page where I need to change the links:

    $newhrefs = preg_replace("/home-furnishing.php\?addition2=(.*)/", "$1\.html", $hrefs);

    would it accomplish what I want:

    Example:

    home-furnishing.php?addition2=my-title

    would want to make it through preg_replace as:

    my-title.html

    I am doing it correctly. If not, what do I need to change and/or add?

    Thanks.

    bcolflesh

    6:13 pm on Dec 15, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    [us2.php.net...]

    <edit>
    Post above beat me.
    </edit>

    jdMorgan

    6:15 pm on Dec 15, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Have you tried a search [google.com]?

    Jim

    JayDev

    8:42 pm on Dec 15, 2004 (gmt 0)

    10+ Year Member



    jdMorgan: yes I have tried to figure it out for a while now. It is getting frustrating though!

    I spent a while reading a tutorial which made sense and here is what I got as a result:

    $str = preg_replace("/home-furnishing\.php\?addition2=(.*?)/", "$1\.html", $str);

    this should make for example

    home-furnishing.php?addition2=whatever-table-field-data-text

    into

    whatever-table-field-data-text.html

    I really am getting annoyed as this is not accomplishing anything. Am I writing something wrong? (Obviously I seem to do so)

    Things seem to make more sense now but still not clicking.:(

    jdMorgan

    1:55 am on Dec 16, 2004 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    I'm no php guru, but this should be closer...

    $str = preg_replace("/home-furnishing\.php\?addition2=(.+)/","$1.html",$str);

    Jim

    coopster

    2:26 am on Dec 16, 2004 (gmt 0)

    WebmasterWorld Administrator 10+ Year Member



    >>I'm no php guru

    Modesty, modesty -- what a virtue ... this coming from a guy that can write regular expressions in his sleep for cyring out loud ;)

    I'd add one modifier, just to make it case-insensitive:

    $str = preg_replace("/home-furnishing\.php\?addition2=(.+)/i", "$1.html", $str);

    JayDev

    4:31 am on Dec 16, 2004 (gmt 0)

    10+ Year Member



    Thank you so much for your feedback. However I am still struggling with preg_replace. I never thought i would spend so much time on it.

    Here is my php code from the source:

    <a href="home-furnishing.php?addition2=<?php echo $row_nav_menu['addition2'];?>

    Therefore I added what you modified to my php code above the <head> tag on my php pages.

    As you know, what I want to accomplish is have the value of 'addition2' to become some-page.html

    Therefore the code preg_replace:

    $str = preg_replace("/home-furnishing\.php\?addition2=(.+)/i", "$1.html", $str);

    Maybe I am not using the right code but that is driving me crazy!

    all I want is instead of having for example:

    home-furnishing.php?addition2=some-page

    I want to have

    some-page.html (when I test in the browser, the links are still in php such as home-furnishing.php?addition2=some-page instead of some-page.html)

    I am so close to being done, this preg_replace drives me crazy.

    Thanks

    tqatsju

    8:03 am on Dec 16, 2004 (gmt 0)

    10+ Year Member



    i have a question about mod_rewrite, specifically rewrite rule. Anyway i think it's a simple one here goes. I have in my .htaccess file these lines:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteRule index.php?ad=blah [mydomain.com[R=301,L]...]

    since for whatever reason google is recognizing the index.php?ad=blah as our main site i want to permanently rewrite it to index.php. But this little tid bit above isn't working, not sure why? When i go to http...index.php?ad=blah it doesn't rewrite to index.php...
    thanks.

    coopster

    1:00 pm on Dec 16, 2004 (gmt 0)

    WebmasterWorld Administrator 10+ Year Member



    You are building the link right there in your php script so you can do one of two things. Simply build the link via concatenation or use a regular expression to turn it around for you. Since we have started down the regular expression road, we'll finish that route.
    // Instead of this... 
    <a href="home-furnishing.php?addition2=<?php echo $row_nav_menu['addition2'];
    // ...try building the link prior to inserting into the html instead:
    $str = 'home-furnishing.php?addition2=' . $row_nav_menu['addition2'];
    $str = preg_replace("/home-furnishing\.php\?addition2=(.+)/i", "$1.html", $str);
    ?>
    <a href="<?php echo $str;?>">

    JayDev

    6:50 pm on Dec 16, 2004 (gmt 0)

    10+ Year Member



    Thank you so much for your help. It finally works after Coopster's post.

    Only one problem, in my category links, the repeat region no longer works: in other words, the link is always the same, which is the one for the first value in my table. It does not pick up the rest of the values in order to link to the appropriate data/page.

    Here is the modified code along with the code for the repeat region. I am guessing that I need to change some variable name somewhere in the code and have tried a few changes without success.


    <?php do {?>
    <a href="home-furnishing.php?addition2=<?php echo $row_nav_menu['addition2'];?>" class="main_menu"><?php echo $row_nav_menu['cat'];?></a><br>
    <?php } while ($row_nav_menu = mysql_fetch_assoc($nav_menu));?>

    Which creates in the browser the following issue:


    <a href="All-About-Home-Furnishing.html" class="main_menu">All About Home Furnishing</a><br>
    <a href="All-About-Home-Furnishing.html" class="main_menu">Bathroom Decorating</a><br>
    <a href="All-About-Home-Furnishing.html" class="main_menu">Kitchen Design & Decorating</a><br>
    <a href="All-About-Home-Furnishing.html" class="main_menu">Living Room Furniture</a>

    Thanks for the help. Almost there!

    JayDev

    9:36 pm on Dec 17, 2004 (gmt 0)

    10+ Year Member



    The modified code (bold) in my link is:

    <?php do {?>
    <a href="<?php echo $str;?>" class="main_menu"><?php echo $row_nav_menu['cat'];?></a><br>
    <?php } while ($row_nav_menu = mysql_fetch_assoc($nav_menu));?>

    And I get the the above html links, all pointing to the same html page. However, in my table, each category name has a different link. Therefore I don't understand what is wrong.

    This is what I added as told by Coopster to my php:

    $str = 'home-furnishing.php?addition2=' . $row_nav_menu['addition2'];
    $str = preg_replace("/home-furnishing\.php\?addition2=(.+)/i", "$1.html", $str);?>

    The above updates work great as I said except that I get in the browser the links:

    <a href="All-About-Home-Furnishing.html" class="main_menu">All About Home Furnishing</a><br>
    <a href="All-About-Home-Furnishing.html" class="main_menu">Bathroom Decorating</a><br>
    <a href="All-About-Home-Furnishing.html" class="main_menu">Kitchen Design & Decorating</a><br>

    when I should get

    <a href="All-About-Home-Furnishing.html" class="main_menu">All About Home Furnishing</a><br>
    <a href="Bathroom-Decorating.html" class="main_menu">Bathroom Decorating</a><br>
    <a href="Kitchen-Design &-Decorating.html" class="main_menu">Kitchen Design & Decorating</a><br>

    This is my last issue before I can get it to work. I have tried a few changes without success.
    Any help would be great

    Thanks

    coopster

    2:36 pm on Dec 19, 2004 (gmt 0)

    WebmasterWorld Administrator 10+ Year Member



    If the link is changing, you need to also dynamically change the link you are trying to "find" in the pattern part of your regular expression. That's what jdMorgan was trying to explain to you earlier.