mod_rewrite only working for /

Discussion in 'Apache' started by jlocke, Nov 7, 2006.

  1. #1
    Hi, anyone have any idea why this only works in the / of the virtual server. index.php is in the root and every subdirectory and accesses fine when called directly. I can also access through the rewrite below but only in the root directory.

    If I put the rule in .htaccess in the subdirectories the rule works fine. But it was my hope to put this one rule here in the root of the virtual that would take care of all directories.

    httpd.conf

    <VirtualHost xx.xx.xx.xx>
    DocumentRoot /var/www/test
    ServerName xxx.xxxxxx.xxx
    <Directory /var/www/test>
    Options FollowSymLinks Includes
    RewriteEngine On
    RewriteBase /
    RewriteRule ^page([^/\.]+)/?$ index.php?page=$1
    </Directory>

    Essentially I end up with a SEO friendly URL like localhost/page1 instead of localhost/index.php?page=1
    But currently is only working in /
     
    jlocke, Nov 7, 2006 IP
  2. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #2
    domain.com/.htaccess

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^page([^.]+)$ index.php?page=$1

    What's the script URL and directory info for other scripts and locations. If you have different scripts to 'take care of all directories' you have to have other lines of code for the other stuff.
     
    Nintendo, Nov 7, 2006 IP
  3. jlocke

    jlocke Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi, the script code is fine since if I put those rules in .htaccess in each directory it works fine. It also works if I put it in httpd.conf for each directory. localhost/index.php locahost/dir1/index.php

    What I am trying to do is just have one rule like above that takes care of the entire virtual server. So whenever index.php is accessed it rewrites to that rule.

    I am assuming that the rule I made above would propagate down from the parent directory. But it only works for /

    localhost/index.php?page=1 --> localhost/page1
    locahost/dir1/index.php?page=1 --> localhost/page1
    locahost/dir2/index.php?page=1 --> localhost/page1
     
    jlocke, Nov 7, 2006 IP
  4. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #4
    If it's the same thing on each domain, I'm not sure how to do that!
     
    Nintendo, Nov 7, 2006 IP
  5. jlocke

    jlocke Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    No, all the same domain.

    btw - I fixed typos in urls before your response.
     
    jlocke, Nov 7, 2006 IP
  6. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #6
    Each script has to have something unique in the URL. Something like

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^page([^.]+)$ index.php?page=$1
    RewriteRule ^whatever1/page([^.]+)$ dir1/index.php?page=$1
    RewriteRule ^whatever2/page([^.]+)$ dir2/index.php?page=$1
    RewriteRule ^whatever3/page([^.]+)$ dir3/index.php?page=$1
     
    Nintendo, Nov 7, 2006 IP
  7. jlocke

    jlocke Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    It does and like I said if I put the rule from my first post in htaccess in each subdirectory it works. But I thought it was not necessary to put an htaccess file in each subdirectory as long as you have it in the root of the virtual server. Therefore the rule would apply to all subdirectories.
     
    jlocke, Nov 7, 2006 IP
  8. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #8
    You can have one .htaccess file at domain.com/.htaccess to cover all directories.
     
    Nintendo, Nov 7, 2006 IP
  9. jlocke

    jlocke Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    That is exactly what I am trying to do but it only works in the / directory. That is unless I copy the htaccess to each subdirectory.

    RewriteRule ^page([^/\.]+)/?$ index.php?page=$1

    I even tried

    RewriteRule ^page([^.]+)$ index.php?page=$1
    RewriteRule ^dir2/page([^.]+)$ dir1/index.php?page=$1

    But it still only works in the root directory. That is unless I copy the htaccess into the subdirectory.
     
    jlocke, Nov 7, 2006 IP
  10. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #10
    Are you saying it works at domain.com/.htaccess until you have a .htaccess file at domain.com/directory/.htaccess ??
     
    Nintendo, Nov 7, 2006 IP
  11. jlocke

    jlocke Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    No, I am saying it only works in each directory only if I use htaccess in each directory.

    However

    1. If I only use one htaccess file in / directory the rule will not work in subdirectories
    2. If I only use the rule in httpd.conf in the / directory the rule will not work in subdirectories

    3. It even does not work if I add another rule in either htaccess or httpd conf like RewriteRule ^dir2/page([^.]+)$ dir1/index.php?page=$1

    It will only work if the htaccess file is in each directory or only in the / directory if I try it in httpd.conf
     
    jlocke, Nov 7, 2006 IP
  12. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #12
    So domain.com/.htaccess messes up the directory .htaccess files?

    Do you have

    RewriteEngine on
    RewriteBase /

    when you try out .domain.com/.htaccess

    I'm not sure if it would fix it, but try it with

    $ /dir1/

    instead of

    $ dir1/
     
    Nintendo, Nov 7, 2006 IP
  13. seo_kate

    seo_kate Peon

    Messages:
    362
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #13
    htaccess on the root must work for u unless u r having some syntax missing or as nitendo said so check ur code first and try to run it
     
    seo_kate, Nov 7, 2006 IP
  14. jlocke

    jlocke Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    No it does not. htaccess with that rule works in every directory I put the htaccess file in, including root.

    But like I keep saying I thought you could have just one .htaccess file in / which would take care of all subdirectories with the rule I posted above without having to place a separate htaccess file in each subdirectory of the virtual domain. I also tried doing this in httpd.conf but no luck there either.

    The goal is to achieve putting only 1 htaccess file with one rule in the root directory that would take care of requests in subdirectories. (or 1 rule in httpd.conf for that virtual domain that takes care of all subdirectories under that domain).


    Tried all that. It will only work in subdirectories if I put an htaccess file there. It always works in / of the virtual domain.
     
    jlocke, Nov 7, 2006 IP
  15. jlocke

    jlocke Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Just to add that I did find I can get one htaccess file in the / directory to work for subdirectories but only by addingmore rules, like this. Which is what I did not want to do.

    RewriteEngine On
    RewriteBase /
    RewriteRule ^page([^/\.]+)/?$ index.php?page=$1
    RewriteRule ^Dir1/page([^/\.]+)/?$ /Dir1/index.php?page=$1

    So how can I change this into one rule so that I do not have to add a 100 rewrite rules with Dir1 Dir2 Dir3, etc?

    Perhaps this makes it more clear now to what I am trying to do.
     
    jlocke, Nov 8, 2006 IP
  16. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #16
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^([^.]+)/page([^.]+)$ $1/index.php?page=$1 [L]

    might do it. With the url being domain.com/original-directory/whatever
     
    Nintendo, Nov 8, 2006 IP
  17. jlocke

    jlocke Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Thx, but I tried all different variations........ Wait, I think I got it just as I was typing this as I was reading the rules in the FAQ I had an idea. Still two rules but the second one takes care of 100 directories, good enough.

    RewriteRule ^(.*)page([^/\.]+)/?$ $1/index.php?page=$2 [L]
    RewriteRule ^(.*)/page([^/\.]+)/?$ $1/index.php?page=$2 [L]

    The first rule takes care of the root directory while the second takes care of the top subdirectories off of the root.

    It seems to work.
     
    jlocke, Nov 8, 2006 IP
  18. jlocke

    jlocke Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Just realized I cut-past the wrong code. I meant to post this,

    RewriteRule ^page([^.]+)$ index.php?page=$1
    RewriteRule ^(.*)page([^/\.]+)/?$ $1/index.php?page=$2 [L]

    But on further investigation that above added an extra / so I ended up with two //

    So I tried only have this rule below. It takes care of everything and without the extra //

    RewriteRule ^(.*)page([^/\.]+)/?$ $1index.php?page=$2 [L]
     
    jlocke, Nov 8, 2006 IP