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. vishwaa

    vishwaa Well-Known Member

    Messages:
    271
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #501
    Add the following lines to the end of "httpd.conf" file (Virtual host section).

    <VirtualHost *:80>
        ServerAdmin you@example.com
        DocumentRoot C:/Docs/MySite
        ServerName www.example.com
        ServerAlias example.com *.example.com
    </VirtualHost>
    Code (markup):
    Also make sure that the default directory configuration defined to be as follow in the main server configuration.

    <Directory />
        Options FollowSymLinks
        AllowOverride [B]All[/B]
    </Directory>
    Code (markup):
    If AllowOverride is set to None, probably mod rewrite may not work well.

    Hope it helps you.
     
    vishwaa, Feb 3, 2007 IP
  2. SpeedyDomainRegistration

    SpeedyDomainRegistration Peon

    Messages:
    170
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
  3. roark

    roark Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #503

    Definately, thanks alot for your help!!
    I'll give it a bash and let y'all know how it goes!!
     
    roark, Feb 3, 2007 IP
  4. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #504
    Nintendo, Feb 3, 2007 IP
  5. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #505
    or

    
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index.php?topic=(.*)\..*$ http://www\.domain\.com/index\.php?topic=$1 [R=301,L]
    
    Code (markup):
     
    apachehtaccess, Feb 6, 2007 IP
  6. mameha1977

    mameha1977 Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #506
    I am currently implementing this tutorial.

    I have got my htaccess working to load the static html with php version. I am now rewriting all my links so they point to .html instead of .php.

    However, search engines and people using bookmarks will continue to land on the .php URIs. I want to redirect them automatically to the correct html URIs. Is there really no way to do that in htaccess?

    I am finding that if I change my links to point to .html, they work OK when .html is already being used in the current URI, but if its the .php URI version the links dont work. Thus when a search engine or user from bookmark comes the links wont work.
     
    mameha1977, Feb 7, 2007 IP
  7. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #507
    apachehtaccess, Feb 7, 2007 IP
  8. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #508
    Heres the easiest way.. This code issues an SEO-friendly 301 redirect every time a file ending in .php is requested. The 301 redirect tells the requesting client that the new Location of the file is at permanent location site.com/file.html instead of site.com/file.php


    RedirectMatch 301 (.*)\.php http://www.site.com/$1.html
    Code (markup):


    Then you need to tell google that files ending in .php should be removed from the index. Follow googles advice and create a robots.txt file with the following content.


    This robots.txt code removes all files that end in .php from google and also removes and files with a ? QUERY_STRING in the url.
    
    User-agent: Googlebot
    Disallow: /*.php$
    Disallow: /*?*
    
    Code (markup):
     
    apachehtaccess, Feb 7, 2007 IP
  9. mameha1977

    mameha1977 Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #509
    OK I added the robots file.

    I have trouble with the 301 redirect from php to html, ive tried for ages but cant get anything like that to work. I think when it works it makes an infinite loop as it passes pph -> html -> php -> html etc..

    Anyway, the old php pages were not indexed well. only 7 pages were indexed and they had little to no pagerank. so i dont think SEO wise I get anything from passing them on to the new pages...? the main benefit would be to pass user with bookmarks over the correct page instead of a main index page, but like i say i cant get any redirect from php to html to work.
     
    mameha1977, Feb 8, 2007 IP
  10. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #510
    Could you give an example of a couple of your old urls with .php and also what you want them to be changed to? (you can leave your domain out)
     
    apachehtaccess, Feb 8, 2007 IP
  11. vishwaa

    vishwaa Well-Known Member

    Messages:
    271
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #511
    You can avoid infinite loops by adding this code before the actual mod rewrite code.

    RewriteCond %{HTTP_HOST} ^www.example\.com [NC]
    RewriteCond %{THE_REQUEST} ^(.*)\.php\ [B]HTTP/[/B]
    RewriteRule ^(.*)\.php$ http://www.example.com/$1.html [R=301,L]
    Code (markup):
    Apache will issue 301 redirect only when the request part of the header sent by the browser contains HTTP (HTTP/1.1 or HTTP/1.0). Hence the redirect stuff will not get stuck with our actual static url implementation.
     
    vishwaa, Feb 8, 2007 IP
  12. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #512
    Nice vishwaa, here is another really handy infinite loop stopper that I use all the time.

    RewriteCond %{ENV:REDIRECT_STATUS} 200
    RewriteRule .* - [L]
    Code (markup):
     
    apachehtaccess, Feb 9, 2007 IP
  13. mameha1977

    mameha1977 Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #513
    It took me about 5 hours of 500 and 404 errors but I finally managed to do it.

    I put all my html -> php redirects at the top, then the php -> html ones at the bottom with R=301 on them.

    I also used your:
    RewriteCond %{ENV:REDIRECT_STATUS} 200
    RewriteRule .* - [L]

    Though it may not be necessary for me, I didnt test it. Thanks for your help.
     
    mameha1977, Feb 12, 2007 IP
  14. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #514

    No problemo, maybe you could post your updated .htaccess file for us.
     
    apachehtaccess, Feb 17, 2007 IP
  15. ragamuffin

    ragamuffin Guest

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #515
    vishwaa,

    Thanks, :) been looking to see an example of that virtualhost.
     
    ragamuffin, Feb 21, 2007 IP
  16. WebGeek182

    WebGeek182 Active Member

    Messages:
    510
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    95
    #516
    Great tutorial. This is going to get a link.
     
    WebGeek182, Feb 22, 2007 IP
  17. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #517
    Rewrite your urls with _ underscores to - hyphens. Based on article Rewrite underscores to hyphens for SEO URL

    This code sends a 301 redirect when it receives a request for an underscored url to the hyphenated url.

    d.com/the_first_apache_article.html ==> d.com/the-first-apache-article.html


    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
     
    RewriteRule !\.(html|php)$ - [S=4]
    RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=uscor:Yes]
    RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=uscor:Yes]
    RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=uscor:Yes]
    RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=uscor:Yes]
     
    RewriteCond %{ENV:uscor} ^Yes$
    RewriteRule (.*) http://d.com/$1 [R=301,L]
    Code (markup):
     
    apachehtaccess, Feb 22, 2007 IP
  18. ragamuffin

    ragamuffin Guest

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #518
    Man, this is great stuff. Mind Boggling stuff.

    I will try it out. The potential time saved by this script is unbelievable. Many thanks.
     
    ragamuffin, Feb 23, 2007 IP
  19. pkr

    pkr Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #519
    i have the code


    But he is not functioning correctly, if to have access www.mysite.com.br/classificados/ does not work, if to have access www.mysite.com.br/classificados/classificados/ it works, but I want that he does not need to repeat /classificados somebody helps me?
     
    pkr, Mar 12, 2007 IP
  20. clover

    clover Peon

    Messages:
    472
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #520
    hi guys,

    i am using this on-the-fly translation script here
    http://forums.digitalpoint.com/showthread.php?t=42057

    i have a small problem with the my jump script being redirected to the translated pages of the destination domain.

    here is my problem here
    http://forums.digitalpoint.com/showpost.php?p=2523009&postcount=638

    basically i need a mod rewrite that will redirect all

    example.com/language/de/jump-xxx.asp
    example.com/language/fr/jump-xxx.asp
    example.com/language/jp/jump-xxx.asp
    example.com/language/es/jump-xxx.asp
    example.com/language/ru/jump-xxx.asp
    etc...

    to
    example.com/jump-xxx.asp

    so that it would be redirected to the ORIGINAL language of the destination page.

    this is what i currently have

    RewriteRule ^jump-(.*).asp$ jump.php?id=$1 [L]
    this was done to avoid the problems with the "?id=" being converted to erroneous characters.

    Any help is very much appreciated.
     
    clover, Mar 12, 2007 IP