FAQ: mod_rewrite, 301 redirects, and optimizing Apache.

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

  1. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #521
    You can't have the URLs not have the language code in the URL. There would be no way for apache to know what script the URL is from. Something has to be unique in each language URL.
     
    Nintendo, Mar 13, 2007 IP
  2. clover

    clover Peon

    Messages:
    472
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #522
    hi nintedo,

    yes i agree with you.

    i have no problem with

    example.com/lang/de/page.html for german
    example.com/lang/fr/page.html for french

    its just on exiting via my jump script.

    all jump links on

    example.com/lang/fr/page.html is redirected to the destination URL IN FRENCH :)

    is there a catch all codes for say

    all

    example.com/lang/de/jump-xxx.php
    example.com/lang/ru/jump-xxx.php
    example.com/lang/fr/jump-xxx.php
    example.com/lang/es/jump-xxx.php
    example.com/lang/jp/jump-xxx.php
    ... etc

    would be redirected to

    example.com/jump-xxx.php

    ??

    thank you very much.
     
    clover, Mar 13, 2007 IP
  3. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #523
    RewriteRule ^jump-(.*).asp$ jump.php?id=$1 [L]
    RewriteRule ^jump-(.*).asp$ language/de/jump-$1 [L]
    RewriteRule ^jump-(.*).asp$ language/es/jump-$1 [L]
    RewriteRule ^jump-(.*).asp$ language/fr/jump-$1 [L]

    will never work because apache has no idea which script to get, since all the scripts are fighting over each other at the exit URL.

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^([^.]+)/jump\-([^.]+)\.asp$ language/$1/jump.php?id=$2 [L]

    domain.com/language/de/jump.php?id=XXX
    being the same as
    domain.com/de/jump-XXX.asp

    And if you want it search engine friendly, you'll do .html instead of .asp or .php.
     
    Nintendo, Mar 14, 2007 IP
  4. clover

    clover Peon

    Messages:
    472
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #524
    hi nintendo,

    it is still acting like

    domain.com/language/de/jump-xxx.asp

    i think i should also include that on /language folder there is also an .htaccess for the language translator

    the .htaccess on /language folder.
    ------------------
    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{QUERY_STRING} !^$
    RewriteRule ^(.*)\.asp$ /language/$1.asp? [R=301]
    RewriteRule ^([A-Za-z0-9_-]+)(/.*)$ index.php?language=$1&page=$2 [L]
    -----------------------

    it may be messing with the rule on the root .htaccess.
     
    clover, Mar 14, 2007 IP
  5. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #525
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^([^.]+)/jump\-([^.]+)\.asp$ language/$1/jump.php?id=$2 [L]

    RewriteCond %{QUERY_STRING} !^$
    RewriteRule ^language/(.*)\.asp$ language/$1.asp? [R=301]
    RewriteRule ^language/([A-Za-z0-9_-]+)(/.*)$ language/index.php?language=$1&page=$2 [L]

    is a guess. What was

    RewriteRule ^(.*)\.asp$ /language/$1.asp? [R=301]

    for. it looks like its changing the URL to the same thing, or you have language/language/ in the URL.
     
    Nintendo, Mar 15, 2007 IP
  6. danzor

    danzor Peon

    Messages:
    208
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #526
    Google is currently indexing the majority of my static HTML content pages as opposed to the include on the main site. For example, Google indexes www.site.com/pages/info/site/site.html but not www.domaincom/site/ or whatever.

    I have tried:
    RewriteRule ^(.*)\.htm$ /site/$1/ [R=301,L]

    But if I request the page www.site.com/pages/links.htm, it will rewrite to http://www.site.com/site/pages/links/

    Is there any way I can get it to rewrite only the filename of the HTML file instead of the full directory?
     
    danzor, Mar 18, 2007 IP
  7. pgu1020

    pgu1020 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #527
    Hi. I have played around and trying to do this.

    * redirect all URL's of http://domain.com/whatever.php to http://www.domain.com
    * Redirect all urls of http://www.domain.com/whatever.php
    to http://www.domain.com

    I.E = Rewrite everything to the indexpage with a 301-redirect.

    This is the code i am using, but i cant get it to work


    <IfModule mod_rewrite.c>

    RewriteEngine on
    Options +FollowSymLinks
    RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
    RewriteRule ^(.*) http://www.domain.com/ [L,R=301]
    </IfModule>


    I get the non-www version to redirect good, but unfortunalty i dont get the www.domain.com/whatever.php to redirect to www.domain.com

    Anyone who can be a friendly soul ?
     
    pgu1020, Mar 19, 2007 IP
  8. Tehy

    Tehy Peon

    Messages:
    254
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #528
    Tehy, Mar 19, 2007 IP
  9. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #529
    The 7 has to be in the URL, and do you know php enough to add it to the URL and replace spaces with _?
     
    Nintendo, Mar 19, 2007 IP
  10. fadetoblack

    fadetoblack Peon

    Messages:
    416
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #530
    Hey Nintendo, first of all a great thread

    I have this new shopping site which is filled with aspx pages , and i am completely new to mod rewrite, need to ask a few questions

    1) Is it possible for mod_rewrite of aspx pages using .htaccess or should something be done with IIS side
    2) If yes for the above query,there are around 100's of pages whose url's need to rewritten ( for different keywords) should it all be put in a single .htaccess file


    if these get cleared ..there will be more queries.Thanx
     
    fadetoblack, Mar 20, 2007 IP
  11. Tehy

    Tehy Peon

    Messages:
    254
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #531
    Sorry, I don't know how to do that :/
     
    Tehy, Mar 20, 2007 IP
  12. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #532
    Do a little test....

    domain.com/.htaccess

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

    If domain.com/wacko.html shows the index, then it works. index.aspx is what ever the index file name is. Something tells me it won't work. I once tried mod_rewrite on a Windows server, and nothing happened.

    Spend some time hanging around the php board for search and replace stuff, until you know how to for example have

    sefanatics.com/reviews/7/sony_ericsson_w600i_review/

    where it spits out the links in the script.
     
    Nintendo, Mar 20, 2007 IP
  13. itrana123

    itrana123 Peon

    Messages:
    177
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #533
    Very nice nintendo, Thanks for sharing such a great tutorial i m just looking for .htaccess tutorial. thanx again..
     
    itrana123, Mar 22, 2007 IP
  14. azdude

    azdude Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #534
    My questions involves the seo implications of using the code below to direct all requests for non existent files and directories to the front page of a site using the code below.

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d


    RewriteRule ^(.*) index.php


    This prevents any 404's from being returned from the server and shows a 200. When i was setting up my Google webmaster account I noticed this.


    What it be a better solution to provide a friendly 404 error message instead. I have already 301 all my old pages to there new homes so the only time a 404 should be returned is when a user types in a link that never existed.
     
    azdude, Mar 22, 2007 IP
  15. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #535
    ErrorDocument 404 /page.html

    domain.com/page.html having the error message.
     
    Nintendo, Mar 22, 2007 IP
  16. azdude

    azdude Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #536
    What is the SEO advantage of doing this?
     
    azdude, Mar 23, 2007 IP
  17. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #537
    ..................
     
    Nintendo, Mar 23, 2007 IP
  18. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #538
    When googlebot and other search engines request a page that doesn't exist, they realize the page doesn't exist when a 404 Status Code is returned. If a 200 Status Code is returned such as in your setup, then the search engines don't realize your page is missing, and this means your ranking will be hurt big time.

    More info: Apache ErrorDocuments and HTTP Status Codes
     
    apachehtaccess, Mar 23, 2007 IP
  19. hawkal

    hawkal Peon

    Messages:
    97
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #539
    Hey thanks a lot Nintendo this has been very helpful!

    I cant decide if I want to use hypens (-) or forward slashes (/) to separate my url are there any useful advantages of one over another?
     
    hawkal, Mar 27, 2007 IP
  20. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #540
    Some people suggest using -. That the less 'directories' the better.
     
    Nintendo, Mar 28, 2007 IP