FAQ: mod_rewrite, 301 redirects, and optimizing Apache.

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

  1. maildeepak

    maildeepak Peon

    Messages:
    220
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #261
    ok...i got the redirection working now...:D ...from /index.html to www.domain.com

    dont ask me what this code means...i got this code from Webmaster world forum

    thanx nintendo for all the support...:)
     
    maildeepak, Apr 13, 2006 IP
  2. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #262
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^viewoffer\-([^.]+)\.html$ view_offer.php?id=$1 [L]
    RewriteRule ^([^.]+)\.html$ $1.php [L]
    RewriteCond %{HTTP_HOST} !^www\..* [NC]
    RewriteRule ^([^.]+)$ http://www.%{HTTP_HOST}/$1 [R=301]
    RedirectMatch permanent ^/gen_confirm.php$ http://www.my site.com


    php to html will only work if this is with every file, even the index file.
     
    Nintendo, Apr 13, 2006 IP
  3. mgrohan

    mgrohan Active Member

    Messages:
    671
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    58
    #263
    Thanks Nintendo but tried your suggestion with no luck.

    The permanent redirect (that was already there) [no www. to www] works. But all the others show the same .php extensions unchanged.

    Would like to get rid of these and change em to .html or / , but like i said your suggestion is not working. Any idea why?
     
    mgrohan, Apr 13, 2006 IP
  4. mgrohan

    mgrohan Active Member

    Messages:
    671
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    58
    #264
    Sorry, i was wrong even the permanent redirect [no www to .www] is not working now. only if i change the ([^.]+) to (.*) does the permanent redirect work. This doesn't work on the others though.
     
    mgrohan, Apr 13, 2006 IP
  5. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #265
    mod_rewrite doesn't change the links.

    Does

    mysite.com/viewoffer-71.html

    work?
     
    Nintendo, Apr 14, 2006 IP
  6. mgrohan

    mgrohan Active Member

    Messages:
    671
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    58
    #266
    Thanks! was a bit mixed up.
    Got confused with how the redirect works.
    The rewritten url's work.

    But how would i make it to make all the new urls if typed: selloffers.php will redirect to selloffers.htm and view_offer?id=71 will redirect to view_offer-71.htm?

    Again thanks for the help
     
    mgrohan, Apr 14, 2006 IP
  7. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #267
    If you try to do both a redirect and change the URLs, you'll get an infinite loop.
     
    Nintendo, Apr 14, 2006 IP
  8. TTSEO

    TTSEO Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #268
    I am trying to make an ecommerce site SEO-friendly. The site uses an undocumented perl sript provided by the hosting company.

    Product pages look like this:
    domain.com/cgibin/shop?info=item&sid=12345678

    Using mod_rewrite I can make them look static like
    domain.com/info/item/12345678

    But is there any way to use .htaccess to drop the sessionid for robots or serve them a static id?

    I thought about something like:
    IF HTTP_USER_AGENT contains google, msnbot
    THEN rewrite whatever sessionid to
    domain.com/info/item/robot123

    But how to do this? I am new to .htaccess and that is a bit too advanced to me ...

    And I know, that this is NOT a great solution, but I do not see any other :(
    Some better suggestions?
    (I do not have access to the perl source, only to html-templates).

    Any help greatly appreciated.
     
    TTSEO, Apr 18, 2006 IP
  9. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #269
    The script would be where to do that. The script makes the fake URLs, and mod_rewrite only makes the fake URLs work. It might be a good idea to get a web host where you have access to the script.

    I'm not sure if you can, but I'm guessing you might be able to do a If HTTP_USER_AGENT...do a 301 redirect to new URL some how.
     
    Nintendo, Apr 18, 2006 IP
  10. vishwaa

    vishwaa Well-Known Member

    Messages:
    271
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #270
    All my pages are mod rewrited to .html extension. It works great. Thanks Ninno.

    But SE bots still keep hitting my old .php files even though I have no links to them anywhere in my site.

    Hence I try to redirect the bots to new static URL in the following way.

    Kindly correct this if im wrong.

    Method#1

    #Redirecting direct file request to mod-rewritten urls
    RewriteCond %{THE_REQUEST} ^(.*)\.php\ HTTP/
    RewriteRule ^(.*)\.php$ http://www.example.com/$1.html [R=301,L]

    #Rewriting for static URLs
    RewriteCond %{REQUEST_FILENAME} !-s
    RewriteRule ^(.*)\.html$ $1.php [T=application/x-httpd-php,L]


    #Method 2

    RewriteCond %{HTTP_USER_AGENT} ^Google.* [OR]
    RewriteCond %{HTTP_USER_AGENT} ^Yahoo.* [OR]
    RewriteCond %{HTTP_USER_AGENT} ^msn.*
    RewriteCond %{HTTP_REFERER} ^$
    RewriteCond %{REQUEST_FILENAME} ^(.*)\.php
    RewriteRule ^(.*)\.php$ http://www.example.com/$1.html [R=permanent,L]


    Method #1(derived from maildeepak's post) works fine for me without any problem.
     
    vishwaa, Apr 21, 2006 IP
  11. soul-healer

    soul-healer Peon

    Messages:
    1,459
    Likes Received:
    145
    Best Answers:
    0
    Trophy Points:
    0
    #271
    anything wrong with this code. This code is working fine on my old server but not working new server Kindly advice.

    
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^downloads/songs/(.*)\.html$ downloads/songs/downloads.php?path=$1 [L]
    RewriteRule Pakistani-Music(.*)\.html$ list.all.downloads.php?page=$1 [L]
    RewriteRule ^PakistaniMusic/(.*)\.html$ downloads/songs/download.php?filename=$1 [L]
    
    
    Code (markup):
     
    soul-healer, Apr 22, 2006 IP
  12. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #272
    Odds are the server is on crack.

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^downloads/songs/([^.]+)\.html$ downloads/songs/downloads.php?path=$1 [L]
    RewriteRule ^Pakistani\-Music([^.]+)\.html$ list.all.downloads.php?page=$1 [L]
    RewriteRule ^PakistaniMusic/([^.]+)\.html$ downloads/songs/download.php?filename=$1 [L]
     
    Nintendo, Apr 22, 2006 IP
  13. Dekker

    Dekker Peon

    Messages:
    4,185
    Likes Received:
    287
    Best Answers:
    0
    Trophy Points:
    0
    #273
    delicious delicoius crack
     
    Dekker, Apr 22, 2006 IP
  14. soul-healer

    soul-healer Peon

    Messages:
    1,459
    Likes Received:
    145
    Best Answers:
    0
    Trophy Points:
    0
    #274
    Still not working. I check via phpinfo and it shows me mod_rewrite is working. I talked to tech team and they are saying mod_rewrite is enable. I am Crack i am facing this problem for the last 3 days .

    :(
     
    soul-healer, Apr 24, 2006 IP
  15. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #275
    Do this test...

    domain.com/.htaccess

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^wacko\.html$ index.html [L]

    Change index.html to what ever the index file is, then go to

    domain.com/wacko.html

    if that doesn't show the index page, mod_rewrite doesn't work!!!
     
    Nintendo, Apr 24, 2006 IP
  16. soul-healer

    soul-healer Peon

    Messages:
    1,459
    Likes Received:
    145
    Best Answers:
    0
    Trophy Points:
    0
    #276
    :) it is working. Thanks alot :)
     
    soul-healer, Apr 24, 2006 IP
  17. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #277
    er...did you get the other code to work??!!! :confused:
     
    Nintendo, Apr 24, 2006 IP
  18. soul-healer

    soul-healer Peon

    Messages:
    1,459
    Likes Received:
    145
    Best Answers:
    0
    Trophy Points:
    0
    #278
    No. the problem is with Server directroy structure the temporary path they given me for files to restore isnt working with mod_rewrite.

    I trasnfered a temporary domain to the server to check mod_rewrite working or not using the same code and it works and i hope it works for my primary domain aswell now going to change the DNS of domain :)
     
    soul-healer, Apr 24, 2006 IP
  19. vishwaa

    vishwaa Well-Known Member

    Messages:
    271
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #279
    Is it possible to exclude specific folders (like admin area) from mod rewriting?

    Im using like
    to prevent admin urls rewritten.
    Is there any right(short) method for the same?
     
    vishwaa, Apr 25, 2006 IP
  20. myhekim

    myhekim Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #280
    if you wanted to buy godaddy hosting i sad that godaddy don't have mod rwrite mod... i chech alot of mail but they don't help me... how can i add url mod manuel to my account?
     
    myhekim, Apr 26, 2006 IP