SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Apache mod_rewrite module

    Hi, anyone here that knows apache mod_rewrite module?

    Okay this is what I'd like to do:

    People come to this address/page (which doesn't exist on the server): mydomain.com/sponsor/index.php

    now I mod_rewrite the URL so that what apache really opens is: mydomain.com/index.php?where=sponsor

    I need this to get listed in SEs (because SEs don't list URLs with ? in them)...

    okay on with the problem...

    for this I use this code in .htaccess:

    RewriteEngine On
    RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+).php $2.php?where=$1

    okay so all works to this point. now the problem:

    when I open mydomain.com/sponsor/ apache won't rewrite to mydomain.com/index.php?where=sponsor but will open 404.

    For this to work I would have to write this in .htaccess:

    RewriteEngine On
    RewriteRule ^([a-zA-Z]+)/ index.php?where=$1

    Now I have one solution and the other working separately just fine, but how do I make them work together in one single .htaccess so that mydomain.com/sponsor/ and mydomain.com/sponsor/index.php will both open mydomain.com/index.php?where=sponsor ???

  2. #2
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you put this line of PHP code to index.php?

    PHP Code:
    <?php
    header 
    ("Location: index.php?where=sponsor"); 
    ?>

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ??

    Robo,

    If I put that line in my index.php file it will continue to load itself! What would be the purpose of that?

    This must be done with apache. I don't see how it could be done the other way within the script. One thing you must be aware of:

    mydomain.com/sponsor/ and mydomain.com/sponsor/index.php URLs DON'T exist on the server! Only mydomain.com/index.php exists!

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this should do the trick.

    Code:
    RewriteEngine On
    #RewriteRule ^([^/]+)/(.*)$ index.php?where=$1
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    signs

    freddydoesphp,

    Thanks! What does ([^/]+) and (.*) are there for? What do they mean? Also why did you put # in front of the command?

    BTW, can I put two rewrite rules in the same row like:
    RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+).php $2.php?where=$1
    RewriteRule ^([a-zA-Z]+)/ index.php?where=$1
    because I found out that this works too. Or is this somehow too risky?

    Does anyone know of any page/URL where this [letters, marks, etc...] for mod_rewrite are explained so I will be able to insert correct ones myself next time?

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry it should actually be
    RewriteRule ^([^/]+)/(.*?)$ index.php?where=$1


    The docs on mod_rewrite are weak at best. Sorry the pund sign should come off. I do believe that the code I gave you will catch both

    And point them both to index.php?where=sponsor

    The ([^/]+) mean everything up to the slash or everything except slash.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    two rewrite rules

    freddydoesphp,

    thanks! do you know is it wrong to put two rewrite rules in .htaccess??

    like:
    RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+).php $2.php?where=$1
    RewriteRule ^([a-zA-Z]+)/ index.php?where=$1

    Will that slow down the server or could it cause any serious problems??

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Again I am totally confused the method I gave you covers both of your needs? Why do you insist on using two rewrite rules, the one rule will take care of both

    www.domain.com/sponsor/

    and

    www.domain.com/sponsor/index.php


    pointing them both to index.php?where=sponsor
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    The update

    freedydoesphp,

    Because I just remembered your code covers almost all my needs. I forgot to tell you the whole story.

    Well this is how it must work:

    www.domain.com/sponsor/index.php
    www.domain.com/sponsor/page1.php
    www.domain.com/sponsor/page2.php
    http://www.domain.com/sponsor/whatev...me_up_with.php

    must be rewrited to:

    www.domain.com/index.php?where=sponsor
    www.domain.com/page1.php?where=sponsor
    www.domain.com/page2.php?where=sponsor
    http://www.domain.com/whatever_I_com...?where=sponsor

    for this to work I used this rule:
    RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+).php $2.php?where=$1

    now I need also www.domain.com/sponsor/ to be rewrited to www.domain.com/index.php?where=sponsor and for this to work I used RewriteRule ^([a-zA-Z]+)/ index.php?where=$1

    Now if I want make this work for all instances I need to use both rewrite rules. Or can there be a magic solution with only one rewrite rule again???

    I just need to learn how to insert this ^#""%%&#$ signs in the rules so I will be able to do this myself. Is there any good plain explanation of this signs? URL?

  10. #10
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its pretty difficult to grasp and I have yet to actually figure it out all the way but I am sure we can make it work with one rule, let me work on it for a few minutes. There are not a lot of docs about how to use it, but you can check http://www.linuxguruz.org there are a ferw links from there. BRB
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this will do what you want. It will redirect to the page but if the page does not exist it will default to the DirectoryIndex setting usually index.php or index.html But you can add

    DirectoryIndex index.php to the .htaccess file just above the rewrite rules.


    Code:
    RewriteEngine On
    RewriteRule ^([^/]+)/(.*)$ $2?where=$1
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  12. #12
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    WOW!

    freddydoesphp,

    Man you really know how to write those rewrites! The rule works like a charm! Unbeliveable!

    Now I know I will always try to find the one line solution

    I just thought it wasn't possible.

    About more rewrite rules in one .htaccess file:
    I am using hotlinking protection to protect my gif and jpg from images being hotlinked from another server or put on some BBS (where I would have to pay for the bandwidth and getting nothing in return) so I have this in my htaccess too:

    AuthUserFile /dev/null
    AuthGroupFile /dev/null
    RewriteEngine On
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://www.domain.com/ [NC]
    RewriteCond %{HTTP_REFERER} !^http://domain.com/ [NC]
    Rewriterule [^/]+\.(gif|jpg)$ http://www.domain.com/404.html [R]


    Now where should I put the rewrite rule (RewriteRule ^([^/]+)/(.*)$ $2?where=$1)?? Above or below this hotlinking protection lines??

    BTW thanks fro that http://www.linuxguruz.org link too! There's a lot of interesting stuff!

  13. #13
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you will want to put it above the first RewriteCond. I am no expert in this, there is some crazy stuff you can do with mod_rewrite. Most of the time I learn because some asked here and I wanted to know how to do it, so I just wrote it and tweaked it till it worked then I gave it to them.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  14. #14
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    freddydoesphp,

    Yes I saw there is more about this rewrite module. I need to read all that apache docs. Anyway thanks, you helped me a lot!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •