FAQ: mod_rewrite, 301 redirects, and optimizing Apache.

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

  1. bLaZ3n

    bLaZ3n Banned

    Messages:
    259
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #541
    bLaZ3n, Apr 3, 2007 IP
  2. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #542
    This makes it so when users request /contact/ or /contact from your site, the url stays as /contact/ but the page they are really seeing is contact.php
    
    RewriteEngine On
    RewriteBase /
    RewriteRule ^contact/?$ /contact.php [L,NC]
    
    Code (markup):
     
    apachehtaccess, Apr 3, 2007 IP
  3. bLaZ3n

    bLaZ3n Banned

    Messages:
    259
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #543
    that didnt work, but thanks anyway
     
    bLaZ3n, Apr 3, 2007 IP
  4. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #544
    Please explain more of what you want.. this is a good thread and we shouldn't waste pages on fluff.

    Why didn't it work? What happened? What are you going for?
     
    apachehtaccess, Apr 3, 2007 IP
  5. bLaZ3n

    bLaZ3n Banned

    Messages:
    259
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #545
    Basically..

    I want ever .php page extension to be displayed as a directory..

    Ex.

    index.php ---- /index/
    contact.php ----- /contact/
    myaccount.php ---- /myaccount/
    about.php ----- /about/

    etc..
     
    bLaZ3n, Apr 3, 2007 IP
  6. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #546
    Ok so when a user types inthe browser site.com/contact/ the url stays site.com/contact/ but its not site.com/contact/ that is served, instead its internally redirected so that site.com/contact/ is served from site.com/contact.php

    This code rewrites internally any request for site.com/var/ to site.com/var.php but the url in the browser stays site.com/var/
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(index|contact|myaccount|about)/?$ /$1.php [NC,L]
    Code (markup):

    If a user types site.com/contact.php then hey, thats what they typed. Your only option here is to redirect requests for site.com/var.php to site.com/var/ you can do that with this code.

    RequestCond %{REQUEST_URI} ^/(.+)\.php$ [NC]
    RewriteRule ^([^\.]+)\.php$ http://www.site.com/$1/ [R,L]
    Code (markup):
    To use them both:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^(index|contact|myaccount|about)/?$ /$1.php [NC,L]
    
    RequestCond %{REQUEST_URI} ^/(.+)\.php$ [NC]
    RewriteRule ^([^\.]+)\.php$ http://www.site.com/$1/ [R,L]
    Code (markup):
    If this isn't working for you either than please explain what you mean when you say it doesn't work, and explain what is seen in the url by the browser for the different stages.
     
    apachehtaccess, Apr 3, 2007 IP
  7. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #547
    If you don't need redirects...

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^([^.]+)/$ $1.php [L]
     
    Nintendo, Apr 3, 2007 IP
  8. Johnburk

    Johnburk Peon

    Messages:
    777
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #548
    Johnburk, Apr 5, 2007 IP
  9. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #549
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^site/blog/([^.]+)$ http://www.domain.com/$1 [R=301,L]

    If you're trying to get rid of the two folders. Take out $1 if you actually want all URLs to redirect to domain.com.
     
    Nintendo, Apr 5, 2007 IP
  10. Johnburk

    Johnburk Peon

    Messages:
    777
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #550
    Johnburk, Apr 5, 2007 IP
  11. pgu1020

    pgu1020 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #551
    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
     
    pgu1020, Apr 5, 2007 IP
  12. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #552
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^(.*)$ http://www.domain.com/ [R=301,L]
     
    Nintendo, Apr 5, 2007 IP
  13. pgu1020

    pgu1020 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #553
    Thanks alot. Nintendo.

    I have one more question.. i try to get a string like this to 301 to the index-page

    domain.com/dir/C5B37243-DEAA-45D7-A131-4F5D6F3CD992.wlt?pageNo=75
    domain.com/dir/E0912940-92A6-4C35-AA9A-D8CD7B38EE22.wlt?eid=176

    I am using ^dir/(.*)$ http://domain.com/

    The problem is that everything from the ? and after doesnt get 301:ed..

    so the result becomes
    domain.com/?pageNo=75
    domain.com/?eid=176

    How do i get a redirect to cover the chars after the ?

    I want everything that is after /dir/ to redirect to the root. http://domain.com
     
    pgu1020, Apr 6, 2007 IP
  14. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #554
    ? isn't very mod_rewrite friendly. I've redirected URLs with ? in them by doing....

    RewriteCond %{QUERY_STRING} ^t=10285(&page=[^&]+)?$
    RewriteRule ^showthread\.php$ http://www.vgchat.com/showthread.php?t=14913 [R=301,L]

    I'm not sure how to translate that in to your URLs.
     
    Nintendo, Apr 6, 2007 IP
  15. bLaZ3n

    bLaZ3n Banned

    Messages:
    259
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #555
    @ apachehtaccess

    It did not work, I got a Internal Server Error..

    @ Nintendo

    Yours work, but when I access my site by www.scrimsnow.com/index/ the images dont show.
     
    bLaZ3n, Apr 6, 2007 IP
  16. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #556
    Image HTML codes have to be changed as if it's the actual URL. The browsers have no ides the URLs are fake!
     
    Nintendo, Apr 6, 2007 IP
  17. bLaZ3n

    bLaZ3n Banned

    Messages:
    259
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #557
    bLaZ3n, Apr 8, 2007 IP
  18. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #558
    Nintendo, Apr 9, 2007 IP
  19. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #559
    Hey Nintendo, thanks for the answers!
    You could better write the above as
    Options +FollowSymLinks +Indexes
    Code (markup):
    But personally I only use +Indexes in my images folder. Normally I do
    Options +FollowSymLinks -Indexes
    DirectoryIndex index.php index.html
    Code (markup):
     
    apachehtaccess, Apr 9, 2007 IP
  20. bLaZ3n

    bLaZ3n Banned

    Messages:
    259
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #560
    weird... www.nixism.com is still redirecting to the splash page..

    even though I changed the htaccess file..

    can someone set me up for my site to link with..

    www.nixism.com and nixism.com
     
    bLaZ3n, Apr 11, 2007 IP