1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

FAQ: mod_rewrite, 301 redirects, and optimizing Apache.

Discussion in 'Apache' started by Nintendo, Jul 30, 2005.

  1. namxas

    namxas Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #21
    nice post, thanks
     
    namxas, Aug 12, 2005 IP
  2. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #22
    This expression will match

    store/d1/d2/d3/f4.html

    because regex by default will grab as much as it can and $1 will be /d1/d2/d3 - not something you really want. Use this one instead:

    ^store/([^/]+)/([^/]+)\.html$

    J.D.
     
    J.D., Aug 13, 2005 IP
  3. guru-seo

    guru-seo Peon

    Messages:
    2,509
    Likes Received:
    152
    Best Answers:
    0
    Trophy Points:
    0
    #23
    HI everyone,

    Thanks for a great guide. I had a quick question. I have mod_rewrite and my PHP pages setup as so:

    /page_detail.php?pageID=1 <-- the number is the incremental unique ID from the MySQL database
    so the rest of the content pages are:
    /page_detail.php?pageID=2
    /page_detail.php?pageID=3
    ...and so on.

    my .htaccess file looks like this:
    ---------------------------------------------------
    RewriteEngine on
    Options +Indexes
    Options +FollowSymlinks
    RewriteRule -([0-9]+).html page_detail.php?pageID=$1 [L]
    ---------------------------------------------------
    and my generated pages look like this:
    ---------------------------------------------------
    "/page-title-here-1.html"

    Note the trailing "-1" at the end of the name which is the unique ID from the DB. How can I get rid of it so I can see
    "/page-title-here.html" without the "-1"


    Any help would be greatly appreciated.

    Thanks
     
    guru-seo, Aug 16, 2005 IP
  4. eiso

    eiso Peon

    Messages:
    583
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #24
    Great that you started this thread,

    I have tried multiple codes in .htaccess, Including the one above me.

    But they don't work and the links stay dynamic.

    Could you please help me out.

    Best regards,

    Eiso
     
    eiso, Aug 16, 2005 IP
  5. expat

    expat Stranger from a far land

    Messages:
    873
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #25
    guru seo

    you can't really get rid of the dynamic element.
    As apache translates a static htm into your require code how would it know that mysite.htm refers to mysite.php?id=8 ??

    you can get rid of the - so it looks like page1 page2 page3 etc.

    RewriteRule page([0-9]+).html page_detail.php?pageID=$1 [L]

    Also how would SE's determine various pages all mypage.htm but with various titles etc etc...?

    If you have unique titles you could use these instead and do the db lookup based on title

    e.g.
    mypage.php?subject=How_to_do_mod_rewrite

    RewriteRule subject_([A-zA-Z_]+).htm page_detail.php?subject=$1 [L]

    could give you a translatable static htm like

    mmydomain.xxx/subject_how_to_do_mod_rewrite.htm

    in your php you can than look up the content for the titel instead of using page id

    Expat
     
    expat, Aug 16, 2005 IP
  6. guru-seo

    guru-seo Peon

    Messages:
    2,509
    Likes Received:
    152
    Best Answers:
    0
    Trophy Points:
    0
    #26

    Expat, I was thinking among the same lines, the work around would be to change the SQL query to the page_detail.php?page_title=page_title as opposed to the pageID. I will give it a shot.

    Thanks again for your feedback, it is greatly appreciated.
     
    guru-seo, Aug 16, 2005 IP
    guerilla and GRIM like this.
  7. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #27
    Make sure you quote the period - otherwise your rule will match something like page1_html. Also, unless you are trying to match a sub-word, make sure to prefix the regex with the caret, otherwise it will match something like this_is_my_page1_html.html.

    J.D.
     
    J.D., Aug 16, 2005 IP
  8. iqra_psh

    iqra_psh Active Member

    Messages:
    992
    Likes Received:
    113
    Best Answers:
    0
    Trophy Points:
    85
    #28
    Thanks for this good post.
    repes added.
     
    iqra_psh, Aug 17, 2005 IP
  9. Yukio

    Yukio Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #29
    Hmm, is it really worth doing this considering a lot of serach engines are getting better with indexing dynamic URLS?

    Won't it increase server load?

    I've got a website that uses the index.php?page= format... and I link to all my pages using this format. If I implement this, will I need to change the way I link to my pages or is it all automated?
     
    Yukio, Aug 18, 2005 IP
  10. kjewat

    kjewat Active Member

    Messages:
    149
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #30
    Greate post!

    I am looking forward to try this out :)
     
    kjewat, Aug 19, 2005 IP
  11. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #31
    I've never noticed any server load increase. And I have about 46 Rewrite lines on my Amazon stores. The only time my server was ever effected was when I got Googlebombed!!!! (ie Google going through your sites so fast that it slows down, crashes or almost crashes the server.) Solution to that was optimizing Apache.
     
    Nintendo, Aug 19, 2005 IP
  12. Yukio

    Yukio Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #32
    Awsome, seems to be working great on my site.

    It however messes up the ad codes for some reason. Strange... would there be any reason to this? Some of the ads default to a normal *.html page on the server... so when it does do this, it also loads the index.php template as well...

    Is there a way to fix this?
     
    Yukio, Aug 20, 2005 IP
  13. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #33
    Make rewrited URLs have an extension that you don't use any where else. Don't use .htm any where? Rewrite all the URLs to end with that.
     
    Nintendo, Aug 20, 2005 IP
  14. Yukio

    Yukio Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #34
    Thanks =] Works great!
     
    Yukio, Aug 20, 2005 IP
  15. Yukio

    Yukio Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #35
    Hmm as a general query, if the urls on the site is in the form of index.php?page=whatever etc... is it worth using mod rewrite considering search engines are able to index these dynamic urls?
     
    Yukio, Aug 20, 2005 IP
  16. expat

    expat Stranger from a far land

    Messages:
    873
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #36
    Let me ask a question: regardless of mod_rewrite or not do your dynamic pages have dynamic title and description as well? As they obvously have dynamic content.

    Because if you where a SE would you index and show 20 pages with the same title (and to a lesser extend description)?

    For listings in minor engines and directorytype sites as well as deeplink exchanges a mod_rewrite page url is easier for all to handle.
    The major se's all happily index two vars (?xxx=xxx&yyy=yyy) but they hate same titel....

    e.g its easier and better to ask a webmaster to link to /sms-arrivals-lhr.htm than to link to /sms.htm?type=arrivals&apt=lhr

    Cheers
    Expat
     
    expat, Aug 20, 2005 IP
  17. Yukio

    Yukio Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #37
    Hmm point taken. I've installed the mod rewrite because it makes everything look so much neater... but I am running into some trouble with the title.

    Basically, I load all my pages within a template index.php and thus, all the site pages have the same title --> I've also noticed this problem when I index the site pages in the site search engine I use.

    Short from going into every single file (which I don't think will do anything because I've tried inserting meta and title tags in the content pages but they're 'overwritten' by being loaded by the index.php template file), what other options would be possible?

    Could a similar mod like mod_rewrite be the answer?
     
    Yukio, Aug 20, 2005 IP
  18. expat

    expat Stranger from a far land

    Messages:
    873
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #38
    don't want to digress too much but using the vars to injecting them in the title is one way. I have title and desc in the content db's and use a default if non found.
    if
    mod-rew /sms-arrival-heathrow-lhr.htm
    from
    sms.htm?type=arrival&apt=heathrow&cde=lhr

    <title>Your SMS service for <?php echo $type?> at <?php echo $apt." (".$cde ?>) blah blah blah</titel>
    is the easy sollution......

    Expat
     
    expat, Aug 20, 2005 IP
  19. Yukio

    Yukio Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #39
    Thanks expat and everyone in this thread ^_^ Your help much appreciated.

    The dynamic titles now exists (woohoo!) and the mod_rewrite successfully installed :)
     
    Yukio, Aug 20, 2005 IP
  20. davert

    davert Banned

    Messages:
    345
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #40
    OK, I've been going nuts with this one..maybe you can help?

    I've been trying the infamous http://domain.com to http://www.domain.com redirect.

    I must have done a dozen different ones. None worked.

    Why won't this one work?

    RewriteEngine On
    RewriteRule http://domain.com/* http://www.domain.com/$1 [L,R=301]

    (Note: I did get one to work but it turned http://www.domain.com/ into http://www.domain.com// which would bring the same problem...!)

    PS> I had to do a few rewrites for other purposes and no, didn't notice any extra CPU time.
     
    davert, Aug 24, 2005 IP