.htaccess

Discussion in 'Apache' started by mehbooba, Nov 8, 2006.

  1. #1
    would someone please help me out here.

    I wish to do a rewrite rule that says any url that has the text '-article.html' then the following file should be executed : /prog2/prog

    i came up with the following :

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^-article.html$ /prog2/prog [L,QSA]


    its not working... when a file say with name : www.mydomain/a-file-article.html

    is clicked... my browser comes HTTP 404 Not Found error.


    would appreciate some help.

    thanks
     
    mehbooba, Nov 8, 2006 IP
  2. WhiteHatHacker

    WhiteHatHacker Peon

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Right, don't worry, the solution is simple:

    RewriteRule ^-article.html$ /prog2/prog [L,QSA]
    Code (markup):
    is not matching anything except the file "http://www.domain.tld/-article.html", because the "^" symbol signifies the start of the locator. Therefore, if any characters come between the / and the -article bits, then it is not matched.

    Simply use
    RewriteRule -article.html$ /prog2/prog [L,QSA]
    Code (markup):
    instead.

    NB:

    If, by inserting the [QSA] option, you hope to send /prog2/prog the filename, you're barking up the wrong tree. (Sorry, I'm beginning to sound really patronising now)

    Use this:

    RewriteRule ^([.*])-article.html$ /prog2/prog?q=$1 [L]
    Code (markup):
    I think that's right, anyway. :S

    Good luck!
     
    WhiteHatHacker, Nov 8, 2006 IP
  3. mehbooba

    mehbooba Peon

    Messages:
    260
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi

    thanks for your help... i tried your suggestions...but neither of your solutions work.
     
    mehbooba, Nov 8, 2006 IP
  4. WhiteHatHacker

    WhiteHatHacker Peon

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Damn! Just my luck...

    Try this:

    RewriteRule (.*)-article.html$ /prog2/prog.php?q=$1
    Code (markup):
    That will - I just tested it on my machine - DEFINITELY change "www.domain.tld/something-article.html" to "www.domain.tld/prog2/prog.php?q=something". However, it will do this server-side, and quietly, so the text in the location bar in your browser doesn't change, but the server does look for /prog2/prog.php and tells it that $_GET["q"] is "something".

    If you want to visually check it works, use this line:

    RewriteRule (.*)-article.html$ /prog2/prog.php?q=$1 [R=301]
    Code (markup):
    which sends a redirect code to the browser, letting the user know of the redirect.
     
    WhiteHatHacker, Nov 8, 2006 IP
  5. mehbooba

    mehbooba Peon

    Messages:
    260
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    :-( no luck.... must be my server.

    i did a random rewrite... to make sure my rewrite function works :

    RewriteRule ^(.*)$ /prog.php [L,QSA]


    (which i assume means.... if any url doesnt exist... execute prog.php) and it does work
     
    mehbooba, Nov 8, 2006 IP
  6. WhiteHatHacker

    WhiteHatHacker Peon

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It doesn't actually check if the url exists first - it'll rewrite it regardless - unless the two specified RewriteCond lines are still there. But you've got me there. It really does work on my local server. If

    RewriteRule (.*)-article.html$ /prog2/prog.php?q=$1 [R=301,L]
    Code (markup):
    doesn't work - as the ONLY line in the .htaccess file apart from RewriteEngine on - then I'm stumped. Try "something-article.html". I'm totally out of my depth here.

    EDIT: As an afterthought, make sure /prog2/prog.php exists, or swap it with "/prog.php". Anything could be the problem, as far as I can see.
     
    WhiteHatHacker, Nov 8, 2006 IP
  7. mehbooba

    mehbooba Peon

    Messages:
    260
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thanks WH.. i'll work on it.
     
    mehbooba, Nov 8, 2006 IP
  8. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #8
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^a\-file\-article\.html$ prog2/prog [L]

    Post the complete URL of how it originally is and how you want it, if it's more than one URL.
     
    Nintendo, Nov 8, 2006 IP
  9. mehbooba

    mehbooba Peon

    Messages:
    260
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #9
    hi nintendo

    tried your code but still not working...... I tried putting the rewrite both in the root directory (eg www.mydomain.com/.htaccess) and in the sub directory.... but no luck.

    what it is, is that my program files are held in a sub directory :

    www.mydomain.com/articles/

    when someone enters the above url.. it lists the articles from my database.... the links are in the form :

    www.mydomain.com/the-life-and-times-of-reginald-perin-article.html

    the common feature is that EVERY link to the articles will have '-article.html' at the end.

    i have a php program : www.mydomain.com/articles/displayarticle

    which will parse the url and display the article.

    so what i want is a mod rewrite rule to say that whenever a url that has the text '-article.html' in it is click, the php program is called and executed.

    hope that makes sense

    thanks for your help.
     
    mehbooba, Nov 8, 2006 IP
  10. WhiteHatHacker

    WhiteHatHacker Peon

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    RewriteRule ^[^/]+\-article\.html$ /articles/displayarticle [L]
     
    WhiteHatHacker, Nov 8, 2006 IP
  11. mehbooba

    mehbooba Peon

    Messages:
    260
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #11
    whitehathacker

    can you advise where this rule in .htaccess file should go ? should it be in the root or the subdirectory ?

    thanks
     
    mehbooba, Nov 8, 2006 IP
  12. WhiteHatHacker

    WhiteHatHacker Peon

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    root, definitely
     
    WhiteHatHacker, Nov 8, 2006 IP
  13. mehbooba

    mehbooba Peon

    Messages:
    260
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #13
    sorry mate, no luck. still not working.
     
    mehbooba, Nov 8, 2006 IP
  14. WhiteHatHacker

    WhiteHatHacker Peon

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Okay, try
    RewriteRule -article\.html?$ /articles/displayarticle [L]
    Code (markup):
     
    WhiteHatHacker, Nov 8, 2006 IP
  15. mehbooba

    mehbooba Peon

    Messages:
    260
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #15
    well tried that..this time its not giving a page not found but sending all links to 'displayarticle' which then displays a piece of code i have written within "displayaricle" that says "file not found" (the script checks the database to see if that file exists, if not it will display a user friendly error saying no such article exists). That is done for ALL links... including if i enter www.mydomain.com
     
    mehbooba, 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
    Is /articles/displayarticle the actual php URL? Normally it looks more like whatever.php?blah=
     
    Nintendo, Nov 8, 2006 IP
  17. mehbooba

    mehbooba Peon

    Messages:
    260
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #17
    no thats the actual url. the php script is called "displayarticle".

    so the url to that script is "www.mydomain.com/displayarticle"


    its a script that takes the url and parses it..... so if you say :

    www.mydomain.com/something-about-me-article.html

    the script "displayarticle"....should be called.. it will take the url and parse it so it ends up with : 'something-about-me'

    it will then search the database and find a match for that article and display it.

    the script works 100% as I have tested it with the articles having the following links :

    www.mydomain.com/articles/something-about-me-article.html


    with .htaccess (in the directory /articles):

    RewriteRule ^(.*)$ /articles/displayarticle [L,QSA]



    that will automatically call the script 'displayarticle' which will then obtain the rest of the file name within the script and make a match within the database and display the article.

    however, i want it to be seo friendly so that google thinks the articles are called from a higher level.

    may be i am being petty.... i dont know.
     
    mehbooba, Nov 8, 2006 IP
  18. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #18
    Unless you can add something unique to the script URL for every article, aka mydomain.com/displayarticle?whatever=article you'll never get it to work. There's no way for mydomain.com/displayarticle to tell apache what article you're geting.

    All mod-rewrite does is lets you change existing URLs. The only URL you got for apache is mydomain.com/displayarticle
     
    Nintendo, Nov 8, 2006 IP
  19. mehbooba

    mehbooba Peon

    Messages:
    260
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #19
    nintendo

    but it works with this rewrite in the subdirectory:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /articles/displayarticle [L,QSA]


    honest guv it does.

    I will PM you the url so you can check it out yourself.

    regards

    M
     
    mehbooba, Nov 8, 2006 IP
  20. mehbooba

    mehbooba Peon

    Messages:
    260
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #20
    nintendo

    your comment :

    There's no way for mydomain.com/displayarticle to tell apache what article you're geting.


    I dont actually want to tell apache what article i am getting.. all i want it to do is to have a rewrite rule that says ANY link that has '-article.html' in the url, the program called /articles/displayarticle should be called. The program displayarticle will sort out what article it should be getting.
     
    mehbooba, Nov 8, 2006 IP