1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help with rewrite rules

Discussion in 'Apache' started by Lucky Bastard, Oct 30, 2005.

  1. #1
    I have a .htaccess in my root web dir with rewrite rules already setup, something like:
    Options -Indexes
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.com\[NC]
    RewriteRule ^([^/]*)\.html$ $1.php?%{QUERY_STRING} [NC]
    ....
    .....

    Firstly is it possible to add rewrite rules for subdirectories in this root webdir .htaccess?

    What I want to do is add rewrite calls for any files / subdirectories in a specific subdirectory to be rewritten to the main index.html page - and do this ONLY WHEN a specific remote host attempts to access them.
    So for example, the remote host is called crawler.remotehost.com
    and the subdirectory which I need anything under (including just it) to be rewritten is:
    /subdir
    rewritten to
    index.html

    so for example, when crawler.remotehost.com visits
    www.mydomain.com/subdir
    it is sent to
    www.mydomain.com/index.html

    or when crawler.remotehost.com visits
    www.mydomain.com/subdir/1.php
    it is sent to
    www.mydomain.com/index.html

    or when crawler.remotehost.com visits
    www.mydomain.com/subdir/subsubdir
    it is sent to
    www.mydomain.com/index.html

    or when crawler.remotehost.com visits
    www.mydomain.com/subdir/subsubdir/2.php
    it is sent to
    www.mydomain.com/index.html

    Hope this makes sense :)
     
    Lucky Bastard, Oct 30, 2005 IP
  2. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #2
    RewriteCond %{HTTP_HOST} ^crawler\.remotehost\.com$
    RewriteCond %{REQUEST_URI} ^subdir
    RewriteRule ^(.*)$ /index.html [R=301,L]
    Code (markup):
    will rewrite anything beginning with "subdir" to /index.html, but only if the host matches crawler.remotehost.com
     
    johnt, Oct 31, 2005 IP
  3. Lucky Bastard

    Lucky Bastard Peon

    Messages:
    406
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi again John :)

    Thanks for your help, when I add (for testing) the below lines to (the the front or end) of my existing .htaccess file (see my last post for extract of it) in the web root directory :
    RewriteCond %{REQUEST_URI} ^subdir
    RewriteRule ^(.*)$ /index.html [R=301,L]

    Nothing happens (ie. it doesn't redirect me to index.html) when I go to "subdir" or any page / subdirectory under it...

    Do these lines have to go anywhere in particular in my existing .htaccess, or do they have to sit in another .htaccess undder "subdir", or something else?

    Once I get this to work, I will add the first line (RewriteCond %{HTTP_HOST} ^crawler\.remotehost\.com$) and test it (one step at a time):
     
    Lucky Bastard, Oct 31, 2005 IP
  4. Lucky Bastard

    Lucky Bastard Peon

    Messages:
    406
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks John, got it working, I had to change:
    RewriteCond %{REQUEST_URI} ^subdir
    to
    RewriteCond %{REQUEST_URI} ^/subdir/

    to get it to work

    Thanks mate.
     
    Lucky Bastard, Oct 31, 2005 IP