FAQ: mod_rewrite, 301 redirects, and optimizing Apache.

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

  1. axemedia

    axemedia Guest

    Messages:
    1,070
    Likes Received:
    79
    Best Answers:
    0
    Trophy Points:
    0
    #421
    I have these pages

    www. domain .com/index.php?page=Folder-Number-One
    www. domain .com/index.php?page=Key-Phrase-One

    I want to create a sub-folder for other files to be under with Folder-Number-One.php file becoming the index file

    I want to create
    www. domain .com/Folder-Number-One
    www. domain .com/Folder-Number-One/Key-Phrase-One.html

    I have this:

    RewriteRule ^Folder-Number-One\/(.*)$ index.php?page=Folder-Number-One
    RewriteRule ^Folder-Number-One/Key-Phrase-One/(.*)$ index.php?page=Key-Phrase-One

    first rewrite ends up creating domain .com/Folder-Number-One.html but i would rather it be a folder not a file.
    2nd rewrite is not working at all. What am i missing? Are my dashed file names causing troublems, or is my rewrite simply wrong?

    I already have a rewrite rule in there that converts files to .html extension.


    EDIT I figured out the folder part by removing the (.*)$ (also works with the $ still in it)
    this now works www. domain .com/Folder-Number-One/

    But i still cant get www. domain .com/Folder-Number-One/Key-Phrase-One.html
     
    axemedia, Sep 27, 2006 IP
  2. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #422
    RewriteRule ^Folder-Number-One/(.*)/$ index.php?page=$1
    RewriteRule ^(.*)/$ index.php?page=$1

    If Folder-Number-One changes....

    RewriteRule ^(.*)/(.*)/$ index.php?page=$2
    RewriteRule ^(.*)/$ index.php?page=$1

    with $2 being Key-Phrase-One.

    With

    index.php?page=Folder-Number-One
    and
    index.php?page=Key-Phrase-One

    both are exactly the same thing so why not just have

    RewriteRule ^(.*)/$ index.php?page=$1
     
    Nintendo, Sep 28, 2006 IP
  3. axemedia

    axemedia Guest

    Messages:
    1,070
    Likes Received:
    79
    Best Answers:
    0
    Trophy Points:
    0
    #423
    I think I have to abandon my attempt at making the other files go into the folder. I just cant get it to work.

    I'll just have to use this RewriteRule ^(.*)/$ index.php?page=$1

    and have them all as files off the root, i guess.
     
    axemedia, Sep 28, 2006 IP
  4. BlueDevilMedia

    BlueDevilMedia Well-Known Member

    Messages:
    1,917
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    190
    #424
    I can't get mod_rewrite to work on this site and it's frustrating the @#$3 out of me :p ... What am I doing wrong???

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^(.*)\.html$ /pages.php?idpages=$1 [L]
    RewriteRule ^(.*)\.html$ /description.php?iddesc=$1 [L]
    RewriteRule ^(.*)/(.*)/(.*)\.html$ /products.php?id=$1&lev1=$2&lev2=$3 [L]
    RewriteRule ^(.*)/(.*)\.html$ /products.php?id=$1&lev1=$2 [L]
    RewriteRule ^(.*)\.html$ /products.php?id=$1 [L]

    Site is cellmansion dot com
     
    BlueDevilMedia, Sep 29, 2006 IP
  5. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #425
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^([^.]+)/([^.]+)/([^.]+)\.html$ products.php?id=$1&lev1=$2&lev2=$3 [L]
    RewriteRule ^([^.]+)/([^.]+)\.html$ products.php?id=$1&lev1=$2 [L]
    RewriteRule ^products/([^.]+)\.html$ products.php?id=$1 [L]
    RewriteRule ^pages/([^.]+)\.html$ pages.php?idpages=$1 [L]
    RewriteRule ^description/([^.]+)\.html$ description.php?iddesc=$1 [L]
     
    Nintendo, Sep 30, 2006 IP
  6. Helping

    Helping Peon

    Messages:
    102
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #426
    Hello,

    I have a very simple question:

    I have to redirect www.domain.com to www.domain.com/mycmsdirectory
    because I installed my CMS inside the /mycmsdirectory to keep clean the root

    I'm using this now
    Redirect 301 /index.html http://www.site.com/directory/index.php

    It's works but I think it's not correct (SEO Point of view) and I can do better. Nintendo can you please help me? :)
     
    Helping, Oct 3, 2006 IP
  7. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #427
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^index\.html$ directory/index.php [L]

    or

    RewriteRule ^index\.html$ http://www.domain.com/directory/index.php [R=301,L]

    for mod_rewrite. Get rid of index\.html for domain.com
     
    Nintendo, Oct 3, 2006 IP
  8. kmzeron

    kmzeron Well-Known Member

    Messages:
    734
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    128
    #428
    Special thanks to Nintendo, I think he is the master of mod_rewrite :D
    Best Regards !
     
    kmzeron, Oct 6, 2006 IP
  9. Helping

    Helping Peon

    Messages:
    102
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #429
    hello again :)

    I set up a test website based on Wordpress at www.domain.tld/dir

    I used this code on /.htaccess to redirect all domain.tld requests to www.domain.tld:

    Options +FollowSymlinks
    RewriteEngine on
    RewriteCond %{http_host} ^domain.tld [nc]
    RewriteRule (.*)$ http://www.domain.tld/$1 [r=301,nc]

    and all went ok.
    Problems started when I started to use permalinks under wordpress which create another .htaccess under /dir directory with this options:

    # BEGIN WordPress

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /dir/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /dir/index.php [L]
    </IfModule>

    # END WordPress

    This override my general htaccess so I tried to insert inside the previous code and i get:

    # BEGIN WordPress

    Options +FollowSymlinks
    RewriteEngine on
    RewriteCond %{http_host} ^domain.tld [nc]
    RewriteRule (.*)$ http://www.domain.tld/$1 [r=301,nc]


    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /dir/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /dir/index.php [L]
    </IfModule>

    # END WordPress

    But the fix don't work
    In particular when I try to access this url:

    http://domain.tld/dir

    I get:

    Moved Permanently
    The document has moved here (http://domain.tld/dir).

    Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
    Servage.net Cluster (Enhanced Apache) Server at domain.tld Port 80

    This is a loop

    I have a share account and the general config of the webserver redirect requests to domain.tld if you not use the trailing slash.

    For example: http:www.domain.tld/dir point to domain.tld/dir/

    I don't know how to resolve the problem so I'm asking again for help Super Nintendo :)

    P.S.

    I used also other htaccess code like:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www.domain.com
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]


    RewriteEngine on
    Options +FollowSymLinks
    RewriteCond %{HTTP_HOST} ^mydomain\.com
    RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=permanent,L]


    but the problems remains
     
    Helping, Oct 7, 2006 IP
  10. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #430
    RewriteRule . /dir/index.php [L]

    doesn't look correct at all. Not even sure if...

    RewriteRule ^.$ dir/index.php [L]

    would fix it. For adding / to the URLs, right before the first $ on the

    RewriteRule

    lines, add a /

    I got wordpress and the .htaccess file code it gave out is 49 lines.

    You might want to make a post over on their message board.
     
    Nintendo, Oct 7, 2006 IP
  11. Helping

    Helping Peon

    Messages:
    102
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #431
    thank you for your fast reply!

    I'm using Wordpress 2.0.4 I tried to reinstall it and change themes but the code it give me is always that :(
    My permalinks structure is: /%year%/%monthnum%/%day%/%category%/%postname%/
     
    Helping, Oct 7, 2006 IP
  12. Helping

    Helping Peon

    Messages:
    102
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #432
    I found something interesting here:

    http://wordpress.org/support/topic/71959?replies=3

    I will do some tests and I will report my finding :)
     
    Helping, Oct 7, 2006 IP
  13. Helping

    Helping Peon

    Messages:
    102
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #433
    The code works:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.domain\.tld$
    RewriteRule ^(.*) http://www.domain.tld/$1 [R=301,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^dir/ /dir/index.php [L]
    </IfModule>

    # END WordPress

    The only problem is this:

    If you call domain.tld/dir

    it redirects to:

    www.domain.tld/dir

    and it redirects to:

    http://domain.tld/dir/

    then to:

    http://www.domain.tld/dir/

    and it finally reach the website

    So it works but is there a cleaner solution?
     
    Helping, Oct 8, 2006 IP
  14. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #434
    Post your complete .htaccess code. I'm guessing you have more than one [R=301,L] code.
     
    Nintendo, Oct 9, 2006 IP
  15. Helping

    Helping Peon

    Messages:
    102
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #435
    Redirect 301 /index.html http://www.domain.tld/dir/

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.domain\.tld$
    RewriteRule ^(.*) http://www.domain.tld/$1 [R=301,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^dir/ /dir/index.php [L]
    </IfModule>

    # END WordPress
     
    Helping, Oct 10, 2006 IP
  16. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #436
    Changing the order is all I can think of.

    Redirect 301 /index.html http://www.domain.tld/dir/

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^dir/ /dir/index.php [L]
    RewriteCond %{HTTP_HOST} !^www\.domain\.tld$
    RewriteRule ^(.*) http://www.domain.tld/$1 [R=301,L]
    </IfModule>

    # END WordPress
     
    Nintendo, Oct 10, 2006 IP
    Richie_Ni likes this.
  17. Richie_Ni

    Richie_Ni Illustrious Member

    Messages:
    10,721
    Likes Received:
    1,175
    Best Answers:
    0
    Trophy Points:
    410
    #437
    Perfect FAQ!!
    It's seems that you are an expert on this,will be asking for help if needed :D
    Thanks,Nintendo!
     
    Richie_Ni, Nov 20, 2006 IP
  18. nowares

    nowares Active Member

    Messages:
    115
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #438
    this is a very complete tutorial, thanx Nintendo!

    ive learnt a few things :)
     
    nowares, Dec 6, 2006 IP
  19. danzor

    danzor Peon

    Messages:
    208
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #439
    I am currently rewriting urls from
    http://www.domain.com/index.php?page=page
    to
    http://www.domain.com/site/page/

    I would like to redirect users who access the page from index.php?page= and redirect them to /site/page. How would I go about doing this without getting in some horrible mod_rewrite infinite loop?

     
    danzor, Dec 11, 2006 IP
  20. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #440
    This is just a guess. If it doesn't do it, you're toast.

    RewriteCond %{QUERY_STRING} ^page=page(&page=[^&]+)?$
    RewriteRule ^index\.php?page=([^.]+)$ http://www.domain.com/site/$1 [R=301,L]
     
    Nintendo, Dec 11, 2006 IP